选择了最简单的康托展开+打表。注意问题:多组输入。
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
#include <stdlib.h>
//#include <windows.h>
using namespace std;
//for(i=1;i<n;i++)
//scanf("%d",&n);
//printf("\n",);
struct node
{
char ch[3][3];
}x,y,temp;
int jiecheng[15];
char ch[50],inf[20],tteemmpp;
queue<node>q;
bool flg[400000];
string ans[400000];
int f()
{
int ans=0;
for(int i=0;i<9;i++)
{
int x=0;int c=1,m=1;
for(int j=i+1;j<9;j++)
{
if(inf[j]<inf[i])x++;
m*=c;c++;
}
ans+=x*m;
}
return ans+1;
}
int main()
{
int i,j,k,l;
jiecheng[1]=1;
for(i=1;i<11;i++)
jiecheng[i]=i*jiecheng[i-1];
for(i=0;i<400000;i++)
{
ans[i]="";
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
x.ch[i][j]=i*3+j+1;
inf[i*3+j]=x.ch[i][j];
}
flg[f()]=1;
q.push(x);int xx,yy;
while(!q.empty())
{
y=q.front();
q.pop();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
if(y.ch[i][j]==9)
{
xx=i;yy=j;
}
inf[i*3+j]=y.ch[i][j];
}
int tt=f();
if(yy+1<3)
{
temp=y;
tteemmpp=temp.ch[xx][yy];temp.ch[xx][yy]=temp.ch[xx][yy+1];temp.ch[xx][yy+1]=tteemmpp;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
inf[i*3+j]=temp.ch[i][j];
k=f();
if(flg[k]==0)
{
q.push(temp);
ans[k]="l"+ans[tt];flg[k]=1;
}
}
if(yy-1>=0)
{
temp=y;
tteemmpp=temp.ch[xx][yy];temp.ch[xx][yy]=temp.ch[xx][yy-1];temp.ch[xx][yy-1]=tteemmpp;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
inf[i*3+j]=temp.ch[i][j];
k=f();
if(flg[k]==0)
{
q.push(temp);
ans[k]="r"+ans[tt];flg[k]=1;
}
}
if(xx+1<3)
{
temp=y;
tteemmpp=temp.ch[xx][yy];temp.ch[xx][yy]=temp.ch[xx+1][yy];temp.ch[xx+1][yy]=tteemmpp;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
inf[i*3+j]=temp.ch[i][j];
k=f();
if(flg[k]==0)
{
q.push(temp);
ans[k]="u"+ans[tt];flg[k]=1;
}
}
if(xx-1>=0)
{
temp=y;
tteemmpp=temp.ch[xx][yy];temp.ch[xx][yy]=temp.ch[xx-1][yy];temp.ch[xx-1][yy]=tteemmpp;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
inf[i*3+j]=temp.ch[i][j];
k=f();
if(flg[k]==0)
{
q.push(temp);
ans[k]="d"+ans[tt];flg[k]=1;
}
}
}
while(gets(ch)>0)
{
j=0;
for(i=0;i<strlen(ch);i++)
{
if(ch[i]!=' ')
{
inf[j]=ch[i];
if(inf[j]=='x') inf[j]=='9';
inf[j]-='0';
j++;
}
}
int fin=f();
/*if(fin==1)
{
cout<<""<<endl;
continue;
}*/
if(flg[fin]==1) cout<<ans[fin]<<endl;
else printf("unsolvable\n");
}
return 0;
}