直接IDA*即可
/*
0 1
2 3
4 5 6 7 8 9 10
11 12
13 14 15 16 17 18 19
20 21
22 23
*/
#include <bits/stdc++.h>
using namespace std;
int a[25];
int cnt[4];
int path[100];
int b[8]={6,7,8,12,17,16,15,11};//检测答案
int c[8][7]={
{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}
};
int ops[8]={5,4,7,6,1,0,3,2};
int f()
{
int sum=0;
memset(cnt,0,sizeof cnt);
for(int i=0;i<8;i++)
{
cnt[a[b[i]]]++;
}
for(int i=1;i<=3;i++)
{
sum=max(cnt[i],sum);
}
return 8-sum;
}
bool ck()
{
for(int i=1;i<8;i++)
{
if(a[b[i-1]]!=a[b[i]]) return false;
}
return true;
}
void op(int x)
{
int t=a[c[x][0]];
for(int i=1;i<7;i++)
{
a[c[x][i-1]]=a[c[x][i]];
}
a[c[x][6]]=t;
}
bool dfs(int dep,int max_dep,int last)
{
if(f()+dep>max_dep) return false;
if(ck()) return true;
for(int i=0;i<8;i++)
{
if(i==ops[last]) continue;
op(i);
path[dep]=i;
if(dfs(dep+1,max_dep,i)) return true;
op(ops[i]);
}
return false;
}
int main()
{
while(cin>>a[0],a[0])
{
for(int i=1;i<24;i++) cin>>a[i];
int depth=0;
while(!dfs(0,depth,-1)) depth++;
if(depth==0)
{
puts("No moves needed");
printf("%d\n",a[6]);
}
else
{
for(int i=0;i<depth;i++) printf("%c",path[i]+'A');
printf("\n%d\n",a[6]);
}
}
return 0;
}
京公网安备 11010502036488号