暴搜求调
查看原帖
暴搜求调
769006
crzcqh楼主2024/3/23 09:36
#include<bits/stdc++.h>
#include<queue>
#define ll long long
#define ans 123804765
using namespace std;
ll n;
ll dx[4]={-1,0,0,1},dy[4]={0,-1,1,0};
map<ll,ll> f;
queue<ll> q;
int main(){
	cin>>n;
	q.push(n);
	f[n]=0;
	while(!q.empty()){
		ll u=q.front(),c[3][3],x=0,y=0;
		q.pop();
		if(u==ans) break;	
		for(int i=2;i>=0;i--)
			for(int j=2;j>=0;j--){
				c[i][j]=u%10;
				u/=10;
				if(!c[i][j]) x=i,y=j;
			}
		for(int k=0;k<4;k++){
			ll px=x+dx[k],py=y+dy[k],tmp=0;
			//cout<<px<<' '<<py<<endl;
			if(px<0||px>2||py<0||py>2) continue;
			swap(c[px][py],c[x][y]);
			for(int i=0;i<=2;i++)
				for(int j=0;j<=2;j++){
					tmp*=10;
					tmp+=c[i][j];
				}
			//cout<<tmp<<endl;
			if(!f[tmp]||f[tmp]>f[u]+1){
				f[tmp]=f[u]+1;
				cout<<f[tmp]<<' '<<f[u]<<endl;//这里出问题了,但是一直没找到错误 
				q.push(tmp);
			}
			swap(c[px][py],c[x][y]);
		}
	}
	cout<<f[ans];
	return 0;
}
2024/3/23 09:36
加载中...