题解 | #迷宫问题#

迷宫问题

https://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc

from collections import deque

n,m=map(int,input().split())
lst=[[]]*n

for i in range(n):
    lst[i]=input().split()
parent_map={}
pq=deque([(0,0)])
visited={(0,0)}
while pq:
    current_x,current_y=pq.popleft()
    for neix,neiy in[(current_x+1,current_y),(current_x,current_y+1),(current_x-1,current_y),(current_x,current_y-1)]:
        if neix==n or neiy==m or neix==-1 or neiy==-1:
            continue
        if lst[neix][neiy]=="0":
            if (neix,neiy) not in visited:
                visited.add((neix,neiy))
                pq.append((neix,neiy))
                parent_map[(neix,neiy)]=(current_x,current_y)
                if neix==n-1 and neiy==m-1:
                    path=[(neix,neiy)]
                    while path[-1]!=(0,0):
                        path.append(parent_map[path[-1]])
                    path.reverse()
                    for i in path:
                        print("(",i[0],",",i[1],")",sep="")
                    break




全部评论

相关推荐

Rac000n:淘天-客户运营部-AI研发工程师,智能客服方向,暑期实习招聘,欢迎联系
点赞 评论 收藏
分享
焦虑中,不知道怎么办了。。。
西北上单:应该放俩项目合理一些 我是一个业务开发项目 一个AI项目和你这个写的亮点差不多
你的简历改到第几版了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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