Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # EXAMPLE
- MATRIX = [[1, 0, 1, 1, 0, 1],
- [0, 1, 1, 0, 1, 0],
- [0, 1, 0, 1, 1, 1],
- [1, 0, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 1],
- [0, 0, 1, 0, 0, 0],
- [1, 1, 0, 0, 0, 0]]
- A = len(MATRIX[0])
- B = len(MATRIX)
- parts = [[]]
- def f(x, y):
- parts[-1].append((x, y))
- # right
- if x < A - 1 and (x + 1, y) not in parts[-1] and MATRIX[y][x + 1]:
- f(x + 1, y)
- # left
- if x > 0 and (x - 1, y) not in parts[-1] and MATRIX[y][x - 1]:
- f(x - 1, y)
- # down
- if y > 0 and (x, y - 1) not in parts[-1] and MATRIX[y - 1][x]:
- f(x, y - 1)
- # up
- if y < B - 1 and (x, y + 1) not in parts[-1] and MATRIX[y + 1][x]:
- f(x, y + 1)
- def main():
- for y in range(B):
- for x in range(A):
- if MATRIX[y][x] and all([(x, y) not in part for part in parts]):
- parts.append([])
- f(x, y)
- del parts[0]
- for ind, part in enumerate(parts, start=1):
- print(f'{ind}:', *part, sep=' ')
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement