1. 程式人生 > >hdu 5983 Pocket Cube(模擬)

hdu 5983 Pocket Cube(模擬)

Pocket Cube

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2580    Accepted Submission(s): 955


 

Problem Description

The Pocket Cube, also known as the Mini Cube or the Ice Cube, is the 2 × 2 × 2 equivalence of a Rubik’s Cube.
The cube consists of 8 pieces, all corners.
Each piece is labeled by a three dimensional coordinate (h, k, l) where h, k, l ∈ {0, 1}. Each of the six faces owns four small faces filled with a positive integer.
For each step, you can choose a certain face and turn the face ninety degrees clockwise or counterclockwise.
You should judge that if one can restore the pocket cube in one step. We say a pocket cube has been restored if each face owns four same integers.

 

 

Input

The first line of input contains one integer N(N ≤ 30) which is the number of test cases.
For each test case, the first line describes the top face of the pocket cube, which is the common 2 × 2 face of pieces
labelled by (0, 0, 1),(0, 1, 1),(1, 0, 1),(1, 1, 1). Four integers are given corresponding to the above pieces.
The second line describes the front face, the common face of (1, 0, 1),(1, 1, 1),(1, 0, 0),(1, 1, 0). Four integers are
given corresponding to the above pieces.
The third line describes the bottom face, the common face of (1, 0, 0),(1, 1, 0),(0, 0, 0),(0, 1, 0). Four integers are
given corresponding to the above pieces.
The fourth line describes the back face, the common face of (0, 0, 0),(0, 1, 0),(0, 0, 1),(0, 1, 1). Four integers are
given corresponding to the above pieces.
The fifth line describes the left face, the common face of (0, 0, 0),(0, 0, 1),(1, 0, 0),(1, 0, 1). Four integers are given
corresponding to the above pieces.
The six line describes the right face, the common face of (0, 1, 1),(0, 1, 0),(1, 1, 1),(1, 1, 0). Four integers are given
corresponding to the above pieces.
In other words, each test case contains 24 integers a, b, c to x. You can flat the surface to get the surface development
as follows.
+ - + - + - + - + - + - +
| q | r | a | b | u | v |
+ - + - + - + - + - + - +
| s | t | c | d | w | x |
+ - + - + - + - + - + - +
        | e | f |
        + - + - +
        | g | h |
        + - + - +
        | i | j |
        + - + - +
        | k | l |
        + - + - +
        | m | n |
        + - + - +
        | o | p |
        + - + - +

 

 

Output

For each test case, output YES if can be restored in one step, otherwise output NO.

 

 

Sample Input

 

4 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 6 6 6 6 1 1 1 1 2 2 2 2 3 3 3 3 5 5 5 5 4 4 4 4 1 4 1 4 2 1 2 1 3 2 3 2 4 3 4 3 5 5 5 5 6 6 6 6 1 3 1 3 2 4 2 4 3 1 3 1 4 2 4 2 5 5 5 5 6 6 6 6

 

 

Sample Output

 

YES YES YES NO

 

 

題意:有一個2*2的魔方,如果我們能夠最多一步能夠將魔方還原成每一面都是同一種顏色,就輸出Yes,否則輸出No。(注意:魔方有6個面,必須有且只有6種顏色)

思路:因為資料的範圍很小,我們可以直接去模擬這個過程(草稿紙上也很好畫)。滿足條件的情況有7種,

①已經是還原的情況(不需要轉動)(6個面的顏色分別為,top:藍色,front:紅色,bottom:灰色,back:紫色,left:綠色,right:黃色)

②由已經還原的情況,沿x軸順時針轉動1次

 

③由已經還原的情況,沿x軸逆時針轉動1次

④由已經還原的情況,沿y軸順時針轉動1次

⑤由已經還原的情況,沿y軸逆時針轉動1次

⑥由已經還原的情況,沿z軸順時針轉動1次

⑦由已經還原的情況,沿z軸逆時針轉動1次

只要滿足其中一種就可以還原,否則不能還原。

每次去判斷相應位置的顏色是否相同。可以將立體的正方體像題中那樣展開,每個位置的對應關係更加的明瞭。

AC程式碼:

#include <cstdio>
#include <set>
using namespace std;
set<int> s;
void myInput(int a[]){
    for(int i=1;i<=4;i++){
        scanf("%d",&a[i]);
        s.insert(a[i]);
    }
}
//ok()函式表示該面的4個顏色相同
int ok(int a[]){
    if(a[1]==a[2]&&a[3]==a[4] && a[2]==a[3]){
        return 1;
    }
    return 0;
}

int top[5],front[5],bottom[5],back[5],left[5],right[5];

//沿x軸轉動
int judge1(){
    if(ok(left) && ok(right)){
        return top[1]==top[3] && top[3]==front[2] && front[2]==front[4]&&
        front[1]==front[3]  && front[3]==bottom[2] && bottom[2]==bottom[4]&&
        bottom[1]==bottom[3] && bottom[3]==back[2] && back[2]==back[4]&&
        back[1]==back[3]  && back[3]==top[2] && top[2]==top[4];
    }
    return 0;
}
//沿x軸轉動
int judge2(){
    if(ok(left) && ok(right)){
        return top[2]==top[4] && top[4]==front[1] &&front[1]==front[3]&&
        front[2]==front[4] && front[4]==bottom[1] && bottom[1]==bottom[3]&&
        bottom[2]==bottom[4] && bottom[4]==back[1] &&back[1]==back[3]&&
        back[2]==back[4] && back[4]==top[1] && top[1]==top[3];
    }
    return 0;
}
//沿y軸轉動
int judge3(){
    if(ok(top) && ok(bottom)){
        return left[1]==left[3] && left[3]==back[3] && back[3]==back[4]&&
        left[2]==left[4] && left[4]==front[3] && front[3]==front[4]&&
        right[1]==right[3] && right[3]==back[1] &&  back[1]==back[2]&&
        right[2]==right[4] && right[4]==front[1] &&front[1]==front[2];
    }
    return 0;
}
//沿y軸轉動
int judge4(){
    if(ok(top) && ok(bottom)){
        return left[2]==left[4] && left[4]==back[1] && back[1]==back[2]&&
        left[1]==left[3] && left[3]==front[1] && front[1]==front[2]&&
        right[2]==right[4] && right[4]==back[3] && back[3]==back[4]&&
        right[1]==right[3] && right[3]==front[3] && front[3]==front[4];
    }
    return 0;
}
//沿z軸轉動
int judge5(){
    if(ok(front) && ok(back)){
        return left[1]==left[2] && left[2]==bottom[1] && bottom[1]==bottom[2]&&
        left[3]==left[4] && left[4]==top[1] && top[1]==top[2]&&
        top[3]==top[4] && top[4]==right[1] && right[1]==right[2]&&
        right[3]==right[4] && right[4]==bottom[3] && bottom[3]==bottom[4];
    }
    return 0;
}
//沿z軸轉動
int judge6(){
    if(ok(front) && ok(back)){
        return left[3]==left[4] && left[4]==bottom[3] && bottom[3]==bottom[4]&&
        left[1]==left[2] && left[2]==top[3] && top[3]==top[4]&&
        top[1]==top[2] && top[2]==right[3] && right[3]==right[4]&&
        right[1]==right[2] && right[2]==bottom[1] && bottom[1]==bottom[2];
    }
    return 0;
}
//不需要轉動
int judge7(){
    if(ok(left) && ok(right) && ok(top)&& ok(bottom) && ok(front) && ok(back)){
        return 1;
    }
    return 0;
}


int main(){
    int T;scanf("%d",&T);
    while(T--){
        s.clear();
        myInput(top);
        myInput(front);
        myInput(bottom);
        myInput(back);
        myInput(left);
        myInput(right);
        //魔方有且只有6個顏色
        if(s.size()!=6){
            printf("NO\n");
            continue;
        }
        //滿足7種情況的一種
        if(judge7()||judge1()||judge2()||judge3()||judge4()||judge5()||judge6()){
            printf("YES\n");
        }else{
            printf("NO\n");
        }
    }
}