#include <bits/stdc++.h>
using namespace std;
const int maxn = 810;
typedef pair<int, int> pii;
int n, s, ox, oy, dx, dy;
int dist[maxn][maxn];
char tab[maxn][maxn];
bool mark[maxn][maxn];
int linha[] = {-1, 1, 0, 0};
int coluna[] = {0, 0, -1, 1};
void bfs(void)
{
queue<pii> fila;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (tab[i][j] == 'H')
dist[i][j] = 0, fila.push({i, j});
dist[dx][dy] = 1e9+10;
while (!fila.empty())
{
int x = fila.front().first, y = fila.front().second;
fila.pop();
for (int i = 0; i < 4; i++)
{
int a = x+linha[i], b = y+coluna[i];
if (a < 1 || a > n || b < 1 || b > n || tab[a][b] == 'T' || tab[a][b] == 'D' || dist[a][b] != -1) continue;
dist[a][b] = dist[x][y] + s;
fila.push({a, b});
}
}
}
bool ok(int t)
{
if (t*s >= dist[ox][oy]) return false;
queue<pair<int, pii> > fila;
memset(mark, 0, sizeof mark);
mark[ox][oy] = 1;
fila.push({t*s, {ox, oy}});
while (!fila.empty())
{
int d = fila.front().first;
int x = fila.front().second.first, y = fila.front().second.second;
fila.pop();
for (int i = 0; i < 4; i++)
{
int a = x+linha[i], b = y+coluna[i];
if (a < 1 || a > n || b < 1 || b > n || tab[a][b] == 'T' || d+1 >= dist[a][b] || mark[a][b]) continue;
if (tab[a][b] == 'D') return true;
mark[a][b] = 1;
fila.push({d+1, {a, b}});
}
}
return false;
}
int busca(void)
{
int ini = 0, fim = 640000*2, ans = -1;
while (ini <= fim)
{
int mid = (ini+fim)>>1;
if (ok(mid)) ini = mid+1, ans = mid;
else fim = mid-1;
}
return ans;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> s;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> tab[i][j];
if (tab[i][j] == 'M') ox = i, oy = j;
if (tab[i][j] == 'D') dx = i, dy = j;
}
}
memset(dist, -1, sizeof dist);
bfs();
cout << busca() << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3584 KB |
Output is correct |
2 |
Correct |
5 ms |
3584 KB |
Output is correct |
3 |
Correct |
6 ms |
3584 KB |
Output is correct |
4 |
Correct |
6 ms |
3584 KB |
Output is correct |
5 |
Correct |
5 ms |
3584 KB |
Output is correct |
6 |
Correct |
5 ms |
3584 KB |
Output is correct |
7 |
Correct |
125 ms |
4216 KB |
Output is correct |
8 |
Correct |
5 ms |
3584 KB |
Output is correct |
9 |
Correct |
5 ms |
3584 KB |
Output is correct |
10 |
Correct |
5 ms |
3584 KB |
Output is correct |
11 |
Correct |
5 ms |
3584 KB |
Output is correct |
12 |
Correct |
6 ms |
3584 KB |
Output is correct |
13 |
Correct |
6 ms |
3584 KB |
Output is correct |
14 |
Correct |
5 ms |
3584 KB |
Output is correct |
15 |
Correct |
5 ms |
3584 KB |
Output is correct |
16 |
Correct |
6 ms |
3584 KB |
Output is correct |
17 |
Correct |
5 ms |
3584 KB |
Output is correct |
18 |
Correct |
5 ms |
3584 KB |
Output is correct |
19 |
Correct |
5 ms |
3584 KB |
Output is correct |
20 |
Correct |
5 ms |
3584 KB |
Output is correct |
21 |
Correct |
5 ms |
3584 KB |
Output is correct |
22 |
Correct |
6 ms |
3584 KB |
Output is correct |
23 |
Correct |
6 ms |
3584 KB |
Output is correct |
24 |
Correct |
5 ms |
3584 KB |
Output is correct |
25 |
Correct |
6 ms |
3584 KB |
Output is correct |
26 |
Correct |
5 ms |
3584 KB |
Output is correct |
27 |
Correct |
5 ms |
3584 KB |
Output is correct |
28 |
Correct |
5 ms |
3604 KB |
Output is correct |
29 |
Correct |
5 ms |
3584 KB |
Output is correct |
30 |
Correct |
5 ms |
3584 KB |
Output is correct |
31 |
Correct |
5 ms |
3584 KB |
Output is correct |
32 |
Correct |
5 ms |
3584 KB |
Output is correct |
33 |
Correct |
10 ms |
3840 KB |
Output is correct |
34 |
Correct |
8 ms |
3840 KB |
Output is correct |
35 |
Correct |
32 ms |
3840 KB |
Output is correct |
36 |
Correct |
10 ms |
3840 KB |
Output is correct |
37 |
Correct |
11 ms |
3840 KB |
Output is correct |
38 |
Correct |
54 ms |
3840 KB |
Output is correct |
39 |
Correct |
12 ms |
3840 KB |
Output is correct |
40 |
Correct |
12 ms |
3968 KB |
Output is correct |
41 |
Correct |
65 ms |
3968 KB |
Output is correct |
42 |
Correct |
12 ms |
3968 KB |
Output is correct |
43 |
Correct |
11 ms |
3968 KB |
Output is correct |
44 |
Correct |
62 ms |
3968 KB |
Output is correct |
45 |
Correct |
14 ms |
3968 KB |
Output is correct |
46 |
Correct |
16 ms |
3968 KB |
Output is correct |
47 |
Correct |
76 ms |
3968 KB |
Output is correct |
48 |
Correct |
17 ms |
3968 KB |
Output is correct |
49 |
Correct |
19 ms |
4096 KB |
Output is correct |
50 |
Correct |
113 ms |
4072 KB |
Output is correct |
51 |
Correct |
16 ms |
4088 KB |
Output is correct |
52 |
Correct |
21 ms |
4096 KB |
Output is correct |
53 |
Correct |
108 ms |
4088 KB |
Output is correct |
54 |
Correct |
22 ms |
4096 KB |
Output is correct |
55 |
Correct |
25 ms |
4096 KB |
Output is correct |
56 |
Correct |
148 ms |
4216 KB |
Output is correct |
57 |
Correct |
21 ms |
4224 KB |
Output is correct |
58 |
Correct |
28 ms |
4216 KB |
Output is correct |
59 |
Correct |
148 ms |
4216 KB |
Output is correct |
60 |
Correct |
22 ms |
4224 KB |
Output is correct |
61 |
Correct |
20 ms |
4196 KB |
Output is correct |
62 |
Correct |
161 ms |
4224 KB |
Output is correct |
63 |
Correct |
133 ms |
4216 KB |
Output is correct |
64 |
Correct |
241 ms |
4260 KB |
Output is correct |
65 |
Correct |
178 ms |
4216 KB |
Output is correct |
66 |
Correct |
140 ms |
4320 KB |
Output is correct |
67 |
Correct |
139 ms |
4248 KB |
Output is correct |
68 |
Correct |
64 ms |
4216 KB |
Output is correct |
69 |
Correct |
60 ms |
4216 KB |
Output is correct |
70 |
Correct |
40 ms |
4216 KB |
Output is correct |
71 |
Correct |
55 ms |
4260 KB |
Output is correct |
72 |
Correct |
37 ms |
4216 KB |
Output is correct |
73 |
Correct |
52 ms |
4448 KB |
Output is correct |
74 |
Correct |
71 ms |
4320 KB |
Output is correct |
75 |
Correct |
103 ms |
4576 KB |
Output is correct |
76 |
Correct |
84 ms |
4320 KB |
Output is correct |
77 |
Correct |
76 ms |
4448 KB |
Output is correct |
78 |
Correct |
114 ms |
4348 KB |
Output is correct |
79 |
Correct |
69 ms |
4396 KB |
Output is correct |
80 |
Correct |
94 ms |
4424 KB |
Output is correct |
81 |
Correct |
93 ms |
4348 KB |
Output is correct |
82 |
Correct |
76 ms |
4476 KB |
Output is correct |
83 |
Correct |
118 ms |
4256 KB |
Output is correct |
84 |
Correct |
105 ms |
4396 KB |
Output is correct |
85 |
Correct |
107 ms |
4252 KB |
Output is correct |
86 |
Correct |
103 ms |
4360 KB |
Output is correct |
87 |
Correct |
99 ms |
4256 KB |
Output is correct |
88 |
Correct |
122 ms |
4312 KB |
Output is correct |
89 |
Correct |
108 ms |
4308 KB |
Output is correct |
90 |
Correct |
120 ms |
4308 KB |
Output is correct |
91 |
Correct |
132 ms |
4388 KB |
Output is correct |
92 |
Correct |
141 ms |
4316 KB |
Output is correct |