Advertisement
Alex-Flexer

Untitled

Jul 13th, 2023
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. # EXAMPLE
  2. MATRIX = [[1, 0, 1, 1, 0, 1],
  3. [0, 1, 1, 0, 1, 0],
  4. [0, 1, 0, 1, 1, 1],
  5. [1, 0, 0, 0, 0, 1],
  6. [1, 1, 1, 1, 1, 1],
  7. [0, 0, 1, 0, 0, 0],
  8. [1, 1, 0, 0, 0, 0]]
  9.  
  10. A = len(MATRIX[0])
  11. B = len(MATRIX)
  12.  
  13. parts = [[]]
  14.  
  15. def f(x, y):
  16. parts[-1].append((x, y))
  17. # right
  18. if x < A - 1 and (x + 1, y) not in parts[-1] and MATRIX[y][x + 1]:
  19. f(x + 1, y)
  20. # left
  21. if x > 0 and (x - 1, y) not in parts[-1] and MATRIX[y][x - 1]:
  22. f(x - 1, y)
  23. # down
  24. if y > 0 and (x, y - 1) not in parts[-1] and MATRIX[y - 1][x]:
  25. f(x, y - 1)
  26. # up
  27. if y < B - 1 and (x, y + 1) not in parts[-1] and MATRIX[y + 1][x]:
  28. f(x, y + 1)
  29.  
  30.  
  31. def main():
  32. for y in range(B):
  33. for x in range(A):
  34. if MATRIX[y][x] and all([(x, y) not in part for part in parts]):
  35. parts.append([])
  36. f(x, y)
  37. del parts[0]
  38.  
  39. for ind, part in enumerate(parts, start=1):
  40. print(f'{ind}:', *part, sep=' ')
  41.  
  42. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement