P1605. 迷宫

考点

  • DFS

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <bits/stdc++.h>
using namespace std;
const int LEN = 6;
int N, M, bg_x, bg_y, ed_x, ed_y, ans;
int arr[LEN][LEN], vis[LEN][LEN], obs[LEN][LEN], walk[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

void dfs(int x, int y)
{
if (x < 1 || x > N || y < 1 || y > M || vis[x][y])
return;
if (x == ed_x && y == ed_y)
{
++ans;
return;
}
vis[x][y] = 1;
for (int i = 0; i < 4; ++i)
dfs(x + walk[i][0], y + walk[i][1]);
vis[x][y] = 0;
}

int main()
{
int t, x, y;
cin >> N >> M >> t >> bg_x >> bg_y >> ed_x >> ed_y;
while (t--)
{
cin >> x >> y;
vis[x][y] = 1;
}
dfs(bg_x, bg_y);
cout << ans;
return 0;
}

思路

DFS模板题,直接套DFS模板即可

这里偷了懒,将障碍物直接标记为“已走过”也可以达到阻挡路径的效果

但还是要养成好习惯,单独再建一个障碍物数组比较好...防止吃亏