搜索

搜索

基础的 DFS 和 BFS ,不说了,多练就有感觉了

例题

Bouncy Ball

解法就是模拟。。。另外就是注意怎么停止dfs!详见代码。

using namespace std;
const int maxm=5e5+5,inf=0x3f3f3f3f,mod=998244353;
int n,m,i[2],j[2],ans;
int fx[4]={1,1,-1,-1},fy[4]={1,-1,1,-1};
bool flag=false;
string d;
map<int,map<string,int>> a;//利用这个对搜索进行标记,去重

void dfs(int x,int y,string ss){
	int id;
	if(ss=="DR") id=0;
	else if(ss=="DL") id=1;
	else if(ss=="UR") id=2;
	else if(ss=="UL") id=3;
	int xx=x,yy=y;
	if(a[m*(xx-1)+yy][ss]){
		return ;
	}else a[m*(xx-1)+yy][ss]=1;
	if(xx==i[1]&&yy==j[1]){
		flag=true;
		return ;
	}
	xx=xx+fx[id];
	yy=yy+fy[id];
	while(xx>=1&&xx<=n&&yy>=1&&yy<=m){
		if(xx==i[1]&&yy==j[1]){
			flag=true;
			return ;
		}
		if(xx==x&&yy==y){
			return ;
		}
		if(a[m*(xx-1)+yy][ss]){
			return ;
		}else a[m*(xx-1)+yy][ss]=1;
		xx=xx+fx[id];
		yy=yy+fy[id];
	}
	string s;
	if(xx==0) s="D";
	else if(xx==n+1) s="U";
	else s=ss[0];
	if(yy==0) s+="R";
	else if(yy==m+1) s+="L";
	else s+=ss[1];
	// cout<<" s: "<<s<<" "<<xx<<" yy: "<<yy<<"\n";
	// cout<<xx-fx[id]<<" xx yy: "<<yy-fy[id]<<"\n";
	++ans;
	dfs(xx-fx[id],yy-fy[id],s);
	return ;
}

void solve(){
	cin>>n>>m>>i[0]>>j[0]>>i[1]>>j[1]>>d;
	ans=0;
	flag=false;
	a.clear();
	dfs(i[0],j[0],d);
	if(flag) cout<<ans<<'\n';
	else cout<<-1<<'\n';
	return ;
}

Tying Rope

图论题
利用dfs判环
题解看官方题解,不留链接和代码了

2022 ICPC 沈阳 L. Tavern Chess

搜索遍历所有情况求概率

posted on 2023-03-20 21:49  Qiansui  阅读(20)  评论(0编辑  收藏  举报