膜拜一位大神:ID叫“不太灵光的程序员”,思路是他的,我就是自己又写了一遍,缩小了一下行数。

def findway(x,y,path):
    if x == m-1 and y == n-1:
        [print(f'({l[0]},{l[1]})') for l in path]
    if x+1 <= m-1 and (x+1,y) not in path and maze[x+1][y] == '0':
        findway(x+1, y, path + [(x+1, y)])
    if y+1 <= n-1 and (x,y+1) not in path and maze[x][y+1] == '0':
        findway(x, y+1, path + [(x, y+1)])
    if x-1 >= 0 and (x-1,y) not in path and maze[x-1][y] == '0':
        findway(x-1, y, path + [(x-1, y)])
    if y-1 >= 0 and (x,y-1) not in path and maze[x][y-1] == '0':
        findway(x, y-1, path + [(x, y-1)])
while 1:
    try:
        m, n = map(int, input().split())
        maze = [input().split() for _ in range(m)]
        findway(0,0,[(0,0)])
    except:
        break