70分 非零返回求助!

B3625 迷宫寻路

THU_ACMER @ 2024-02-18 23:40:42

n, m = map(int, input().split())
maze = [['#' for i in range(m+1)]]
for i in range(n):
    lst = list(input())
    lst.insert(0, '#')
    maze.append(lst)  # 构建迷宫
maze.append(['#' for i in range(m+1)])
directions = [
    lambda x, y:(x+1, y),  # 右
    lambda x, y:(x, y+1),  # 下
    lambda x, y:(x-1, y),  # 左
    lambda x, y:(x, y-1)  # 上
]

def maze_path(maze):
    stack = [(1, 1)]
    while len(stack) > 0:
        now_site = stack[-1]
        if now_site[0] == n and now_site[1] == m:
            print('Yes')
            break
        for direction in directions:  # 四个方向
            next_site = direction(now_site[0], now_site[1])
            if maze[next_site[0]][next_site[1]] == '.':
                stack.append(next_site)
                maze[next_site[0]][next_site[1]] = '#'
                break
        else:  # 接下来没路了
            maze[now_site[0]][now_site[1]] = '#'
            stack.pop()
    else:
        print('No')

maze_path(maze)

|