笨办法,靠调试过的
a = list(map(int, input().split())) c = [] while True: try: b = input().split() c += [b] except: break s = [[0,0]] n_i=[] def find_path(i, j): global a global n_i global s n = [] try: if [i+1,j] not in s and i + 1 < a[0] and j + 1 < a[1] and c[i + 1][j] == "0" and c[i][j + 1] == "0": n_i = s+[[i,j+1]] s += [[i+1,j]] n += [[i + 1, j], [i, j + 1]] elif [i+1,j] not in s and i + 1 < a[0] and c[i + 1][j] == "0": s += [[i+1,j]] n += [[i+1,j]] if i + 1 == a[0]-1 and j == a[1]-1: for d in s: print('(' + str(d[0]) + ',' + str(d[1]) + ')') elif [i,j+1] not in s and j + 1 < a[1] and c[i][j+1] == "0": s+= [[i,j+1]] n+= [[i,j+1]] if i == a[0]-1 and j+1 == a[1]-1: for d in s: print('(' + str(d[0]) + ',' + str(d[1]) + ')') elif [i-1,j] not in s and i - 1 >=0 and c[i - 1][j] == "0": s += [[i-1,j]] n += [[i-1,j]] elif [i,j-1] not in s and j-1 >=0 and c[i][j-1] == "0": s += [[i,j-1]] n += [[i,j-1]] else: s = n_i except: pass for k in n: find_path(k[0], k[1]) find_path(0,0)