求教 #2 #5数据点过不去,看不出来有什么问题,求大佬教一下

B3625 迷宫寻路

jkxuezhang @ 2024-08-22 16:17:09

n, m = map(int,input().split())
Map = []
for i in range(n):
    Map.append(list(input()))
path = [[0] * m for _ in range(n)]
ans = [0, 0]
Dir = [(1, 0), (-1, 0), (0, 1), (0, -1)]

def dfs(x, y, ww):
    global ans
    if x == n-1 and y == m-1:
        ans[0], ans[1] = x, y
        return

    for i in range(4):
        w, e = Dir[i]
        if n > w + x >= 0 and m > e + y >= 0:
            # x += w
            # y += e
            if path[x+w][y+e] != 1:
                if Map[x+w][y+e] != '#':
                    path[x][y] = 1
                    dfs(x+w, y+e, i)
                    break
    try:
        w, e = Dir[ww]
        path[x][y] = 1
        path[x-w][y-e] = 0
        dfs(x-w,y-e,0)
    except:
        return

dfs(0, 0, 0)
if ans[0] == (n-1) and ans[1] == (m-1):
    print("Yes")
else:
    print("No")

by jkxuezhang @ 2024-08-22 16:17:57

还有就是哪个回溯搞不明白要怎么调合适,容易越界


|