#include<bits/stdc++.h>
#define debu
using namespace std;
int cmd[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},
};
const int opp[8]= {5,4,7,6,1,0,3,2};
const int center[8]= {6,7,8,12,17,16,15,11};
int a[30];
char ans[1000];
inline int minlast(int last)
{
int tmp=0;
for(int i=0; i<8; i++)
if(a[center[i]]!=last) tmp++;
return tmp;
}
inline int f()
{
return min(min(minlast(1),minlast(2)),minlast(3));
}
inline void Move(int line)
{
int tmp=a[cmd[line][0]];
for(int i=0; i<6; i++)
a[cmd[line][i]]=a[cmd[line][i+1]];
a[cmd[line][6]]=tmp;
}
inline void prepare()
{
for(int i=4; i<8; i++)
for(int j=0; j<7; j++)
cmd[i][j]=cmd[opp[i]][6-j];
}
inline int check()
{
for(int i=1; i<8; i++)
if(a[center[0]]!=a[center[i]]) return 0;
return 1;
}
int solve(int d,int maxd)
{
if(check())
{
ans[d]='\0';
printf("%s\n",ans);
return 1;
}
if(d+f()>maxd) return 0;
for(int i=0; i<8; i++)
{
ans[d]=i+'A';
Move(i);
if(solve(d+1,maxd)) return 1;
Move(opp[i]);
}
return 0;
}
int main()
{
#ifdef debug
freopen("in.in","r",stdin);
#endif // debug
prepare();
int x;
while(scanf("%d",&x)!=EOF&&(x!=0))
{
a[0]=x;
for(int i=1; i<24; i++)
scanf("%d",&a[i]);
if(check()) printf("No moves needed\n");
else
{
for(int maxd=1;; maxd++)
if(solve(0,maxd)) break;
}
printf("%d\n",a[6]);
}
return 0;
}
题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4089
题解:要求中间八个数字相同,即8个1或8个2或8个3,所以可以分三次求解,当所求为8个1时,2与3相同(都为非1)。A与F,B与E等操作相反,设置一反向的数组,可简化代码。