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.
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.
Print «YES» (without quotes) if it's possible to solve cube using one rotation and «NO» (without quotes) otherwise.
2 5 4 6 1 3 6 2 5 5 1 2 3 5 3 1 1 2 4 6 6 4 3 4
NO
5 3 5 3 2 5 2 5 6 2 6 2 4 4 4 4 1 1 1 1 6 3 6 3
YES
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.
题意:由数字1~6分别代表一种颜色,问能否通过旋转魔方一次就能将魔方还原。
思路:暴力即可,因为是二阶魔方,所以旋转方式一共有六种。每种旋转都尝试一次,旋转完之后看是否魔方被还原即可。用二维数组储存魔方上的颜色,用重新给数组赋值的方式模拟旋转过程。
#include <cstdio>
#include <cmath>
#include <set>
#include <cstring>
#include <algorithm>
using namespace std;
int a[10][10],b[10][10];
int i,j;
bool check()
{
for(i=1;i<=6;i++)
for(j=1;j<=3;j++)
{
if(b[i][j]!=b[i][j+1])
return false;
}
return true;
}
void copy_cube()
{
for(i=1;i<=6;i++)
for(j=1;j<=4;j++)
b[i][j]=a[i][j];
}
int main()
{
int x,y;
for(i=1;i<=6;i++)
for(j=1;j<=4;j++)
scanf("%d",&a[i][j]);
copy_cube();
x=b[1][2],y=b[1][4];
b[1][2]=b[6][3],b[1][4]=b[6][1];
b[6][3]=b[3][2],b[6][1]=b[3][4];
b[3][2]=b[2][2],b[3][4]=b[2][4];
b[2][2]=x,b[2][4]=y;
if(check())
{
printf("YES\n");
return 0;
}
copy_cube();
x=b[1][2],y=b[1][4];
b[1][2]=b[2][2],b[1][4]=b[2][4];
b[2][2]=b[3][2],b[2][4]=b[3][4];
b[3][2]=b[6][3],b[3][4]=b[6][1];
b[6][3]=x,b[6][1]=y;
if(check())
{
printf("YES\n");
return 0;
}
copy_cube();
x=b[2][1],y=b[2][2];
b[2][1]=b[5][1],b[2][2]=b[5][2];
b[5][1]=b[6][1],b[5][2]=b[6][2];
b[6][1]=b[4][1],b[6][2]=b[4][2];
b[4][1]=x,b[4][2]=y;
if(check())
{
printf("YES\n");
return 0;
}
copy_cube();
x=b[2][1],y=b[2][2];
b[2][1]=b[4][1],b[2][2]=b[4][2];
b[4][1]=b[6][1],b[4][2]=b[6][2];
b[6][1]=b[5][1],b[6][2]=b[5][2];
b[5][1]=x,b[5][2]=y;
if(check())
{
printf("YES\n");
return 0;
}
copy_cube();
x=b[1][3],y=b[1][4];
b[1][3]=b[4][4],b[1][4]=b[4][2];
b[4][4]=b[3][2],b[4][2]=b[3][1];
b[3][2]=b[5][1],b[3][1]=b[5][3];
b[5][1]=x,b[5][3]=y;
if(check())
{
printf("YES\n");
return 0;
}
copy_cube();
x=b[1][3],y=b[1][4];
b[1][3]=b[5][1],b[1][4]=b[5][3];
b[5][1]=b[3][2],b[5][3]=b[3][1];
b[3][2]=b[4][4],b[3][1]=b[4][2];
b[4][4]=x,b[4][2]=y;
if(check())
{
printf("YES\n");
return 0;
}
printf("NO\n");
return 0;
}