大体题意:
给你一个2*2*2的魔方,问你能否一步到达各个面的颜色完全一样。
思路:
其实挺简单的,题意已经给足了提示,已经告诉你了魔方怎么进行标号。(就是那个图)
只要给魔方标号,怎么转就很简单了,可以预处理一个b 数组和c 数组,分别是魔方转一个面时候的侧面的八个面和上面的四个面,循环赋值即可。
这样 写好循环赋值函数后,转六个面只需要更改b数组和c数组即可。
详细见代码:
#include
#include
#include
using namespace std;
int a[25];
bool ok;
int ori[25];
void fuyuan(){
for (int i = 0; i < 24; ++i) a[i] = ori[i];
}
bool check(){
for (int i = 0; i < 24; i += 4){
for (int j = i; j < i+4; ++j){
if (a[j] != a[i]) return false;
}
}
return true;
}
int b[] = {14,15,20,22,5,4,19,17};
int c[] = {2,3,1,0};
void zhuan(){
int t1 = a[b[0]], t2 = a[b[1]];
for (int i = 0; i < 6; i += 2){
int id1 = b[i], id2 = b[i+1];
int nid1 = b[i+2], nid2 = b[i+3];
a[id1] = a[nid1];
a[id2] = a[nid2];
}
a[b[6] ] = t1; a[b[7] ] = t2;
if (check()) ok = 1;fuyuan();
t1 = a[b[7]], t2 = a[b[6]];
for (int i = 7; i > 2; i -= 2){
int id1 = b[i], id2 = b[i-1];
int nid1 = b[i-2], nid2 = b[i-3];
a[id1] = a[nid1];
a[id2] = a[nid2];
}
a[b[0] ] = t2; a[b[1] ] = t1;
if (check()) ok = 1; fuyuan();
}
void add(int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,int c0,int c1,int c2,int c3){
b[0] = a0;b[1] = a1;b[2] = a2;b[3] = a3;b[4] = a4;b[5] = a5;b[6] = a6;b[7] = a7;
c[0] = c0;c[1] = c1;c[2] = c2;c[3] = c3;
}
void up(){
add(14,15,20,22,5,4,19,17,2,3,1,0);
zhuan();
}
void down(){
add(12,13,21,23,7,6,18,16,8,9,11,10);
zhuan();
}
void Left(){
add(2,0,14,12,10,8,6,4,19,17,18,16);
zhuan();
}
void Right(){
add(3,1,15,13,11,9,7,5,20,22,23,21);
zhuan();
}
void qian(){
add(0,1,20,21,11,10,16,17,14,15,13,12);
zhuan();
}
void hou(){
add(2,3,22,23,9,8,18,19,4,5,6,7);
zhuan();
}
int main(){
int T;
scanf("%d",&T);
while(T--){
ok = 0;
for (int i = 0; i < 24; ++i) scanf("%d",a+i);
for (int i = 0; i < 24; ++i)ori[i] = a[i];
if (check()) ok = 1;
up(); down(); Left(); Right(); qian(); hou();
if (ok)puts("YES");
else puts("NO");
}
return 0;
}
+ - + - + - + - + - + - + | q | r | a | b | u | v | + - + - + - + - + - + - + | s | t | c | d | w | x | + - + - + - + - + - + - + | e | f | + - + - + | g | h | + - + - + | i | j | + - + - + | k | l | + - + - + | m | n | + - + - + | o | p | + - + - +