Eight
涉及 康托展开 及 BFS
整的有点懵,参考了别人的代码,搞了好久
#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
int m,n;
const int Maxn=362880+5;
static const int FAC[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
struct node{
string path;//路径
int hashs;//哈希值
int pos;//x的位置
}now,net;
queue <node> q;
int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char op[5]="udlr";//反向 bfs -> 与上面搜索方法相反
int tmp[9];
int result=46234;//123456780 最终答案的hash值
string path[Maxn];//path[x] hash值为x的路径
int vis[Maxn];//vis[x] hash值为x的拼图序列是否标记过
int cantor(int *a, int n)
{
int x = 0;
for (int i = 0; i < n; ++i) {
int smaller = 0; // 在当前位之后小于其的个数
for (int j = i + 1; j < n; ++j) {
if (a[j] < a[i])
smaller++;
}
x += FAC[n - i - 1] * smaller; // 康托展开累加
}
return x + 1; // 康托展开值
}
void decantor(int x, int *a)
{
vector<int> v; // 存放当前可选数
for(int i=0; i<9; i++)
v.push_back(i);
for(int i=0; i<9; i++)
{
int r = x % FAC[9-i-1];
int t = x / FAC[9-i-1];
x = r;
sort(v.begin(),v.end());// 从小到大排序
a[i]=v[t]; // 剩余数里第t+1个数为当前位
v.erase(v.begin()+t); // 移除选做当前位的数
}
}
void bfs()
{
memset(vis,0,sizeof(vis));
for(int i=0; i<8; i++)//tmp一开始为123456780,从这开始打散拼图
tmp[i]=i+1;
tmp[8]=0;
now.pos=8;
now.hashs=result;
now.path="";
path[result]="";
vis[result]=1;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=0; i<4; i++)//四个方向
{
int tx=now.pos/3+dis[i][0];
int ty=now.pos%3+dis[i][1];
if(tx>=0&&ty>=0&&tx<=2&&ty<=2)//没走出去拼图
{
net=now;
net.pos=tx*3+ty;
decantor(now.hashs-1,tmp);//求tmp
swap(tmp[now.pos],tmp[net.pos]);//得到新的tmp
net.hashs=cantor(tmp,9);//得到新tmp对应的hash
if(!vis[net.hashs])//这都bfs老套路了 没啥说的
{
vis[net.hashs]=1;
net.path=op[i]+net.path;
q.push(net);
path[net.hashs]=net.path;
}
}
}
}
}
int main()
{
bfs();
char x;
while(cin>>x)
{
if(x=='x')
{
now.pos=0;
tmp[0]=0;
}else{
tmp[0]=x-'0';
}
for(int i=1;i<9;i++)
{
cin>>x;
if(x=='x')
{
now.pos=i;
tmp[i]=0;
}else{
tmp[i]=x-'0';
}
}
now.hashs=cantor(tmp,9);//求出tmp这个拼图序列的hash值
if(!vis[now.hashs])//这个hash没标记过,即没产生过这个拼图序列
cout<<"unsolvable"<<endl;
else
cout<<path[now.hashs]<<endl;//输出hash的路径
}
return 0;
}