Pocket Cube

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

题意:题中给出了魔方的展开图,每一面是由四个小方块构成,如图的a-x,T组测试数据,每组6行,每行4个数字,每个数字代表一种颜色,数字相同则颜色相同,24个数字展示出每一个小方块的颜色,魔方可以左右前后四种方式转动,问能否通过不大于1次转动使魔方复原即每个面颜色都一样。

思路:这个题就是模拟,枚举出所有满足条件的情况,上下两面颜色相同时可通过向左或向右转动一次恢复,左右两面相等时可通过向前或者向后转一次恢复,前后两面相等时可通过向左或向右转动一次恢复。枚举出这每一种情况的状态。如果满足任意一种,就输出yes否则输出no。代码如下:


#include<stdio.h>
#include<string.h>
#include <math.h>
#include<algorithm>
using namespace std;
int check(int a,int b,int c,int d)
{
    if(a==b&&b==c&&c==d)
        return 1;
    else
        return 0;
}
int main()
{ 
    int T,flag,i,j;   //b[i]为1表示这一面不相等
    int a[10][10],b[10];
    scanf("%d",&T);
    while(T--)
    {
        flag=0;
        memset(b,0,sizeof(b));
        for(i=1; i<=6; i++)
        {
            for(j=1; j<=4; j++)
            {
                scanf("%d",&a[i][j]);
                if(j>=2)
                {
                    if(a[i][j]!=a[i][j-1])
                        b[i]=1;   
                }
            }
        }
        if(check(a[1][1],a[1][2],a[1][3],a[1][4])&&check(a[2][1],a[2][2],a[2][3],a[2][4])&&check(a[3][1],a[3][2],a[3][3],a[3][4])&&check(a[4][1],a[4][2],a[4][3],a[4][4])&&check(a[5][1],a[5][2],a[5][3],a[5][4])&&check(a[5][1],a[5][2],a[5][3],a[5][4])) flag=1;
        if(b[1]==0&&b[3]==0&&check(a[2][1],a[2][2],a[5][1],a[5][3])&&check(a[6][1],a[6][3],a[2][3],a[2][4])&&check(a[5][2],a[5][4],a[4][1],a[4][2])&&check(a[4][3],a[4][4],a[6][4],a[6][2])) flag=1; //上下相等下方向左转
        if(b[1]==0&&b[3]==0&&check(a[4][1],a[4][2],a[6][1],a[6][3])&&check(a[4][3],a[4][4],a[5][1],a[5][3])&&check(a[5][2],a[5][4],a[2][3],a[2][4])&&check(a[2][1],a[2][2],a[6][4],a[6][2]))  flag=1;  //上下相等下方向右转
        if(b[5]==0&&b[6]==0&&check(a[1][2],a[1][4],a[2][1],a[2][3])&&check(a[2][2],a[2][4],a[3][1],a[3][3])&&check(a[3][2],a[3][4],a[4][3],a[4][1])&&check(a[4][4],a[4][2],a[1][3],a[1][1]))  flag=1;//左右相等向后转
        if(b[5]==0&&b[6]==0&&check(a[2][2],a[2][4],a[1][1],a[1][3])&&check(a[1][2],a[1][4],a[4][3],a[4][1])&&check(a[4][4],a[4][2],a[3][1],a[3][3])&&check(a[3][2],a[3][4],a[2][1],a[2][3])) flag=1;//左右相等向前转
        if(b[2]==0&&b[4]==0&&check(a[1][1],a[1][2],a[6][3],a[6][4])&&check(a[6][1],a[6][2],a[3][1],a[3][2])&&check(a[3][3],a[3][4],a[5][3],a[5][4])&&check(a[5][1],a[5][2],a[1][3],a[1][4]))  flag=1;//前后相等向左转
        if(b[2]==0&&b[4]==0&&check(a[1][1],a[1][2],a[5][3],a[5][4])&&check(a[5][1],a[5][2],a[3][1],a[3][2])&&check(a[3][3],a[3][4],a[6][3],a[6][4])&&check(a[6][1],a[6][2],a[1][3],a[1][4]))  flag=1;//前后相等向右转
        if( flag)
            printf("YES\n");
        else
           printf("NO\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Angeliaaa/article/details/81227093