-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVJ 搜索 Key Task.cpp
125 lines (117 loc) · 2.76 KB
/
VJ 搜索 Key Task.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
这个题是一个bfs搜索加上一个状态压缩,每一把钥匙对应每一扇门,那么我们就可以利用二进制存
我们目前拥有的钥匙数量,如果捡到一把钥匙就用之前的拥有钥匙的二进制与这个做|运算,如果碰
到一扇门那么我们就用我们目前拥有钥匙的二进制与门进行&运算判断能否开门,其他的部分和正常
bfs一样
#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
#define Maxn 110
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 {
int x;
int y;
int step;
int key;
};
char map1[Maxn][Maxn];
int vis[Maxn][Maxn][22];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int n, m;
fin st;
int door[4] = {'B', 'Y', 'R', 'G'};
char Key[4] = {'b', 'y', 'r', 'g'};
void bfs() {
format(vis);
queue<fin> Q;
fin p, q;
st.key = 0;
st.step = 0;
vis[st.x][st.y][st.key] = 1;
Q.push(st);
while (!Q.empty()) {
p = Q.front();
Q.pop();
if (map1[p.x][p.y] == 'X') {
cout << "Escape possible in " << p.step << " steps." << endl;
//printf("Escape possible in %d steps.\n", p.step);
return;
}
for (int i = 0; i < 4; i++) {
q.x = p.x + dir[i][0];
q.y = p.y + dir[i][1];
q.step = p.step + 1;
q.key = p.key;
if (q.x < 1 || q.x > n || q.y < 1 || q.y > m || map1[q.x][q.y] == '#') {
continue;
}
if (isupper(map1[q.x][q.y]) && map1[q.x][q.y] != 'X') {
for (int j = 0; j < 4; j++) {
if (map1[q.x][q.y] == door[j]) {
if (q.key & (1 << j)) {
if (!vis[q.x][q.y][q.key]) {
vis[q.x][q.y][q.key] = 1;
Q.push(q);
}
break;
}
}
}
}
else if (islower(map1[q.x][q.y])) {
for (int j = 0; j < 4; j++) {
if (map1[q.x][q.y] == Key[j]) {
if ((q.key & (1 << j)) == 0) {
q.key += (1 << j);
}
if (!vis[q.x][q.y][q.key]) {
vis[q.x][q.y][q.key] = 1;
Q.push(q);
}
}
}
} else {
if (!vis[q.x][q.y][q.key]) {
vis[q.x][q.y][q.key] = 1;
Q.push(q);
}
}
}
}
cout << "The poor student is trapped!" << endl;
}
int main(int argc, char *argv[]) {
while (cin >> n >> m && n + m) {
for (int i = 1; i <= n; i++) {
scanf("%s", map1[i] + 1);
for (int j = 1; j <= m; j++) {
if (map1[i][j] == '*') {
st.x = i;
st.y = j;
}
}
}
bfs();
}
return 0;
}