10分 迷宫求助 python
查看原帖
10分 迷宫求助 python
1095779
xiao_xi楼主2024/3/7 12:57
n, m = map(int, input().split())
map = []
for _ in range(n):
    map.append(list(input()))

dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]  # 上下左右


def in_map(x, y):
    return x >= 0 and y >= 0 and x < n and y < m



result = False


def dfs(x, y):  # x y轴与步数
    global result
    if x == n - 1 and y == m - 1:
        result = True
        
    if result:  # 只判断能否出去
        return
   
    map[x][y] = '#'  # 走过的路不能走
    for i in dir:
        if in_map(x + i[0], y + i[1]) and map[x + i[0]][y + i[1]] == '.':
            dfs(x + i[0], y + i[1])
   
    return


dfs(0, 0)


if result:
    print('Yes')
else:
    print('No')

哪里错了啊,第一个对了其他全是RE

2024/3/7 12:57
加载中...