Codeforces 887C:Solution for Cube(模拟)

题目链接

Solution for Cube

Time limit:2 seconds Memory limit:256 megabytes


Problem Description

During the breaks between competitions, top-model Izabella tries to develop herself and not to be bored. For example, now she tries to solve Rubik’s cube 2x2x2.

It’s too hard to learn to solve Rubik’s cube instantly, so she learns to understand if it’s possible to solve the cube in some state using 90-degrees rotation of one face of the cube in any direction.

To check her answers she wants to use a program which will for some state of cube tell if it’s possible to solve it using one rotation, described above.

Cube is called solved if for each face of cube all squares on it has the same color.

https://en.wikipedia.org/wiki/Rubik‘s_Cube

Input

In first line given a sequence of 24 integers ai (1 ≤ ai ≤ 6), where ai denotes color of i-th square. There are exactly 4 occurrences of all colors in this sequence.

Output

Print «YES» (without quotes) if it’s possible to solve cube using one rotation and «NO» (without quotes) otherwise.

Examples

input
2 5 4 6 1 3 6 2 5 5 1 2 3 5 3 1 1 2 4 6 6 4 3 4
output
NO

input
5 3 5 3 2 5 2 5 6 2 6 2 4 4 4 4 1 1 1 1 6 3 6 3
output
YES

Note

In first test case cube looks like this:
这里写图片描述
In second test case cube looks like this:
这里写图片描述
It’s possible to solve cube by rotating face with squares with numbers 13, 14, 15, 16.


题意:

一个二阶魔方,给出所有面的颜色,问能否旋转一次得到每面颜色单一的魔方

解题思路:

一开始去想判断两个对面的面颜色单一,再去用中间四面去旋转,,,写起来真的不知道咋写。
这里写图片描述
后来发现对于二阶魔方,只需要模拟三种方式的旋转,要能一次旋转得到解,只要向前,向后旋转一次再去判断每面的颜色是否单一
旋转的模拟,就是将这一次操作的面的元素全部向后移动两格

Code:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

int a[25];
int r[3][8]={{1,3,5,7,9,11,24,22},{13,14,5,6,17,18,21,22},{3,4,17,19,10,9,16,14}};

void rot(int k)//第k种方式旋转一次
{
    int a1=a[r[k][0]],a2=a[r[k][1]];
    for(int i=0;i<6;i++)
        a[r[k][i]]=a[r[k][i+2]];
    a[r[k][6]]=a1;
    a[r[k][7]]=a2;
}

//判断每面颜色是否单一
bool check()
{
    for(int i=0;i<6;i++)
    {
        for(int j=1;j<4;j++)
        {
            if(a[i*4+j]!=a[i*4+j+1])
                return false;
        }
    }
    return true;
}

int main()
{
    for(int i=1;i<=24;i++)
        cin>>a[i];
    for(int i=0;i<3;i++)
    {
        rot(i);//第一次旋转
        if(check())
        {
            cout<<"YES"<<endl;
            return 0;
        }
        rot(i);rot(i);//再做两次旋转,相当于往回转一次
        if(check())
        {
            cout<<"YES"<<endl;
            return 0;
        }
        rot(i);//最后一次旋转,回归原样
    }
    cout<<"NO"<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值