HDU 5983 B - 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

题意:就是给你一个2*2的魔方,每个格子里有一个数,问你旋转魔方一次能否使魔方每个面的数字相同

分析:旋转魔方可以对三个面俩个方向进行旋转,总共六种,可以直接模拟6种情况,只要有一种情况符合,就输出YES,否则输出NO,需要注意的是翻转后对应情况,标号容易出错。

代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[10][10],b[10][10];
int check()
{
    for(int i=1;i<=6;i++)
    {
        for(int j=1;j<=4;j++)
        {
            if(a[i][j]!=a[i][1])
                return 0;
        }
    }
    return 1;
}
void he()
{
    for(int i=1;i<=6;i++)
        for(int j=1;j<=4;j++)
            a[i][j]=b[i][j];
}
int app()
{
    if(check())
        return 1;
    return 0;
}
int app1()
{
    he();
    a[1][1]=b[5][1];
    a[1][2]=b[5][2];
    a[6][1]=b[1][1];
    a[6][2]=b[1][2];
    a[3][3]=b[6][2];
    a[3][4]=b[6][1];
    a[5][1]=b[3][4];
    a[5][2]=b[3][3];
    if(check())
        return 1;
    return 0;
}
int app2()
{
    he();
    a[1][1]=b[6][1];
    a[1][2]=b[6][2];
    a[6][1]=b[3][4];
    a[6][2]=b[3][3];
    a[3][3]=b[5][2];
    a[3][4]=b[5][1];
    a[5][1]=b[1][1];
    a[5][2]=b[1][2];
    if(check())
        return 1;
    return 0;
}
int app3()
{
    he();
    a[1][1]=b[4][1];
    a[1][3]=b[4][3];
    a[2][1]=b[1][1];
    a[2][3]=b[1][3];
    a[3][1]=b[2][1];
    a[3][3]=b[2][3];
    a[4][1]=b[3][1];
    a[4][3]=b[3][3];
    if(check());
        return 1;
    return 0;
}
int app4()
{
    he();
    a[1][1]=b[2][1];
    a[1][3]=b[2][3];
    a[2][1]=b[3][1];
    a[2][3]=b[3][3];
    a[3][1]=b[4][1];
    a[3][3]=b[4][3];
    a[4][1]=b[1][1];
    a[4][3]=b[1][3];
    if(check())
        return 1;
    return 0;
}
int app5()
{
    he();
    a[5][2]=b[4][4];
    a[5][4]=b[4][3];
    a[2][1]=b[5][2];
    a[2][2]=b[5][4];
    a[6][1]=b[2][2];
    a[6][3]=b[2][1];
    a[4][3]=b[6][1];
    a[4][4]=b[6][3];
    if(check())
        return 1;
    return 0;
}
int app6()
{
    he();
    a[5][2]=b[2][1];
    a[5][4]=b[2][2];
    a[2][1]=b[6][3];
    a[2][2]=b[6][1];
    a[6][1]=b[4][3];
    a[6][3]=b[4][4];
    a[4][3]=b[5][4];
    a[4][4]=b[5][2];
    if(check())
        return 1;
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=1;i<=6;i++)
        {
            for(int j=1;j<=4;j++)
            {
                scanf("%d",&b[i][j]);
                a[i][j]=b[i][j];
            }
        }
        if(app()||app1()||app2()||app3()||app4()||app5()||app6())
            printf("YES\n");
        else
            printf("NO\n");
    }
}

猜你喜欢

转载自blog.csdn.net/Vace___yun/article/details/81227828