调了一下午终于ac了,恶心死我了23333333
这个题大佬们都是用什么康托展开,什么hash。。。
作为一个懒。。我不想写了,就写了个bool,暴力判断重的情况
可是九维的bool会爆数组啊,那我们就要想一个问题,因为数码不会重,知道了前八位,最后一位可以推出来,这样就不会爆了,就可以尽情的去爆搜了
我用了一个bfs
思路:先取出一个0,从0开时搜,交换值
交换值时打一个表就很好解决了
#include<cstdio>
#include<queue>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std;
struct st{
int s;
int l;
int ans;
};
bool ss[88888888];int n;
int mb=123804765;
int p[9]={100000000,10000000,1000000,100000,10000,1000,100,10,1};
int xx[9]={-1,3,-3,1};
queue <st> q;
int bfs(){
ss[n/10]=1;
while(!q.empty())
{
st x = q.front();
if(x.s == mb) {
printf("%d",x.ans);
return 1;
}
q.pop();
for(int i=0;i<=3;i++)
{
int x2=x.l+xx[i];
int j=x.l;
if(x2<0||x2>8) continue;
if(j%3==2&&xx[i]==1) continue;
if(j%3==0&&xx[i]==-1) continue;
int w=(x.s/p[x2])%10;
int www=(x.s/p[j]+w)*p[j]+x.s%p[j];
www=www/p[x2]/10*10*p[x2]+www%p[x2];
if(!ss[www/10])
{
// printf("%d %d\n",www,x.ans+1);
ss[www/10]=1;
st o;o.s=www;o.l=x2;o.ans=x.ans+1;
q.push(o);
}
}
}
}
int main()
{
scanf("%d",&n);
int ww=n,t=8;
while(ww){
if(ww%10==0)
break;
t--;
ww/=10;
}
st o;
o.s=n; o.l=t;o.ans=0;
q.push(o);
bfs();
return 0;
}