forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10047.cpp
101 lines (95 loc) · 1.62 KB
/
10047.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
#include <bits/stdc++.h>
using namespace std;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};
int nrows, ncols, endx, endy, map_[32][32], dist[32768], queue_[32768], head, tail;
int state(int y, int x, int d, int c)
{
return (y << 10) | (x << 5) | (d << 3) | c;
}
void push(int s, int d)
{
if (dist[s] == 0)
{
dist[s] = d;
queue_[tail++] = s;
}
}
int solve()
{
int i, j, k, y, x, d, c;
memset(map_, 1, sizeof(map_));
memset(dist, 0, sizeof(dist));
for (i = 1; i <= nrows; i++)
{
for (j = 1; j <= ncols && (k = getchar()) != EOF;)
{
if (k == '.')
{
map_[i][j++] = 0;
}
else if (k == '#')
{
map_[i][j++] = 1;
}
else if (k == 'S')
{
queue_[0] = state(i, j, 0, 0);
map_[i][j++] = 0;
}
else if (k == 'T')
{
endy = i;
endx = j;
map_[i][j++] = 0;
}
}
}
dist[queue_[0]] = 1;
head = 0;
tail = 1;
while (head < tail)
{
k = queue_[head++];
y = k >> 10;
x = (k >> 5) & 0x1F;
d = (k >> 3) & 3;
c = k & 7;
for (i = -1; i <= 1; i += 2)
{
push(state(y, x, (d + 4 + i) % 4, c), dist[k] + 1);
}
y += dy[d];
x += dx[d];
if (map_[y][x] == 0)
{
push(state(y, x, d, (c + 1) % 5), dist[k] + 1);
}
}
for (i = 0, j = 0; i < 4; i++)
{
k = state(endy, endx, i, 0);
if (dist[k] != 0 && (j == 0 || dist[k] < j))
{
j = dist[k];
}
}
return (j - 1);
}
int main()
{
int r, t;
for (t = 1; scanf("%d %d", &nrows, &ncols) == 2 && nrows > 0; t++)
{
printf("%sCase #%d\n", (t == 1) ? "" : "\n", t);
r = solve();
if (r < 0)
{
printf("destination not reachable\n");
}
else
{
printf("minimum time = %d sec\n", r);
}
}
return 0;
}