题意:
给出一个2阶魔方,问在少于N步旋转内,最多产生多少个同样颜色的面?
思路:
共6个有效方向,模拟即可。
有一个坑点,颜色不是0-5的,是 Integer 。判重时注意下即可
代码:
#include <bits/stdc++.h>
using namespace std;
int n, fans;
int dirx[6][8] = {
{ 0,2,6,12,16,18,20,22 },
{ 4,5,6,7,8,9,23,22 },
{ 0,1,9,15,19,18,10,4 },
{ 1,3,7,13,17,19,21,23 },
{ 10,11,12,13,14,15,21,20 },
{ 2,3,8,14,17,16,11,5 }
};
int diry[6][4] = {
{ 5,11,10,4 },
{ 2,3,1,0 },
{ 23,21,20,22 },
{ 8,14,15,9 },
{ 16,17,19,18 },
{ 7,13,12,6 }
};
int check[6][4] = {
{ 0,1,2,3 },
{ 4,5,10,11 },
{ 6,7,12,13 },
{ 8,9,14,15 },
{ 16,17,18,19 },
{ 23,21,20,22 }
};
int a[24], b[24];
void debug() {
for (int i = 0; i < 24; i++) {
cout << a[i] << ' ';
}
cout << endl;
}
bool make(int t) {
for (int i = 0; i < 24; i++) b[i] = a[i];
for (int i = 0; i < 8; i++) {
b[dirx[t][i]] = a[(dirx[t][(i + 2) % 8])];
}
for (int i = 0; i < 4; i++) {
b[diry[t][i]] = a[(diry[t][(i + 1) % 4])];
}
for (int i = 0; i < 8; i++) {
a[dirx[t][i]] = b[dirx[t][i]];
}
for (int i = 0; i < 4; i++) {
a[diry[t][i]] = b[diry[t][i]];
}
return 1;
}
int che() {
int ans = 0;
for (int i = 0; i < 6; i++) {
bool ok = 1;
for (int j = 0; j < 3; j++) {
if (a[check[i][j]] != a[check[i][j + 1]]) ok = 0;
}
ans += ok;
}
return ans;
}
void remake(int t) {
for (int i = 0; i < 24; i++) b[i] = a[i];
for (int i = 0; i < 8; i++) {
b[dirx[t][i]] = a[(dirx[t][(i + 6) % 8])];
}
for (int i = 0; i < 4; i++) {
b[diry[t][i]] = a[(diry[t][(i + 3) % 4])];
}
for (int i = 0; i < 8; i++) {
a[dirx[t][i]] = b[dirx[t][i]];
}
for (int i = 0; i < 4; i++) {
a[diry[t][i]] = b[diry[t][i]];
}
}
void dfs(int dep, int di) {
if (dep == 0 || fans == 6) return;
for (int i = 0; i < 6; i++) {
if ((i + 3) % 6 != di && fans != 6 && make(i)) {
fans = max(che(), fans);
dfs(dep - 1, i);
remake(i);
}
}
}
int main() {
while (scanf("%d", &n) != -1) {
fans = 0;
for (int i = 0; i < 24; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
fans = max(fans, che());
dfs(n,-1);
printf("%d\n", fans);
}
}