题解 | 迷宫寻路
迷宫寻路
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')
查看27道真题和解析
