第一行输入两个正整数
。
接下来的
行每行输入一个长为
的仅包含字符 `.` 与 `#` 的字符串,描述整个迷宫。
保证起点
和终点
均为空地。
若旺仔哥哥可以走到终点,则输出单词
;否则输出
。
3 5 .##.# .#... ...#.
Yes
路线如下:
n , m = map(int,input().split()) arr = [] for i in range(n): arr.append(list(input())) graph = {} for i in range(n): for j in range(m): if arr[i][j] == ".": graph[(i,j)] = [] if i - 1 >= 0 and arr[i-1][j]==".": graph[(i,j)].append((i-1,j)) if j - 1 >= 0 and arr[i][j-1]==".": graph[(i,j)].append((i,j-1)) if j + 1 <= m - 1 and arr[i][j+1]==".": graph[(i,j)].append((i,j+1)) if i + 1 <= n - 1 and arr[i+1][j]==".": graph[(i,j)].append((i+1,j)) # print(graph) def can_reach(graph, start, target): stack = [start] visited = set() while stack: current = stack.pop() if current == target: return "Yes" if current in visited: continue visited.add(current) for neighbor in graph[current]: if neighbor not in visited: stack.append(neighbor) return "No" print(can_reach(graph,(0,0),(n-1,m-1)))