HDU 5983-Pocket Cube(一步还原二阶魔方)

22 篇文章 0 订阅

Pocket Cube

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


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
 

Recommend
jiangzijing2015   |   We have carefully selected several similar problems for you:   5994  5993  5992  5991  5990 

题目意思:

二阶魔方,六个面用六种颜色,是否能转动最多一次就还原。

测试数据的24个数字对应展开面的a~x,同一面的四个数字相同即说明还原成功。


解题思路:

注意要用六种不同颜色。

有六种旋转的方式,每两种为一组,若对面标号0~5,则本来就同色的分成4 5/1 3/0 2这三个组,剩下四个面转动后还原。

除去本来就同色,可以一步还原的情况是:①a b c d →b c d a  ②a b c d→d a b c

正好相差一位,所以我直接用两个数组保存后判断了。

代码最后是自己写的测试数据,对应代码中枚举的7种情况。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std;
set<int> se;
#define maxn 102
int s[6][4];
int a[4],b[4];
bool f()
{
    memset(a,0,sizeof(0));
    memset(b,0,sizeof(0));
    /*必须是六种颜色,利用set不存储相同数字的特性*/
    /* set<int>::iterator it;
     for(it=se.begin();it!=se.end();it++)
         cout<<*it<<" ";
     cout<<se.size()<<endl;*/
    if(se.size()!=6) return false;
    /*六面各自同色*/
    bool flag=true;
    int x1=s[1][0],x2=s[2][0],x3=s[3][0],x4=s[4][0],x5=s[5][0],x0=s[0][0];
    for(int i=1; i<4; ++i)
        if((s[1][i]!=x1)||(s[2][i]!=x2)||(s[3][i]!=x3)||(s[4][i]!=x4)||(s[5][i]!=x5)||(s[0][i]!=x0))
            flag=false;
    if(flag) return true;
    /*45侧面同色*/
    flag=true;
    int x=s[4][0],y=s[5][0];
    for(int i=1; i<4; ++i)
        if((s[4][i]!=x)||(s[5][i]!=y))
            flag=false;
    if(flag)
    {
        a[0]=s[0][0],a[1]=s[1][0],a[2]=s[2][0],a[3]=s[3][0];
        b[0]=s[0][1],b[1]=s[1][1],b[2]=s[2][1],b[3]=s[3][1];
        /* for(int i=0; i<4; ++i) cout<<a[i]<<" ";
                 cout<<endl;
                 for(int i=0; i<4; ++i) cout<<b[i]<<" ";
                 cout<<endl;*/
        bool q=true;
        for(int i=0; i<4; ++i)
            if(a[i]!=b[(i+1)%4]) q=false;
        if(q)return true;

        q=true;
        for(int i=0; i<4; ++i)
        {
            if(i==0)
            {
                if(a[i]!=b[3])q=false;
            }
            else
            {
                if(a[i]!=b[(i-1)]) q=false;
            }
        }
        if(q)return true;
    }
    /*13侧面同色*/
    flag=true;
    x=s[1][0],y=s[3][0];
    for(int i=1; i<4; ++i)
        if((s[1][i]!=x)||(s[3][i]!=y))
            flag=false;
    if(flag)
    {
        a[0]=s[0][0],a[1]=s[4][0],a[2]=s[2][2],a[3]=s[5][0];
        b[0]=s[0][2],b[1]=s[4][2],b[2]=s[2][0],b[3]=s[5][2];
        bool q=true;
        for(int i=0; i<4; ++i)
            if(a[i]!=b[(i+1)%4])q=false;
        if(q) return true;
        q=true;
        for(int i=0; i<4; ++i)
        {
            if(i==0)
            {
                if(a[i]!=b[3])q=false;
            }
            else
            {
                if(a[i]!=b[(i-1)]) q=false;
            }
        }
        if(q) return true;
    }
    /*02侧面同色*/
    flag=true;
    x=s[0][0],y=s[2][0];
    for(int i=1; i<4; ++i)
        if((s[0][i]!=x)||(s[2][i]!=y))
            flag=false;
    if(flag)
    {
        a[0]=s[1][0],a[1]=s[4][1],a[2]=s[3][2],a[3]=s[5][0];
        b[0]=s[1][2],b[1]=s[4][0],b[2]=s[3][0],b[3]=s[5][1];

        bool q=true;
        for(int i=0; i<4; ++i)
            if(a[i]!=b[(i+1)%4])q=false;
        if(q) return true;
        q=true;
        for(int i=0; i<4; ++i)
        {
            if(i==0)
            {
                if(a[i]!=b[3])q=false;
            }
            else
            {
                if(a[i]!=b[(i-1)]) q=false;
            }
        }
        if(q) return true;
    }
    return false;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        se.clear();
        for(int i=0; i<6; ++i)
            for(int j=0; j<4; ++j)
            {
                scanf("%d",&s[i][j]);
                se.insert(s[i][j]);
            }
        bool t=f();
        if(t) puts("YES");
        else puts("NO");
    }
    return 0;
}
/*
7
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6


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 2 1 2
2 3 2 3
3 4 3 4
4 1 4 1
5 5 5 5
6 6 6 6


1 1 2 2
5 5 5 5
4 4 3 3
6 6 6 6
2 2 3 3
4 4 1 1


1 1 4 4
5 5 5 5
2 2 3 3
6 6 6 6
2 2 1 1
4 4 3 3


5 5 5 5
2 2 1 1
6 6 6 6
3 3 4 4
2 3 2 3
1 4 1 4


5 5 5 5
2 2 3 3
6 6 6 6
1 1 4 4
4 3 4 3
1 2 1 2
*/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值