#include <iostream>
#include <string>
#include <queue>
using namespace std;
bool validmecho(int i, int j);
bool validbees(int i, int j);
bool bfs(int starttime);
string grid[800];
int bees[800][800];
int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1};
int n, s;
pair<int, int> mecho;
int main()
{
cin >> n >> s;
for (int i = 0; i < n; i++)
{
cin >> grid[i];
for (int j = 0; j < n; j++)
{
if (grid[i][j] == 'M')
{
mecho = {i, j};
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
bees[i][j] = 1e9;
}
}
queue<pair<int, int>> q;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (grid[i][j] == 'H')
{
q.push({i, j});
bees[i][j] = 0;
}
}
}
while (!q.empty())
{
int curri = q.front().first, currj = q.front().second;
q.pop();
for (int d = 0; d < 4; d++)
{
int nexti = curri + dx[d], nextj = currj + dy[d];
if (validbees(nexti, nextj) && bees[nexti][nextj] > bees[curri][currj] + s)
{
bees[nexti][nextj] = bees[curri][currj] + s;
q.push({nexti, nextj});
}
}
}
int low = 0, high = n * n;
while (low < high)
{
int mid = (low + high + 1) / 2;
if (bfs(mid))
{
low = mid;
}
else
{
high = mid - 1;
}
}
cout << low << endl;
return 0;
}
bool ongrid(int i, int j)
{
return (i >= 0 && i < n && j >= 0 && j < n);
}
bool validbees(int i, int j)
{
return (ongrid(i, j) && !(grid[i][j] == 'T') && !(grid[i][j] == 'D') && !(grid[i][j] == 'H'));
}
bool validmecho(int i, int j)
{
return (ongrid(i, j) && !(grid[i][j] == 'T'));
}
bool bfs(int starttime)
{
queue<pair<int, int>> q;
q.push(mecho);
vector<vector<int>> mechodist(n, vector<int> (n, -1));
mechodist[mecho.first][mecho.second] = starttime * 3;
while (!q.empty())
{
int curri = q.front().first, currj = q.front().second;
q.pop();
if (grid[curri][currj] == 'D')
{
return true;
}
for (int d = 0; d < 4; d++)
{
int nexti = curri + dx[d], nextj = currj + dy[d];
if (!validmecho(nexti, nextj))
{
continue;
}
if (mechodist[nexti][nextj] == -1)
{
if (bees[nexti][nextj] > mechodist[curri][currj] + 1)
{
mechodist[nexti][nextj] = mechodist[curri][currj] + 1;
q.push({nexti, nextj});
}
}
}
}
return false;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Incorrect |
195 ms |
6368 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
464 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
456 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
432 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
32 |
Incorrect |
2 ms |
588 KB |
Output isn't correct |
33 |
Incorrect |
12 ms |
2188 KB |
Output isn't correct |
34 |
Incorrect |
10 ms |
2200 KB |
Output isn't correct |
35 |
Incorrect |
50 ms |
2196 KB |
Output isn't correct |
36 |
Incorrect |
13 ms |
2524 KB |
Output isn't correct |
37 |
Incorrect |
12 ms |
2508 KB |
Output isn't correct |
38 |
Incorrect |
69 ms |
2508 KB |
Output isn't correct |
39 |
Incorrect |
15 ms |
2996 KB |
Output isn't correct |
40 |
Incorrect |
15 ms |
2912 KB |
Output isn't correct |
41 |
Incorrect |
86 ms |
2940 KB |
Output isn't correct |
42 |
Incorrect |
19 ms |
3636 KB |
Output isn't correct |
43 |
Incorrect |
21 ms |
3624 KB |
Output isn't correct |
44 |
Incorrect |
112 ms |
3572 KB |
Output isn't correct |
45 |
Incorrect |
22 ms |
4052 KB |
Output isn't correct |
46 |
Incorrect |
24 ms |
3980 KB |
Output isn't correct |
47 |
Incorrect |
139 ms |
4048 KB |
Output isn't correct |
48 |
Incorrect |
26 ms |
4444 KB |
Output isn't correct |
49 |
Incorrect |
28 ms |
4480 KB |
Output isn't correct |
50 |
Incorrect |
163 ms |
4704 KB |
Output isn't correct |
51 |
Incorrect |
31 ms |
4920 KB |
Output isn't correct |
52 |
Incorrect |
31 ms |
4920 KB |
Output isn't correct |
53 |
Incorrect |
214 ms |
4956 KB |
Output isn't correct |
54 |
Incorrect |
36 ms |
5396 KB |
Output isn't correct |
55 |
Incorrect |
37 ms |
5472 KB |
Output isn't correct |
56 |
Incorrect |
239 ms |
5336 KB |
Output isn't correct |
57 |
Incorrect |
42 ms |
5860 KB |
Output isn't correct |
58 |
Incorrect |
42 ms |
5860 KB |
Output isn't correct |
59 |
Incorrect |
304 ms |
5824 KB |
Output isn't correct |
60 |
Incorrect |
47 ms |
6520 KB |
Output isn't correct |
61 |
Incorrect |
49 ms |
6384 KB |
Output isn't correct |
62 |
Incorrect |
363 ms |
6332 KB |
Output isn't correct |
63 |
Correct |
147 ms |
6364 KB |
Output is correct |
64 |
Incorrect |
374 ms |
6448 KB |
Output isn't correct |
65 |
Incorrect |
243 ms |
6332 KB |
Output isn't correct |
66 |
Correct |
178 ms |
6348 KB |
Output is correct |
67 |
Incorrect |
160 ms |
6404 KB |
Output isn't correct |
68 |
Incorrect |
101 ms |
6372 KB |
Output isn't correct |
69 |
Incorrect |
96 ms |
6356 KB |
Output isn't correct |
70 |
Incorrect |
85 ms |
6384 KB |
Output isn't correct |
71 |
Incorrect |
78 ms |
6428 KB |
Output isn't correct |
72 |
Incorrect |
65 ms |
6432 KB |
Output isn't correct |
73 |
Incorrect |
101 ms |
6400 KB |
Output isn't correct |
74 |
Incorrect |
155 ms |
6396 KB |
Output isn't correct |
75 |
Incorrect |
138 ms |
6372 KB |
Output isn't correct |
76 |
Incorrect |
149 ms |
6508 KB |
Output isn't correct |
77 |
Incorrect |
146 ms |
6396 KB |
Output isn't correct |
78 |
Incorrect |
155 ms |
6372 KB |
Output isn't correct |
79 |
Incorrect |
144 ms |
6440 KB |
Output isn't correct |
80 |
Incorrect |
120 ms |
6364 KB |
Output isn't correct |
81 |
Incorrect |
132 ms |
6372 KB |
Output isn't correct |
82 |
Incorrect |
126 ms |
6372 KB |
Output isn't correct |
83 |
Correct |
161 ms |
6372 KB |
Output is correct |
84 |
Incorrect |
215 ms |
6428 KB |
Output isn't correct |
85 |
Incorrect |
167 ms |
6388 KB |
Output isn't correct |
86 |
Incorrect |
190 ms |
6436 KB |
Output isn't correct |
87 |
Incorrect |
199 ms |
6364 KB |
Output isn't correct |
88 |
Incorrect |
172 ms |
6380 KB |
Output isn't correct |
89 |
Incorrect |
239 ms |
6592 KB |
Output isn't correct |
90 |
Incorrect |
192 ms |
6372 KB |
Output isn't correct |
91 |
Incorrect |
226 ms |
6360 KB |
Output isn't correct |
92 |
Incorrect |
202 ms |
6456 KB |
Output isn't correct |