1 条题解

  • 1
    @ 2025-4-12 19:53:07

    可以直接用string数组整体考虑 (上面的三个函数其实是我自己不知道string的函数怎么用的…)

    #include<bits/stdc++.h>
    using namespace std;
    char te,ttt[111][111];
    string f[5000],e;
    int fr[5000],po,h=0,t=1,t2=1,de=1;
    int p(int n)
    {
    	for(int i=0;i<9;i++) if(f[n][i]!=e[i]) return 0;
    	return 1;
    }
    int fi(int n)
    {
    	for(int i=0;i<9;i++) if(f[n][i]=='0') return i;
    }
    void cop(int fr,int to)
    {
    	for(int i=0;i<9;i++) f[to][i]=f[fr][i];
    	return;
    }
    int main()
    {
    	freopen("8num.in","r",stdin);
    	freopen("8num.out","w",stdout);
    	cin>>f[0]>>e;
    	while(1)
    	{
    		po=fi(h);
    		if(po>2){t++;cop(h,t);swap(f[t][po],f[t][po-3]);fr[t]=h;if(p(t)) break;}
    		if(po!=0&&po!=3&&po!=6){t++;cop(h,t);swap(f[t][po],f[t][po-1]);fr[t]=h;if(p(t)) break;}
    		if(po<6){t++;cop(h,t);swap(f[t][po],f[t][po+3]);fr[t]=h;if(p(t)) break;}
    		if(po!=2&&po!=5&&po!=8){t++;cop(h,t);swap(f[t][po],f[t][po+1]);fr[t]=h;if(p(t)) break;}
    		if(h==t2){t2=t;de++;}
    		if(de>6){cout<<"No solution!";return 0;}
    		h++;
    	}
    	cout<<de<<endl;
    	for(int i=0;i<9;i++) cout<<f[0][i];
    	cout<<endl;
    	for(int i=0;i<de;i++){
    		for(int j=0;j<9;j++) ttt[i][j]=f[t][j];
    		t=fr[t];
    	}
    	for(int i=de-1;i>=0;i--){
    		for(int j=0;j<9;j++) cout<<ttt[i][j];
    		cout<<endl;
    	}
    	return 0;
    }
    
    

    信息

    ID
    455
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    65
    已通过
    17
    上传者