Week10-B-Turn the Rubik's Cube

topic

Dongdong has a second-order Rubik's Cube, which is a cube group of 2×2×2. The cube consists of eight corners.
Each piece of the Rubik's cube is marked with three-dimensional coordinates (h, k, l), where h, k, l∈{0,1}. Each of the six faces has four facets, and each facet has a positive integer.
For each step, Dongdong can select a specific face and turn this face 90 degrees clockwise or counterclockwise.
Please judge whether Dongdong can restore this Rubik's Cube in one step (without different colors on each side).

Input

The first line of input contains an integer N (N≤30), which is the number of test cases.

For each test case, the
first to fourth numbers describe the top surface of the Rubik's Cube, which is a common 2×2 surface, consisting of (0, 0, 1), (0, 1, 1), (1, 0, 1 ), (1, 1, 1) mark. The four integers correspond to the above parts.

The 5th to 8th numbers describe the front, that is, the common surface of (1, 0, 1), (1, 1, 1), (1, 0, 0), (1, 1, 0). The four integers
correspond to the parts above.

The 9th to 12th numbers describe the bottom surface, that is, the common surface of (1, 0, 0), (1, 1, 0), (0, 0, 0), (0, 1, 0). The four integers correspond to the above parts.

The 13th to 16th numbers describe the back side, which is the common surface of (0,0,0), (0,1,0), (0,0,1), (0,1), (0,1,1) . The four integers correspond to the above parts.

The 17th to 20th numbers describe the left side, that is, the common side of (0, 0, 0), (0, 0, 1), (1, 0, 0), (1, 0, 1). Give four integers corresponding to the above parts.

The 21st to 24th numbers describe the right side, that is, the common side of (0, 1, 1), (0, 1, 0), (1, 1, 1), (1, 1, 0). Give four integers corresponding to the above parts.

In other words, each test case contains 24 integers a, b, c to x. You can unfold the surface to get a floor plan

As follows.
Insert picture description here

Output

For each test case, if the Rubik's Cube can be restored by "just one step" at most, it outputs YES and then NO.

Friendly reminder: If you can think about the design of the problem-solving framework is the best, it will be very painful when it comes up.

Friendly reminder: If you can think about the design of the problem-solving framework is the best, it will be very painful when it comes up.

Friendly reminder: If you can think about the design of the problem-solving framework is the best, it will be very painful when it comes up.

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

Ideas

Enter the Rubik's Cube into the array a, and then call the methods tops, topn, fronts, frontn, lefts, and leftn to imitate the counterclockwise & clockwise rotation of the top, front, and left sides. For the new array obtained by rotation, call the pan function to determine whether The six faces are all the same number. After any one rotation, if the numbers on each face are the same, output YES, otherwise output NO

error

1. Note that the condition for the restoration of the title is that the numbers on each side are the same, and it does not say that the numbers on the six sides are different orz

Code

#include<stdio.h>
using namespace std;
int a[25];
bool pan(int *b)
{
    
    
 int cnt=0;
 for(int i=0;i<6;i++)
  if((b[0+4*i]==b[1+4*i])&&(b[1+4*i]==b[2+4*i])&&(b[2+4*i]==b[3+4*i]))
   cnt++;
 if(cnt==6) return true;
 return false;
}
bool tops(){
    
    
 int b[25];
 for(int i=0;i<24;i++) b[i]=a[i];
 int a1=b[4],a2=b[5];//前变成右
 b[4]=b[22];b[5]=b[20];
 b[22]=b[15];b[20]=b[14];//右变成后
 b[15]=b[17];b[14]=b[19];//后变左
 b[17]=a1;b[19]=a2;//左变前
 return pan(b);
}
bool topn(){
    
    
 int b[25];
 for(int i=0;i<24;i++) b[i]=a[i];
 int a1=b[4],a2=b[5];b[4]=b[17];b[5]=b[19];b[17]=b[15];b[19]=b[14];b[15]=b[22];b[14]=b[20];b[22]=a1;b[20]=a2;
 return pan(b);
}
bool frontn(){
    
    
 int b[25];
 for(int i=0;i<24;i++) b[i]=a[i];
 int a1=b[8],a2=b[9];b[8]=b[19];b[9]=b[18];b[19]=b[3];b[18]=b[2];b[3]=b[23];b[2]=b[22];b[23]=a1;b[22]=a2;
 return pan(b);
}
bool fronts(){
    
    
 int b[25];
 for(int i=0;i<24;i++) b[i]=a[i];
 int a1=b[8],a2=b[9];b[8]=b[23];b[23]=b[3];b[3]=b[19];b[9]=b[22];b[22]=b[2];b[2]=b[18];b[19]=a1;b[18]=a2;
 return pan(b);
}
bool lefts(){
    
    
 int b[25];
 for(int i=0;i<24;i++) b[i]=a[i];
 int a1=b[0],a2=b[2];b[0]=b[4];b[4]=b[8];b[8]=b[12];b[2]=b[6];b[6]=b[10];b[10]=b[14];b[12]=a1;b[14]=a2;
 return pan(b);
}
bool leftn(){
    
    
 int b[25];
 for(int i=0;i<24;i++) b[i]=a[i];
 int a1=b[0],a2=b[2];b[0]=b[12];b[12]=b[8];b[8]=b[4];b[2]=b[14];b[14]=b[10];b[10]=b[6];b[4]=a1;b[6]=a2;
 return pan(b);
}
int main()
{
    
    
 int N;
 scanf("%d",&N);
 while(N--){
    
    
  for(int i=0;i<24;i++)
   scanf("%d",&a[i]);
  //不移动是否可以
  if(pan(a)==true) {
    
    
   printf("YES\n");
   continue;
  }
  bool flag[6],li=false;
  flag[0]=tops();flag[1]=topn();
  flag[2]=fronts();flag[3]=frontn();
  flag[4]=lefts();flag[5]=leftn();
  for(int i=0;i<6;i++){
    
    
   if(flag[i]==true){
    
    
    li=true;
    printf("YES\n");
    break;
   }
  }
  if(li==false)
   printf("NO\n");
 }
}

Guess you like

Origin blog.csdn.net/alicemh/article/details/105691921