Description
Initially, the blocks are placed on the board randomly. Your task is to move the blocks so that the eight blocks placed in the center square have the same symbol marked. There is only one type of valid move, which is to rotate one of the four lines, each consisting of seven blocks. That is, six blocks in the line are moved towards the head by one block and the head block is moved to the end of the line. The eight possible moves are marked with capital letters A to H. Figure 1 illustrates two consecutive moves, move A and move C from some initial configuration.
Input
Output
Sample Input
1 1 1 1 3 2 3 2 3 1 3 2 2 3 1 2 2 2 3 1 2 1 3 3 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 0
Sample Output
AC 2 DDHH 2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
#define N 30
#define M 1024
int a[N];
int pos[]={6,7,8,11,12,15,16,17};
int pd[]={0,1,2,3,5,4,7,6};
char ans[M];
int depth,num;
int check(int *s){
int cnt[4]={0,0,0,0};
for (int i=0;i<8;i++){
cnt[s[pos[i]]]++;
}
return max(cnt[1],max(cnt[2],cnt[3]));
}
void change(int *s,int a,int b,int c,int d,int e,int f,int g){
int temp=s[a];
s[a]=s[b];
s[b]=s[c];
s[c]=s[d];
s[d]=s[e];
s[e]=s[f];
s[f]=s[g];
s[g]=temp;
}
bool dfs(int *s,int now,int pre){
if (depth-now<8-check(s))
return false;
if (now>=depth)
return false;
int b[N];
for (int i=0;i<8;i++){
int w=pd[i];
if (abs(w-pre)==4)
continue;
memcpy(b,s,sizeof(b));
switch (w){
case 0:
ans[now]='A';
change(b,0,2,6,11,15,20,22);
break;
case 1:
ans[now]='B';
change(b,1,3,8,12,17,21,23);
break;
case 2:
ans[now]='C';
change(b,10,9,8,7,6,5,4);
break;
case 3:
ans[now]='D';
change(b,19,18,17,16,15,14,13);
break;
case 5:
ans[now]='E';
change(b,23,21,17,12,8,3,1);
break;
case 4:
ans[now]='F';
change(b,22,20,15,11,6,2,0);
break;
case 7:
ans[now]='G';
change(b,13,14,15,16,17,18,19);
break;
case 6:
ans[now]='H';
change(b,4,5,6,7,8,9,10);
break;
}
if (check(b)==8){
ans[now+1]=0;
num=b[6];
return true;
}
if (dfs(b,now+1,i))
return true;
}
return false;
}
int main(){
while (~scanf("%d",&a[0])){
if (a[0]==0)
break;
for (int i=1;i<24;i++)
scanf("%d",&a[i]);
if (check(a)==8){
printf("No moves needed\n");
printf("%d\n",a[6]);
}else{
depth=1;
while (1){
if (dfs(a,0,-100))
break;
depth++;
}
printf("%s\n",ans);
printf("%d\n",num);
}
}
return 0;
}