ZOJ 3736 Pocket Cube

2014-11-24 02:54:22 · 作者: · 浏览: 1


Pocket Cube

Time Limit: 2 Seconds Memory Limit: 65536 KB

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 thanN twist steps.

Index of each face is shown as below:

\

Input

There will be several test cases. In each test case, there will be 2 lines. One integer N (1 ≤ N ≤ 7) in the first line, then 24 integers Ci seperated by a sinle space in the second line. For index 0 ≤ i < 24, Ci is color of the corresponding face. We guarantee that the color arrangement is a valid state which can be achieved by doing a finite number of twist steps from an initial cube whose all 6 large cube faces are completed faces.

Output

For each test case, please output the maximum number of completed faces during no more than N twist step(s).

Sample Input

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

Sample Output

6
2

Author: FAN, Yuzhe;CHEN, Cong;GUAN, Yao
Contest: The 2013 ACM-ICPC Asia Changsha Regional Contest
#include 
  
   
#include 
   
     #include 
    
      using namespace std; int mf[24],ans=0; bool _ck(int a,int b,int c,int d) { if(mf[a]==mf[b]&&mf[a]==mf[c]&&mf[a]==mf[d]) return true; return false; } bool ck() { if(_ck(0,1,2,3)&&_ck(4,5,10,11)&&_ck(6,7,12,13)&&_ck(8,9,14,15)&&_ck(16,17,18,19)&&_ck(20,21,22,23)) return true; return false; } int count_ck() { return _ck(0,1,2,3)+_ck(4,5,10,11)+_ck(6,7,12,13)+_ck(8,9,14,15)+_ck(16,17,18,19)+_ck(20,21,22,23); } bool zhuan(int a,int b,int c,int d, int e,int f, int g,int h, int i,int j, int k,int l) { ///圈 int t=mf[a]; mf[a]=mf[d];mf[d]=mf[c];mf[c]=mf[b];mf[b]=t; ///环 int t1=mf[e],t2=mf[f]; mf[e]=mf[g];mf[f]=mf[h]; mf[g]=mf[i];mf[h]=mf[j]; mf[i]=mf[k];mf[j]=mf[l]; mf[k]=t1;mf[l]=t2; } void qian1() { zhuan(13,12,6,7,3,2,5,11,16,17,14,8); } void qian2() { zhuan(6,12,13,7,2,3,8,14,17,16,11,5); } void zhuo1() { zhuan(15,14,8,9,1,3,7,13,17,19,21,23); } void zhuo2() { zhuan(8,14,15,9,23,21,19,17,13,7,3,1); } void shang1() { zhuan(16,17,19,18,20,21,15,14,13,12,11,10); } void shang2() { zhuan(16,18,19,17,10,11,12,13,14,15,21,20); } void dfs(int k) { if(ans==6) return ; ans=max(ans,count_ck()); if(!k) return ; qian1(); dfs(k-1); qian2(); qian2(); dfs(k-1); qian1(); zhuo1(); dfs(k-1); zhuo2(); zhuo2(); dfs(k-1); zhuo1(); shang1(); dfs(k-1); shang2(); shang2(); dfs(k-1); shang1(); } int main() { int k; while(scanf(%d,&k)!=EOF) { for(int i=0;i<24;i++) scanf(%d,mf+i); ans=count_ck(); dfs(k); printf(%d ,ans); } return 0; }