为什么dfs死循环了
  • 板块P1605 迷宫
  • 楼主zlttcl
  • 当前回复9
  • 已保存回复9
  • 发布时间2022/4/27 14:50
  • 上次更新2023/10/28 02:48:45
查看原帖
为什么dfs死循环了
555381
zlttcl楼主2022/4/27 14:50
#include <bits/stdc++.h>
using namespace std;
int xi[5]={0,-1,1,0,0};
int yi[5]={0,0,0,-1,1};
int n,m,t,x1,x2,y2,ans,aa,bb;
int a[110][110];
bool p[110][110];
void dfs(int x,int y){
	if(x==x2&&y==y2){
		++ans;
		return;
	}
	for(int i=1;i<=4;++i){
		int xx=x+xi[i];
		int yy=y+yi[i];
		if(!p[xx][yy]&&a[xx][yy]==0){
			p[xx][yy]=true;
			dfs(xx,yy);
			p[xx][yy]=false;
		}
	}
}
int main(){
	int y1;
	cin>> n >> m >> t;
	cin>> x1 >> y1 >> x2 >> y2;
	while(t--){
		cin>> aa >> bb;
		a[aa][bb]=1;
	}
	dfs(x1,y1);
	cout<<ans;
	return 0;
}
2022/4/27 14:50
加载中...