题解 | 迷宫问题
迷宫问题
https://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc
h, w = [int(i) for i in input().split()]
std = []
for i in range(h):
row = [int(i) for i in input().split()]
std.append(row)
def walk(x, y, pos=[(0, 0)]):
if x + 1 < w and std[y][x+1] == 0:
if (y, x+1) not in pos:
walk(x+1, y, pos=pos+[(y, x+1)])
if x - 1 >= 0 and std[y][x-1] == 0:
if (y, x-1) not in pos:
walk(x-1, y, pos=pos+[(y, x-1)])
if y + 1 < h and std[y+1][x] == 0:
if (y+1, x) not in pos:
walk(x, y+1, pos=pos+[(y+1, x)])
if y - 1 >= 0 and std[y-1][x] == 0:
if (y-1, x) not in pos:
walk(x, y-1, pos=pos+[(y-1, x)])
if x == w - 1 and y == h - 1:
for i in pos:
print("".join(str(i).split()))
walk(0, 0)
