HDU 5836 Rubik's Cube

链接

传送门

题意

给出一个黑白色魔方,最多有五个黑块,求最多有几种不同形态。

思路

场上感觉这道能搞,搞了搞就出来了。枚举移动,用最小表示判重就好了。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
const int rf[][16] = {
{ 0, 1, 2, 5, 8, 7, 6, 3, 0, 1, 2, 5, 8, 7, 6, 3},
{ 9, 10, 11, 14, 17, 16, 15, 12, 9, 10, 11, 14, 17, 16, 15, 12},
{18, 19, 20, 23, 26, 25, 24, 21, 18, 19, 20, 23, 26, 25, 24, 21},
{27, 28, 29, 32, 35, 34, 33, 30, 27, 28, 29, 32, 35, 34, 33, 30},
{36, 37, 38, 41, 44, 43, 42, 39, 36, 37, 38, 41, 44, 43, 42, 39},
{45, 46, 47, 50, 53, 52, 51, 48, 45, 46, 47, 50, 53, 52, 51, 48},
};
const int re[][24] = {
{38, 37, 36, 29, 28, 27, 20, 19, 18, 11, 10, 9, 38, 37, 36, 29, 28, 27, 20, 19, 18, 11, 10, 9},
{ 0, 3, 6, 18, 21, 24, 45, 48, 51, 44, 41, 38, 0, 3, 6, 18, 21, 24, 45, 48, 51, 44, 41, 38},
{ 6, 7, 8, 27, 30, 33, 47, 46, 45, 17, 14, 11, 6, 7, 8, 27, 30, 33, 47, 46, 45, 17, 14, 11},
{36, 39, 42, 53, 50, 47, 26, 23, 20, 8, 5, 2, 36, 39, 42, 53, 50, 47, 26, 23, 20, 8, 5, 2},
{ 2, 1, 0, 9, 12, 15, 51, 52, 53, 35, 32, 29, 2, 1, 0, 9, 12, 15, 51, 52, 53, 35, 32, 29},
{15, 16, 17, 24, 25, 26, 33, 34, 35, 42, 43, 44, 15, 16, 17, 24, 25, 26, 33, 34, 35, 42, 43, 44},
{ 1, 4, 7, 19, 22, 25, 46, 49, 52, 43, 40, 37, 1, 4, 7, 19, 22, 25, 46, 49, 52, 43, 40, 37},
{12, 13, 14, 21, 22, 23, 30, 31, 32, 39, 40, 41, 12, 13, 14, 21, 22, 23, 30, 31, 32, 39, 40, 41},
{ 3, 4, 5, 28, 31, 34, 50, 49, 48, 16, 13, 10, 3, 4, 5, 28, 31, 34, 50, 49, 48, 16, 13, 10},
};
const LL Mask[] = {-481977700336, -2588525269081674, -246300532427201, -10278919686062373, -15796654028395016, -17447110756761601, -5146951406846099, -3855821598721, -1970344432837689};
set<LL> s;
queue<LL> q;
LL Rotate(LL k, int x, int d1, int d2) {
LL res = k & Mask[x];
for (int i = 0; i < 8; ++i) {
if (k & (1LL << rf[x][i])) {
res |= 1LL << rf[x][i + d1];
}
}
for (int i = 0; i < 12; ++i) {
if (k & (1LL << re[x][i])) {
res |= 1LL << re[x][i + d2];
}
}
return res;
}
LL Rotate2(LL k, int x, int d) {
LL res = k & Mask[x];
for (int i = 0; i < 12; ++i) {
if (k & (1LL << re[x][i])) {
res |= 1LL << re[x][i + d];
}
}
return res;
}
LL trans(LL k) {
LL res = k, x = k;
for (int i = 1; i < 4; ++i) {
x = Rotate(x, 2, 2, 3);
x = Rotate(x, 4, 6, 9);
x = Rotate2(x, 8, 3);
if (x < res) {
res = x;
}
}
x = Rotate(k, 0, 2, 3);
x = Rotate(x, 5, 6, 9);
x = Rotate2(x, 7, 9);
if (x < res) {
res = x;
}
for (int i = 1; i < 4; ++i) {
x = Rotate(x, 2, 2, 3);
x = Rotate(x, 4, 6, 9);
x = Rotate2(x, 8, 3);
if (x < res) {
res = x;
}
}
x = Rotate(k, 0, 4, 6);
x = Rotate(x, 5, 4, 6);
x = Rotate2(x, 7, 6);
if (x < res) {
res = x;
}
for (int i = 1; i < 4; ++i) {
x = Rotate(x, 2, 2, 3);
x = Rotate(x, 4, 6, 9);
x = Rotate2(x, 8, 3);
if (x < res) {
res = x;
}
}
x = Rotate(k, 0, 6, 9);
x = Rotate(x, 5, 2, 3);
x = Rotate2(x, 7, 3);
if (x < res) {
res = x;
}
for (int i = 1; i < 4; ++i) {
x = Rotate(x, 2, 2, 3);
x = Rotate(x, 4, 6, 9);
x = Rotate2(x, 8, 3);
if (x < res) {
res = x;
}
}
x = Rotate(k, 1, 2, 3);
x = Rotate(x, 3, 6, 9);
x = Rotate2(x, 6, 3);
if (x < res) {
res = x;
}
for (int i = 1; i < 4; ++i) {
x = Rotate(x, 2, 2, 3);
x = Rotate(x, 4, 6, 9);
x = Rotate2(x, 8, 3);
if (x < res) {
res = x;
}
}
x = Rotate(k, 1, 6, 9);
x = Rotate(x, 3, 2, 3);
x = Rotate2(x, 6, 9);
if (x < res) {
res = x;
}
for (int i = 1; i < 4; ++i) {
x = Rotate(x, 2, 2, 3);
x = Rotate(x, 4, 6, 9);
x = Rotate2(x, 8, 3);
if (x < res) {
res = x;
}
}
return res;
}
int main() {
int t, tt = 0;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
LL st = 0;
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
st |= 1LL << x;
}
st = trans(st);
q = queue<LL>();
s.clear();
q.push(st);
s.insert(st);
while (!q.empty()) {
LL k = q.front();
q.pop();
for (int i = 0; i < 6; ++i) {
LL a = trans(Rotate(k, i, 2, 3));
if (!s.count(a)) {
s.insert(a);
q.push(a);
}
LL b = trans(Rotate(k, i, 6, 9));
if (!s.count(b)) {
s.insert(b);
q.push(b);
}
}
}
printf("Case #%d: %d\n", ++tt, int(s.size()));
}
return 0;
}

支付宝扫码领红包