参考的刘汝佳大神的思路 IDA*算法 感觉用bfs会有超内存的危险==
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int maxn = 25;
const int id[10][10] =
{ {0, 2, 6, 11, 15, 20, 22},
{1, 3, 8, 12, 17, 21, 23},
{10, 9, 8, 7, 6, 5, 4 },
{19, 18, 17, 16, 15, 14, 13},
{23, 21, 17, 12, 8, 3, 1},
{22, 20, 15, 11, 6, 2, 0},
{13, 14, 15, 16, 17, 18, 19},
{4, 5, 6, 7, 8, 9, 10} };
const int cen[10] = {6, 7, 8, 11, 12, 15, 16, 17};
const int rev[8] = {5, 4, 7, 6, 1, 0, 3, 2};
char ans[10000];
int str[maxn];
int dep;
bool judge()
{
for(int i = 0; i < 8; ++i)
if(str[cen[i]] != str[cen[0]]) return false;
return true;
}
void move(int i) {
int t = str[id[i][0]];
for(int j = 0; j < 6; j++) str[id[i][j]] = str[id[i][j+1]];
str[id[i][6]] = t;
}
int diff(int x) {
int res = 0;
for(int i = 0; i < 8; ++i) if(str[cen[i]] != x) ++res;
return res;
}
int q() {
return min(diff(1), min(diff(2), diff(3)));
}
bool dfs(int d) {
if(judge()) {
//cout << "++" << endl;
ans[d] = '\0';
printf("%s\n", ans);
return true;
}
if(d + q() > dep) return false;
for(int i = 0; i < 8; ++i) {
char t = 'A' + i;
ans[d] = t;
move(i);
if(dfs(d+1)) return true;
move(rev[i]);
}
return false;
}
int main()
{
while(scanf("%d", &str[0]) != EOF && str[0]) {
for(int i = 1; i < 24; ++i) scanf("%d", &str[i]);
memset(ans, 0, sizeof(ans));
if(judge()) printf("No moves needed\n");
else {
dep = 0;
while(!dfs(0)) ++dep;
}
//printf("%d\n", dep);
// for(int i = 0; i < 24; ++i) printf("%d ", str[i]);
// printf("\n");
printf("%d\n", str[7]);
}
return 0;
}