[DFS] [洛谷] P1605 迷宫
迷宫类是经典dfs
地图数组标记障碍物
存在数组表姐已经是否走过
边dfs边回溯
枚举所有符合题意的组合
#include <iostream>
using namespace std;
const int MAXN = 6;
int N, M, T, ans = 0;
int bx, by, ex, ey, ta, tb;
int arr[MAXN][MAXN] = {0}, exz[MAXN][MAXN] = {0};
int move[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0 , 1}};
void dfs(int x, int y)
{
if(x == ex && y == ey)
{
ans ++;
return ;
}
for(int i = 0; i < 4; i++)
{
ta = x + move[i][0];
tb = y + move[i][1];
if(ta > 0 && ta <= N && tb > 0 && tb <= M && arr[ta][tb] != 1 && exz[ta][tb] == 0)
{
exz[x][y] = 1;
dfs(ta, tb);
exz[x][y] = 0;
}
}
}
int main()
{
cin>>N>>M>>T;
cin>>bx>>by>>ex>>ey;
while(T--)
{
cin>>ta>>tb;
arr[ta][tb] = 1;
}
dfs(bx, by);
cout<<ans<<endl;
return 0;
}