python3题解

def bfs(maze, visited, path, i, j):
    directions = ((-1, 0), (1, 0), (0, -1), (0, 1))
    if i == m - 1 and j == n - 1:
        for p in path:
            print(f'({p[0]},{p[1]})')
        return
    for d in directions:
        iNew = i + d[0]
        jNew = j + d[1]
        if 0 <= iNew < m and 0 <= jNew < n and visited[iNew][jNew] == 0 \
            and maze[iNew][jNew] == 0:
            visited[iNew][jNew] = 1
            path.append((iNew,jNew))
            bfs(maze, visited, path, iNew, jNew)
            visited[iNew][jNew] = 0
            path.pop()
        
m, n = map(int, input().split())
maze = []
for _ in range(m):
    maze.append(list(map(int, input().split())))
visited = [[0] * n for _ in range(m)]
path = [(0,0)]
visited[0][0] = 1
bfs(maze, visited, path, 0, 0)