-
-
Save Q726kbXuN/a56bde5b7ea0e6fe1531584851a8b771 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
grid = [3,3,1,3,2,2,0,0,2,1,2,0,0,0,0,0,1,1,3,0,3,3,1,0,3] | |
todo = deque([(grid, [])]) | |
seen = set() | |
while True: | |
grid, trail = todo.pop() | |
if sum(grid) == -25: | |
print(trail) | |
exit(0) | |
for x_col in range(5): | |
target = grid[x_col + 20] | |
if target != -1: | |
temp = grid[:] | |
flood = deque([(x_col, 4)]) | |
while len(flood): | |
x, y = flood.pop() | |
temp[x+y*5] = -1 | |
for xo, yo in [[-1, 0], [1, 0], [0, -1], [0, 1]]: | |
if 0 <= x+xo <= 4 and 0 <= y+yo <= 4 and temp[x+xo+(y+yo)*5] == target: | |
flood.append((x+xo,y+yo)) | |
for x in range(5): | |
col = [temp[x+y*5] for y in range(4,-1,-1)] | |
if sum(col) != -5: | |
col = [i for i in col if i >= 0] | |
col = col + [-1] * (5 - len(col)) | |
for i, val in enumerate(col): | |
temp[x + (4 - i) * 5] = val | |
if tuple(temp) not in seen: | |
seen.add(tuple(temp)) | |
todo.appendleft((temp, trail + [x_col])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment