-
Notifications
You must be signed in to change notification settings - Fork 1
/
VJ 搜索 魔板.cpp
105 lines (95 loc) · 2.07 KB
/
VJ 搜索 魔板.cpp
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
这个题用到的是康托展开,利用1,2,3,4,5,6,7,8进行排列,求出每一个排列组合对应的位置,
再用逆康托展开求出某一个位置所对应的排列方式,预处理之后再输出我们要求的值进行判断即可.
#include <bits/stdc++.h>
#define INF 0x7fffffff
#define rep(x, y, z) for (int x = y; x <= z; x++)
#define dec(x, y, z) for (int x = y; x >= z; x--)
#define format(a) memset (a, 0, sizeof(a))
#define swap(a, b) (a ^= b ^= a ^= b)
#define ll long long int
#define ull unsigned long long int
#define uint unsigned int
using namespace std;
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') {
w = -1;
}
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * w;
}
struct fin {
char s[8];
};
int d[3][8] = {{7, 6, 5, 4, 3, 2, 1, 0}, {3, 0, 1, 2, 5, 6, 7, 4}, {0, 6, 1, 3, 4, 2, 5, 7}};
char str[8], fir[9], sec[9];
int vis[60010];
string ans[60010];
int a[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};
int gethash(char s[]) {
int cnt, i, j;
int hash = 0;
for (i = 0; i < 8; i++) {
cnt = 0;
for (j = i + 1; j < 8; j++) {
if (s[j] < s[i]) {
cnt++;
}
}
hash += cnt * a[8 - i - 1];
}
return hash;
}
void bfs() {
int i, j;
queue<fin> q;
fin a, b;
for (i = 0; i < 8; i++) {
a.s[i] = i + '1';
}
int k = gethash(a.s);
q.push(a);
vis[k] = 1;
q.push(a);
while (q.size()) {
a = q.front();
q.pop();
int Hash = gethash(a.s);
for (i = 0; i < 3; i++) {
for (j = 0; j < 8; j++) {
b.s[j] = a.s[d[i][j]];
}
k = gethash(b.s);
if (vis[k]) {
continue;
}
vis[k] = 1;
ans[k] = ans[Hash] + (char)('A' + i);
q.push(b);
}
}
}
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
bfs();
while (scanf("%s%s", fir, sec) != EOF) {
for (int i = 0; i < 8; i++) {
str[fir[i] - '1'] = i + '1';
}
for (int i = 0; i < 8; i++) {
sec[i] = str[sec[i] - '1'];
}
int Ans = gethash(sec);
cout << ans[Ans] << endl;
}
return 0;
}