forked from cacophonix/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CERC07K.cpp
89 lines (84 loc) · 2.26 KB
/
CERC07K.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
/*
USER: zobayer
TASK: CERC07K
ALGO: breadth first search
*/
#include <cstdio>
#include <queue>
#include <cctype>
using namespace std;
typedef pair< int, int > pii;
typedef pair< pii, int > ppi;
const int MAX = 101;
char grid[MAX][MAX];
int d[MAX][MAX][16], R, C;
int dr[4] = {0, 0, 1,-1};
int dc[4] = {1,-1, 0, 0};
inline bool ingrid(const pii &p) {
return (p.first>=0 && p.first<R && p.second>=0 && p.second<C);
};
int bfs(const pii &s) {
pii u, v;
int umask, vmask, i;
queue< ppi > Q;
Q.push(ppi(s,0));
d[s.first][s.second][0] = 0;
while(!Q.empty()) {
u = Q.front().first;
umask = Q.front().second;
Q.pop();
for(i=0; i<4; i++) {
v = pii(u.first+dr[i], u.second+dc[i]);
if(ingrid(v) && grid[v.first][v.second]!='#') {
if(grid[v.first][v.second]=='X') return d[u.first][u.second][umask]+1;
if(d[v.first][v.second][umask]==-1) {
vmask = umask;
if(islower(grid[v.first][v.second])) {
switch(grid[v.first][v.second]) {
case 'b': vmask |= 0x1; break;
case 'y': vmask |= 0x2; break;
case 'r': vmask |= 0x4; break;
case 'g': vmask |= 0x8; break;
}
Q.push(ppi(v, vmask));
d[v.first][v.second][umask] = d[u.first][u.second][umask]+1;
d[v.first][v.second][vmask] = d[v.first][v.second][umask];
}
else if(isupper(grid[v.first][v.second])) {
switch(grid[v.first][v.second]) {
case 'B': if(umask & 0x1) Q.push(ppi(v, umask)); break;
case 'Y': if(umask & 0x2) Q.push(ppi(v, umask)); break;
case 'R': if(umask & 0x4) Q.push(ppi(v, umask)); break;
case 'G': if(umask & 0x8) Q.push(ppi(v, umask)); break;
}
d[v.first][v.second][umask] = d[u.first][u.second][umask]+1;
}
else {
Q.push(ppi(v, umask));
d[v.first][v.second][umask] = d[u.first][u.second][umask]+1;
}
}
}
}
}
return -1;
}
int main() {
int i, j, k, dis;
pii start;
//freopen("C:\\in.txt", "r", stdin);
while(scanf("%d%d", &R, &C)==2) {
if(!R && !C) break;
for(i=0; i<R; i++) {
scanf("%s", grid[i]);
for(j=0; j<C; j++) {
if(grid[i][j]=='*') start = pii(i, j);
for(k=0; k<16; k++) d[i][j][k] = -1;
}
}
dis = bfs(start);
if(dis<0) printf("The poor student is trapped!\n");
else printf("Escape possible in %d steps.\n", dis);
}
return 0;
}