C. Solution for Cube
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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
Copy
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
Copy
NO
Input
Copy
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
Copy
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.
题目链接:http://codeforces.com/contest/887/problem/C
题意:给出六个面,每个面的四个方块的颜色,可以随机拼凑,问能否在转动且转动一次的情况下将魔方还原,直接模拟三种情况的旋转。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
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 turn(int 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++)
{
turn(i);
if(check())
{
puts("YES");
return 0;
}
turn(i);
turn(i); //相当于反方向的旋转一次
if(check())
{
puts("YES");
return 0;
}
turn(i); //复原
}
puts("NO");
return 0;
}