0pts求助
查看原帖
0pts求助
1486812
zzr134楼主2025/1/6 15:57

双向广搜0分,求助一下

#include <bits/stdc++.h>
using namespace std;
int xw[4]={0,1,0,-1},yw[4]={-1,0,1,0};
map <string,int>mp;
map <string,int>v;
queue <string> q;
signed main(){
	string s;
	cin>>s;
	int c=0;
	q.push(s);
	q.push("123804765");
	mp[s]=0;
	mp["123804765"]=1;
	v[s]=1;
	v["123804765"]=2;
	while(!q.empty()){
		c++;
		if(c==100) return 0;
		string now,t=q.front();
		now=t;
		q.pop();
		int k=t.find('0');
		int x=k/3,y=k%3,dis=mp[t];
		/*cout<<endl<<"-------------------------------------------"<<endl<<"now:"<<endl;
		for(int i=0;i<9;i++){
			if(i%3==0){
				cout<<endl;
			}
			cout<<now[i];
		}*/
		for(int i=0;i<=3;i++){
			int xx=x+xw[i];
			int yy=y+yw[i];
			if(xx<0||xx>2||yy<0||yy>2) continue;
			swap(t[k],t[xx*3+yy]);
			if (v[now] == v[t]){
				swap(t[k],t[xx*3+yy]);
				continue;
			}
			if (v[now] + v[t] == 3){
				cout<<mp[t]+mp[now];
				exit(0);
			}
			q.push(t);
			mp[t]=dis+1;
			v[t]=v[now];
			/*cout <<endl<<endl<<v[t]<<endl;
			for(int i=0;i<9;i++){
				if(i%3==0){
					cout<<endl;
				}
				cout<<t[i];
			}*/
			swap(t[k],t[xx*3+yy]);
		}
	}
	return 0;
}
2025/1/6 15:57
加载中...