题解 | 迷宫问题

h, w = map(int, input().split())
s = [list(map(int, input().split())) for _ in range(h)]

ans = []
vis = [[False for _ in range(w)] for _ in range(h)]
def dfs(i, j):
    vis[i][j] = True
    directions = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    for dx, dy in directions:
        nx, ny = i+dx, j+dy
        if not vis[h-1][w-1] and 0<=nx<h and 0<=ny<w and s[nx][ny] == 0 and not vis[nx][ny]:
            dfs(nx, ny)
            
    if vis[h-1][w-1]:
        ans.append((i,j))

dfs(0, 0)
ans = reversed(ans)
for i in ans:
    print(f'({i[0]},{i[1]})')

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务