题解 | 迷宫寻路

迷宫寻路

https://www.nowcoder.com/practice/0c8930e517444d04b426e9703d483ed4

import sys
from collections import deque

n,m = map(int,input().split())

map_matrix=[]
for _ in range(n):
    map_matrix.append(input())

arrived_place=deque()

is_arrived=[[1]*m for _ in range(n)]
if map_matrix[0][0]!='.':
    print('No')
else:
    is_arrived[0][0]=0
    arrived_place.append((0,0))

    while arrived_place:
        x,y=arrived_place.popleft()
        if x - 1 >= 0 and map_matrix[x-1][y]=="." and is_arrived[x-1][y]==1:
                arrived_place.append((x-1,y))
                is_arrived[x-1][y]=0
        if y - 1 >= 0 and map_matrix[x][y-1]=="." and is_arrived[x][y-1]==1:
                arrived_place.append((x,y-1))
                is_arrived[x][y-1]=0
        if x + 1 <= n-1 and map_matrix[x+1][y]=="." and is_arrived[x+1][y]==1:
                arrived_place.append((x+1,y))
                is_arrived[x+1][y]=0
        if y + 1 <= m-1 and map_matrix[x][y+1]=="." and is_arrived[x][y+1]==1:
                arrived_place.append((x,y+1))
                is_arrived[x][y+1]=0
    if is_arrived[n-1][m-1]==0:
        print('Yes')
    else:
        print('No')
        




全部评论

相关推荐

10-30 19:23
已编辑
山东大学(威海) C++
牛至超人:其实简历是不需要事无巨细的写的,让对方知道你有这段经历就行了,最重要的是面试的时候讲细讲明白
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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