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");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Rainbow_storm/article/details/82965451