题解 | #迷宫问题#
迷宫问题
http://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc
LeetCode 面试题 08.02. 迷路的机器人 类似,这是增加上走和左走。
def dfs(i, j):
if (i, j) in seen or i < 0 or i >= m or j < 0 or j >= n or mg[i][j]:
return False
seen.add((i, j))
# path.append((i, j))
path.append('({},{})'.format(i, j))
# print(path)
if (i, j) == (m-1, n-1) or dfs(i + 1, j) or dfs(i, j + 1) or dfs(i - 1, j) or dfs(i, j - 1):
return True
path.pop()
# return False
while True:
try:
m, n = list(map(int, input().split()))
mg = []
for i in range(m):
mg.append(list(map(int, input().split())))
# print(mg)
path = []
seen = set()
dfs(0, 0)
for l in path:
print(l)
except:
break