Codeforces Round #444 (Div. 2)-C-Solution for Cube(魔方一步还原)

来源:互联网 发布:搜狗输入法云计算进程 编辑:程序博客网 时间:2024/05/18 00:18
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
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.


题意:给你一个魔方,让你可以操作一次,选择一个位置,按照魔方的操作顺时针或者逆时针旋转90度,问你操邹一次后能否将魔方的每一面都是相同的颜色。

题解:我们可以将移动的位置所能影响到的所有位置放在二维数组的同一行中,这样我们可以方便的进行模拟操作,然后判断合法性即可。

附上惨图:(为什么我补的cf都挂题?)


#include<stdio.h>int n,a[25],b[25];int r[6][8] ={{1, 3, 5, 7, 9, 11, 24, 22},{2, 4, 6, 8, 10, 12, 23, 21},{13, 14, 5, 6, 17, 18, 21, 22},{15, 16, 7, 8, 19, 20, 23, 24},{3, 4, 17, 19, 10, 9, 16, 14},{1, 2, 18, 20, 12, 11, 15, 13}};bool solve(int id,int k){for(int i=0;i<24;i++)b[i]=a[i];if(k==0){for(int i=0;i<8;i++)b[r[id][i]-1]=a[r[id][(i+2)%8]-1];}else{for(int i=0;i<8;i++)b[r[id][(i+2)%8]-1]=a[r[id][i]-1];}for(int i=0;i<24;i+=4)if(b[i]!=b[i+1] || b[i]!=b[i+1] || b[i]!=b[i+2] || b[i]!=b[i+3])return 0;return 1;}int main(void){for(int i=0;i<24;i++)scanf("%d",&a[i]);for(int i=0;i<6;i++)for(int j=0;j<6;j++)if(solve(i,j)){printf("YES\n");return 0;}printf("NO\n");return 0;}


原创粉丝点击