题解 | 迷宫问题

global_rout = []
m,n = input().split()
m,n = int(m),int(n)
mapp = []
for i in range(m) :
    mapp.append(list(map(int,input().split())))
# print(mapp)

def print_route(route):
    for r in route:
        print(f"({r[0]},{r[1]})")

def dfs_fun(rout,x,y):        
    if -1<x<m and -1<y<n and mapp[x][y] == 0 and ([x,y] not in rout):
        rout = rout + [[x,y]]
        if x == m-1 and y == n-1:
            global_rout.append(rout)
        dfs_fun(rout,x+1,y) 
        dfs_fun(rout,x,y+1)
        dfs_fun(rout,x-1,y) 
        dfs_fun(rout,x,y-1)

dfs_fun([[0,0]],1,0)
dfs_fun([[0,0]],0,1)
# print(len(global_rout))
if len(global_rout) > 0:
    print_route(global_rout[-1])


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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