The 2013 ACM-ICPC Asia Changsha Regional Contest - K

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.

The 2013 ACM-ICPC Asia Changsha Regional Contest - K_第1张图片  The 2013 ACM-ICPC Asia Changsha Regional Contest - K_第2张图片  The 2013 ACM-ICPC Asia Changsha Regional Contest - K_第3张图片

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:

The 2013 ACM-ICPC Asia Changsha Regional Contest - K_第4张图片

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
一共是12种变化,但是有等价的,最后每一纬是2种变换,一共是6种变换。
#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <set>
#define Max(a,b) ((a)>(b)?(a):(b))
#pragma comment(linker, "/STACK:16777216")
using namespace std ;
typedef long long LL ;
struct Cube{
    int a[24] ;

    void out(){
         printf("  %d%d    \n",a[0],a[1]) ;
         printf("  %d%d    \n",a[2],a[3]) ;
         printf("%d%d%d%d%d%d\n",a[4],a[5],a[6],a[7],a[8],a[9]) ;
         printf("%d%d%d%d%d%d\n",a[10],a[11],a[12],a[13],a[14],a[15]) ;
         printf("  %d%d    \n",a[16],a[17]) ;
         printf("  %d%d    \n",a[18],a[19]) ;
         printf("  %d%d    \n",a[20],a[21]) ;
         printf("  %d%d    \n",a[22],a[23]) ;
         puts("") ;
    }

    int complete_face(){
        int sum = 0 ;
        if(a[0]==a[1]&&a[0]==a[2]&&a[0]==a[3])
           sum++ ;
        if(a[4]==a[5]&&a[4]==a[10]&&a[4]==a[11])
           sum++ ;
        if(a[6]==a[7]&&a[6]==a[12]&&a[6]==a[13])
           sum++ ;
        if(a[8]==a[9]&&a[8]==a[14]&&a[8]==a[15])
           sum++ ;
        if(a[16]==a[17]&&a[16]==a[18]&&a[16]==a[19])
           sum++ ;
        if(a[20]==a[21]&&a[20]==a[22]&&a[20]==a[23])
           sum++ ;
        return sum ;
    }

    Cube R_colock(){
        Cube o ;
        for(int i = 0 ;i < 24 ;i++)
            o.a[i] = a[i] ;
        o.a[1] = a[7] ;
        o.a[3] = a[13] ;
        o.a[7] = a[17] ;
        o.a[13] = a[19] ;
        o.a[17] = a[21] ;
        o.a[19] = a[23] ;
        o.a[21] = a[1] ;
        o.a[23] = a[3] ;
        o.a[8] = a[14] ;
        o.a[9] = a[8] ;
        o.a[14] = a[15] ;
        o.a[15] = a[9] ;
        return o ;
    }

    Cube R_count_colock(){
        Cube o ;
        for(int i = 0 ;i < 24 ;i++)
            o.a[i] = a[i] ;
        o.a[7]= a[1];
        o.a[13]= a[3];
        o.a[17]= a[7];
        o.a[19]= a[13] ;
        o.a[21]= a[17] ;
        o.a[23] =a[19];
        o.a[1] =a[21];
        o.a[3] =a[23];
        o.a[14] =a[8];
        o.a[8] =a[9];
        o.a[15] =a[14];
        o.a[9] =a[15];
        return o ;
    }

    Cube U_colock(){
        Cube o ;
        for(int i = 0 ;i < 24 ;i++)
            o.a[i] = a[i] ;
        o.a[5] = a[16] ;
        o.a[11] = a[17] ;
        o.a[16] = a[14] ;
        o.a[17] = a[8] ;
        o.a[14] = a[3] ;
        o.a[8] = a[2] ;
        o.a[3] = a[5] ;
        o.a[2] = a[11] ;
        o.a[6] = a[12] ;
        o.a[7] = a[6] ;
        o.a[13] = a[7] ;
        o.a[12] = a[13] ;
        return o ;
    }

    Cube U_count_colock(){
        Cube o ;
        for(int i = 0 ;i < 24 ;i++)
            o.a[i] = a[i] ;
        o.a[16]= a[5];
        o.a[17]= a[11];
        o.a[14]= a[16];
        o.a[8]= a[17] ;
        o.a[3]= a[14] ;
        o.a[2] =a[8];
        o.a[5] =a[3];
        o.a[11] =a[2];
        o.a[12] =a[6];
        o.a[6] =a[7];
        o.a[7] =a[13];
        o.a[13] =a[12];
        return o ;
    }

    Cube F_colock(){
        Cube o ;
        for(int i = 0 ;i < 24 ;i++)
            o.a[i] = a[i] ;
        o.a[4] = a[6] ;
        o.a[5] = a[7] ;
        o.a[6] = a[8] ;
        o.a[7] = a[9] ;
        o.a[8] = a[23] ;
        o.a[9] = a[22] ;
        o.a[23] = a[4] ;
        o.a[22] = a[5] ;
        o.a[0] = a[2] ;
        o.a[1] = a[0] ;
        o.a[2] = a[3] ;
        o.a[3] = a[1] ;
        return o ;
    }

    Cube F_count_colock(){
        Cube o ;
        for(int i = 0 ;i < 24 ;i++)
            o.a[i] = a[i] ;
        o.a[6]= a[4];
        o.a[7]= a[5];
        o.a[8]= a[6];
        o.a[9]= a[7] ;
        o.a[23]= a[8] ;
        o.a[22] =a[9];
        o.a[4] =a[23];
        o.a[5] =a[22];
        o.a[2] =a[0];
        o.a[0] =a[1];
        o.a[3] =a[2];
        o.a[1] =a[3];
        return o ;
    }

};

int N ;
int ans ;

void dfs(Cube cb ,int step){
     Cube o ;
     if(step>N)
        return  ;
     if(ans == 6)
        return  ;

     o = cb.F_colock() ;
     ans = Max(ans,o.complete_face()) ;
     dfs(o,step+1) ;

     o = cb.F_count_colock() ;
     ans = Max(ans,o.complete_face()) ;
     dfs(o,step+1) ;

     o = cb.R_colock() ;
     ans = Max(ans,o.complete_face()) ;
     dfs(o,step+1) ;

     o = cb.R_count_colock() ;
     ans = Max(ans,o.complete_face()) ;
     dfs(o,step+1) ;

     o = cb.U_colock();
     ans = Max(ans,o.complete_face()) ;
     dfs(o,step+1) ;

     o = cb.U_count_colock() ;
     ans = Max(ans,o.complete_face()) ;
     dfs(o,step+1) ;
}

int main(){
   Cube now ;
   while(scanf("%d",&N)!=EOF){
       for(int i = 0 ;i < 24 ;i++)
          scanf("%d",&now.a[i]) ;
       ans = now.complete_face() ;
       dfs(now,1) ;
       printf("%d\n",ans)  ;
   }
   return 0 ;
}

 

你可能感兴趣的:(The 2013 ACM-ICPC Asia Changsha Regional Contest - K)