HDU 5983 Pocket Cube (暴力)

Pocket Cube

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


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
 

Source


【思路】

2阶魔方共有6种转法,分别按上、左、前三个面的顺时针、逆时针来转。直接暴力枚举转法,注意每一次转前初始化。


【代码】

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAXN=25;

int t;
int origin[MAXN],now[MAXN],trans[MAXN];

bool check()
{
    for(int i=1;i<=21;i+=4)
        for(int j=i+1;j<=i+3;j++)
            if(now[j]!=now[i])return false;
    return true;
}

void init()
{
    for(int i=1;i<=24;i++)
        now[i]=origin[i];
}

void trans_clock()
{
    int tmp,tmp1,tmp2;
    tmp=now[trans[1]];
    now[trans[1]]=now[trans[2]];now[trans[2]]=now[trans[3]];
    now[trans[3]]=now[trans[4]];now[trans[4]]=tmp;
    tmp1=now[trans[5]];tmp2=now[trans[6]];
    now[trans[5]]=now[trans[7]];now[trans[6]]=now[trans[8]];
    now[trans[7]]=now[trans[9]];now[trans[8]]=now[trans[10]];
    now[trans[9]]=now[trans[11]];now[trans[10]]=now[trans[12]];
    now[trans[11]]=tmp1;now[trans[12]]=tmp2;
}

void trans_counterclock()
{
    int tmp,tmp1,tmp2;
    tmp=now[trans[4]];
    now[trans[4]]=now[trans[3]];now[trans[3]]=now[trans[2]];
    now[trans[2]]=now[trans[1]];now[trans[1]]=tmp;
    tmp1=now[trans[11]];tmp2=now[trans[12]];
    now[trans[11]]=now[trans[9]];now[trans[12]]=now[trans[10]];
    now[trans[9]]=now[trans[7]];now[trans[10]]=now[trans[8]];
    now[trans[7]]=now[trans[5]];now[trans[8]]=now[trans[6]];
    now[trans[5]]=tmp1;now[trans[6]]=tmp2;
}

bool up()
{
    trans[1]=1;trans[2]=3;trans[3]=4;trans[4]=2;
    trans[5]=5;trans[6]=6;
    trans[7]=23;trans[8]=21;
    trans[9]=16;trans[10]=15;
    trans[11]=18;trans[12]=20;
    bool flag=false;
    init();trans_clock();if(check())flag=true;
    init();trans_counterclock();if(check())flag=true;
    return flag;
}

bool left()
{
    trans[1]=17;trans[2]=19;trans[3]=20;trans[4]=18;
    trans[5]=7;trans[6]=5;
    trans[7]=3;trans[8]=1;
    trans[9]=15;trans[10]=13;
    trans[11]=11;trans[12]=9;
    bool flag=false;
    init();trans_clock();if(check())flag=true;
    init();trans_counterclock();if(check())flag=true;
    return flag;
}

bool front()
{
    trans[1]=5;trans[2]=7;trans[3]=8;trans[4]=6;
    trans[5]=9;trans[6]=10;
    trans[7]=24;trans[8]=23;
    trans[9]=4;trans[10]=3;
    trans[11]=20;trans[12]=19;
    bool flag=false;
    init();trans_clock();if(check())flag=true;
    init();trans_counterclock();if(check())flag=true;
    return flag;
}

int main()
{
    scanf("%d",&t);
    while(t--){
        for(int i=1;i<=24;i++)
            scanf("%d",&origin[i]);
        bool flag=false;
        init();if(check())flag=true;
        if(up())flag=true;
        if(left())flag=true;
        if(front())flag=true;
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值