1. 程式人生 > >HDU 4801 Pocket Cube (模擬)

HDU 4801 Pocket Cube (模擬)

題意:

給出一個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);
	}
}