#include<bits/stdc++.h>
using namespace std;
const int N = 10;
int n,m,t,sx,sy,fx,fy,ans = 0;
int a,b;
int table[N][N];
int vis[N][N];
int dir[][2] = {{0,1},{1,0},{0,-1},{-1,0}};
int check(int x,int y){
if(x < 1 || x > n)return 0;
if(y < 1 || y > m)return 0;
return 1;
}
void DFS(int x,int y){
//cout<<x<<" "<<y<<endl;
//if(!check(x,y))return;
if(x == fx && y == fy){
ans++;
return;
}
for(int i = 0;i <= 3;i++)
if(table[x + dir[i][0]][y + dir[i][1]] == 1 && !vis[x + dir[i][0]][y + dir[i][1]]){
vis[x][y] = 1;//是给走过的打上标记
DFS(x + dir[i][0],y + dir[i][1]);
vis[x][y] = 0;
}
//如果没有障碍并且不是自己走过的,就进一步搜索,把自己走过的路打上标记,返回时,再将标记还原;
}
int main(){
cin>>n>>m>>t;
cin>>sx>>sy;
cin>>fx>>fy;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)table[i][j] = 1;
//有一个zz把 table[i][j] = 1 写成table[i][j] == 1 然后查了半个小时代码 没错是我
for(int i = 1;i <= t;i++){
cin>>a>>b;
table[a][b] = 0;
}
DFS(sx,sy);
cout<<ans;
return 0;
}
P1605 迷宫(洛谷 地图型DFS)
最新推荐文章于 2023-05-15 20:55:18 发布