Pocket Cube is a 3-D combination puzzle. It is a 2 × 2 × 2 cube, which means it is constructed by 8 mini-cubes. For a combination of 2 × 2 mini-cubes which sharing a whole cube face, you can twist it 90 degrees in clockwise or counterclockwise direction, this twist operation is called one twist step.
Considering all faces of mini-cubes, there will be totally 24 faces painted in 6 different colors (Indexed from 0), and there will be exactly 4 faces painted in each kind of color. If 4 mini-cubes' faces of same color rely on same large cube face, we can call the large cube face as a completed face.
Now giving you an color arrangement of all 24 faces from a scrambled Pocket Cube, please tell us the maximum possible number of completed faces in no more than N twist steps.
Index of each face is shown as below:
1
0 0 0 0 1 1 2 2 3 3 1 1 2 2 3 3 4 4 4 4 5 5 5 5
1
0 4 0 4 1 1 2 5 3 3 1 1 2 5 3 3 4 0 4 0 5 2 5 2
6
2
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n,sum;
int a[25];
int turn[6][24] = {//六种旋转情况
{0,1,8,14,4,3,7,13,17,9,10,2,6,12,16,15,5,11,18,19,20,21,22,23},
{0,1,11,5,4,16,12,6,2,9,10,17,13,7,3,15,14,8,18,19,20,21,22,23},
{1,3,0,2,23,22,4,5,6,7,10,11,12,13,14,15,16,17,18,19,20,21,9,8},
{2,0,3,1,6,7,8,9,23,22,10,11,12,13,14,15,16,17,18,19,20,21,5,4},
{6,1,12,3,5,11,16,7,8,9,4,10,18,13,14,15,20,17,22,19,0,21,2,23},
{20,1,22,3,10,4,0,7,8,9,11,5,2,13,14,15,6,17,12,19,16,21,18,23},
};
int fun(int map[]){//判断有几个面一色
int sumx=0;
if(map[0]==map[1]&&map[1]==map[2]&&map[2]==map[3])sumx++;
if(map[6]==map[7]&&map[7]==map[12]&&map[12]==map[13])sumx++;
if(map[8]==map[9]&&map[9]==map[14]&&map[14]==map[15])sumx++;
if(map[4]==map[5]&&map[5]==map[10]&&map[10]==map[11])sumx++;
if(map[16]==map[17]&&map[17]==map[18]&&map[18]==map[19])sumx++;
if(map[20]==map[21]&&map[21]==map[22]&&map[22]==map[23])sumx++;
return sumx;
}
void dfs(int num,int c[]){
if(num==n)return ;
if(sum==6)return ;
int te[25];
for(int i=0;i<6;i++)
{
for(int j=0;j<24;j++)
{
te[j] = c[turn[i][j]];
}
sum=max(fun(te),sum);
dfs(num+1,te);
}
}
int main(){
while(~scanf("%d",&n)){
sum = 0;
for(int i=0;i<24;i++){
scanf("%d",&a[i]);
}
sum=max(fun(a),sum);
dfs(0,a);//传入第几次查找
printf("%d\n",sum);
}
return 0;
}