DFS模板
2026-08-12 15:12:43
发布于:江苏
7阅读
0回复
0点赞
DFS模板
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,t,sx,sy,fx,fy;
int ans=0;
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
bool vis[45][45],flag=0;
void dfs(int nx,int ny){
if(nx==fx&&ny==fy){
ans++;
return ;
}
for(int i=0;i<4;i++){
int tx=nx+dx[i];
int ty=ny+dy[i];
if(tx<1||tx>n||ty<1||ty>m){
continue;
}
if(vis[tx][ty]==1){
continue;
}
vis[tx][ty]=1;
dfs(tx,ty);
vis[tx][ty]=0;
}
}
signed main(){
cin>>n>>m>>t;
cin>>sx>>sy>>fx>>fy;
while(t--){
int a,b;
cin>>a>>b;
vis[a][b]=1;
}
vis[sx][sy]=1;
dfs(sx,sy);
cout<<ans;
return 0;
}
全部评论 2
d
1周前 来自 江苏
0d
1周前 来自 江苏
0






有帮助,赞一个