思路:IDA*。
注意:存储时可以就按题目中所给的形式。初始化时不能打错了。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<sstream>
#include<queue>
using namespace std;
#define n 8
#define m 24
#define num 7
int const Move[n+5][n+5]= {{0},{0,1,3,7,12,16,21,23},{0,2,4,9,13,18,22,24},{0,11,10,9,8,7,6,5},{0,20,19,18,17,16,15,14},{0,24,22,18,13,9,4,2},{0,23,21,16,12,7,3,1},{0,14,15,16,17,18,19,20},{0,5,6,7,8,9,10,11}};
int const Central[n+5]= {0,7,8,9,12,13,16,17,18};
int maxd=0;
string Mstring;
int b;
int judge(int a[]) {
int cnt[4]= {0};
for(int i=1; i<=n; i++) {
cnt[a[Central[i]]]++;
}
int Max=0,what;
for(int i=1; i<=3; i++) {
if(cnt[i]>Max) {
Max=cnt[i],what=i;
}
}
if(Max==8) return what;
return 0-Max;
}
void DO(int (&c)[m+5],int x) {
int t=c[Move[x][1]];
for(int i=1; i<num; i++) {
c[Move[x][i]]=c[Move[x][i+1]];
}
c[Move[x][num]]=t;
}
bool dfs(int d,int a[],string step) {
if(d==maxd) {
int x=judge(a);
if(x<0) return false;
b=x;
Mstring=step;
return true;
}
if(maxd<d+8+judge(a)) return false;
for(int i=1; i<=n; i++) {
int c[m+5];
memcpy(c,a,sizeof(c));
DO(c,i);
if(dfs(d+1,c,step+(char)(i+'A'-1))) return true;
}
return false;
}
int main() {
int a[m+5];
while(~scanf("%d",&a[1])&&a[1]!=0) {
for(int i=2; i<=m; i++) scanf("%d",&a[i]);
int x=judge(a);
if(x>0) {
printf("No moves needed\n%d\n",x);
continue;
}
maxd=0;
while(++maxd) {
if(dfs(0,a,"")) break;
}
cout<<Mstring<<endl;
printf("%d\n",b);
}
return 0;
}