这份代码,AC并抢到了最优解,结果样例4输出是错误的:
#include<bits/stdc++.h>
using namespace std;
int a[12][4];
struct node{
int a;
string go;
double f;
void init(){
int s=0;
for(int i=0;i<12;i++){
if((a>>(i<<1))&3) s+=4-((a>>(i<<1))&3);
}
f=go.length()+s*1.1;
}
bool operator<(const node &y) const{
return f>y.f;
}
};
bool vis[17000000];
priority_queue<node> q;
void print(string str){
int len=str.length();
printf("%d\n",len);
for(int i=0;i<len;i++) printf("%d ",str[i]+1);
}
int main(){
node start;
start.go="";
start.a=0;
for(int i=0;i<12;i++){
int t;
scanf("%d",&t);
start.a|=(t-1)<<(i<<1);
for(int j=0;j<4;j++){
scanf("%d",&a[i][j]);
a[i][j]--;
}
}
start.init();
vis[start.a]=1;
q.push(start);
while(!q.empty()){
node now=q.top();
q.pop();
if(!now.a){
print(now.go);
break;
}
for(int i=0;i<12;i++){
int t=now.a,w=(now.a>>(i<<1))&3;
t^=w<<(i<<1);
if(w<3) t|=(w+1)<<(i<<1);
if(a[i][w]!=i){
int j=a[i][w];
w=(now.a>>(j<<1))&3;
t^=w<<(j<<1);
if(w<3) t|=(w+1)<<(j<<1);
}
if(!vis[t]){
vis[t]=1;
node put;
put.a=t;
put.go=now.go+char(i);
put.init();
q.push(put);
}
}
}
return 0;
}