P1605 迷宫 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
dfs代码:
这道题也是简单的深搜问题,但是要注意地图的原点记得要初始化maps[sx-1][sy-1] = 1
n, m, t = map(int,input().split())
sx, sy, fx, fy = map(int,input().split())
maps = [[0 for _ in range(m)] for _ in range(n)]
flag = [[0 for _ in range(m)] for _ in range(n)]
for i in range(t):
tx, ty = map(int, input().split())
maps[tx-1][ty-1] = 1
ans = 0
d = [[1,0], [-1,0], [0,1], [0,-1]]
maps[sx-1][sy-1] = 1
def dfs(sx,sy):
global ans
if sx == fx-1 and sy == fy-1:
ans += 1
return
for xx, yy in d:
dx = sx+xx
dy = sy+yy
if 0<=dx<n and 0<=dy<m:
if flag[dx][dy] == 0 and maps[dx][dy] == 0:
flag[dx][dy] = 1
dfs(dx,dy)
flag[dx][dy] = 0
dfs(sx-1,sy-1)
print(ans)
BFS做这道题可以吗???