HDU5983Pocket Cube

Pocket Cube

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


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
 

解题思路:
自己叠了一个简易的立方体,就能看出魔方的每个面坐标变换的规则,直接模拟的,暂时没有什么好的方法。QAQ

源代码:
#include<iostream>
#include<string>
#include<set>
#include<cstdio>
#include<cstring>
using namespace std;
int ans[9][9];
void readdata() {
    memset(ans,-1,sizeof(ans));
    for(int i=1;i<=8;i++)
        for(int j=3;j<=4;j++)
            scanf("%d",&ans[i][j]);
    for(int i=1;i<=2;i++)
        for(int j=1;j<=2;j++)
            scanf("%d",&ans[i][j]);
    for(int i=1;i<=2;i++)
        for(int j=5;j<=6;j++)
            scanf("%d",&ans[i][j]);
}
/*是否已经是还原成功的模样*/
int check(int c[9][9]) {
    int i,j;
    i=1,j=3;if(c[i][j]!=c[i][j+1]||c[i][j]!=c[i+1][j]||c[i][j]!=c[i+1][j+1])return 0;
    i=3,j=3;if(c[i][j]!=c[i][j+1]||c[i][j]!=c[i+1][j]||c[i][j]!=c[i+1][j+1])return 0;
    i=5,j=3;if(c[i][j]!=c[i][j+1]||c[i][j]!=c[i+1][j]||c[i][j]!=c[i+1][j+1])return 0;
    i=7,j=3;if(c[i][j]!=c[i][j+1]||c[i][j]!=c[i+1][j]||c[i][j]!=c[i+1][j+1])return 0;
    i=1,j=1;if(c[i][j]!=c[i][j+1]||c[i][j]!=c[i+1][j]||c[i][j]!=c[i+1][j+1])return 0;
    i=1,j=5;if(c[i][j]!=c[i][j+1]||c[i][j]!=c[i+1][j]||c[i][j]!=c[i+1][j+1])return 0;
    return 1;
}
/*总共出现的数字超过了6个*/
int cnt() {
    set<int> s;s.clear();
    for(int i=1;i<=8;i++)
        for(int j=3;j<=4;j++)
            s.insert(ans[i][j]);
    for(int i=1;i<=2;i++)
        for(int j=1;j<=2;j++)
            s.insert(ans[i][j]);
    for(int i=1;i<=2;i++)
        for(int j=5;j<=6;j++)
            s.insert(ans[i][j]);
    return s.size();
}
/*检查竖列的两个方向*/
int linecheck() {
    int sum=0;
    int bns[9][9];
    for(int i=1;i<=8;i++)
        for(int j=1;j<=8;j++)
            bns[i][j]=ans[i][j];
    int temp1,temp2;
    /*up*/
    temp1=bns[1][3];
    temp2=bns[2][3];
    for(int i=1;i<=6;i++) {
        bns[i][3]=bns[i+2][3];
    }
    bns[7][3]=temp1;
    bns[8][3]=temp2;
    if(check(bns)) sum+=1;
    /*down*/
    for(int i=1;i<=8;i++)
        for(int j=1;j<=8;j++)
            bns[i][j]=ans[i][j];
    temp1=bns[7][3];
    temp2=bns[8][3];
    for(int i=8;i>=3;i--) {
        bns[i][3]=bns[i-2][3];
    }
    bns[1][3]=temp1;
    bns[2][3]=temp2;
    if(check(bns)) sum+=1;
    return sum;
}
/*检查横向的两个方向*/
int rowcheck() {
    int sum=0;
    int bns[9][9];
    for(int i=1;i<=8;i++)
        for(int j=1;j<=8;j++)
            bns[i][j]=ans[i][j];
    int temp1,temp2;
    /*right*/
    temp1=bns[6][3];
    temp2=bns[6][4];
    bns[6][3]=bns[1][6];
    bns[6][4]=bns[1][5];
    for(int i=6;i>=3;i--)
        bns[1][i]=bns[1][i-2];
    bns[1][1]=temp2;
    bns[1][2]=temp1;
    if(check(bns)) sum+=1;
    /*left*/
    for(int i=1;i<=8;i++)
        for(int j=1;j<=8;j++)
            bns[i][j]=ans[i][j];
    temp1=bns[6][3];
    temp2=bns[6][4];
    bns[6][3]=bns[1][2];
    bns[6][4]=bns[1][1];
    for(int i=1;i<=4;i++)
        bns[1][i]=bns[1][i+2];
    bns[1][5]=temp2;
    bns[1][6]=temp1;
    if(check(bns)) sum+=1;
    return sum;
}
int exdir() {
    int sum=0;
    int bns[9][9];
    for(int i=1;i<=8;i++)
        for(int j=1;j<=8;j++)
            bns[i][j]=ans[i][j];
    int temp1,temp2;
    /*dir-left*/
    temp1=bns[8][3];
    temp2=bns[8][4];
    bns[8][3]=bns[1][5];bns[8][4]=bns[2][5];
    bns[1][5]=bns[3][4];bns[2][5]=bns[3][3];
    bns[3][3]=bns[1][2];bns[3][4]=bns[2][2];
    bns[1][2]=temp2;bns[2][2]=temp1;
    if(check(bns)) sum+=1;
    /*dir-right*/
    for(int i=1;i<=8;i++)
        for(int j=1;j<=8;j++)
            bns[i][j]=ans[i][j];
    temp1=bns[8][3];
    temp2=bns[8][4];
    bns[8][3]=bns[2][2];bns[8][4]=bns[1][2];
    bns[1][2]=bns[3][3];bns[2][2]=bns[3][4];
    bns[3][3]=bns[2][5];bns[3][4]=bns[1][5];
    bns[1][5]=temp1;bns[2][5]=temp2;
    if(check(bns)) sum+=1;
    return sum;
}
int main() {
    int t;scanf("%d",&t);
    while(t--) {
        int flag=0;
        readdata();
        /*总共出现的数字超过了6个*/
        if(cnt()!=6) {
            printf("NO
");continue;
        }
        /*是否已经是还原成功的模样*/
        if(check(ans)==1) {
            printf("YES
");continue;
        }
        /*检查竖列的两个方向*/
        if(linecheck()) {
            printf("YES
");continue;
        }
        /*检查横向的两个方向*/
        if(rowcheck()) {
            printf("YES
");continue;
        }
        /*ex-dir另外两个方向*/
        if(exdir()) {
            printf("YES
");continue;
        }
        /*上面所有情况都不符合*/
        if(!flag)
            printf("NO
");
    }
    return 0;
}


原文地址:https://www.cnblogs.com/lemonbiscuit/p/7776047.html