#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)
{
if (starttime * s <= bees[mecho.first][mecho.second])
{
return false;
}
queue<pair<int, int>> q;
q.push(mecho);
vector<vector<int>> mechodist(n, vector<int> (n, -1));
mechodist[mecho.first][mecho.second] = starttime * s;
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 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Incorrect |
54 ms |
6272 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 |
Correct |
1 ms |
460 KB |
Output is 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 |
328 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
360 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
456 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
460 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 |
460 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 |
1 ms |
588 KB |
Output isn't correct |
33 |
Incorrect |
7 ms |
2124 KB |
Output isn't correct |
34 |
Incorrect |
7 ms |
2132 KB |
Output isn't correct |
35 |
Incorrect |
9 ms |
2136 KB |
Output isn't correct |
36 |
Incorrect |
8 ms |
2380 KB |
Output isn't correct |
37 |
Incorrect |
9 ms |
2380 KB |
Output isn't correct |
38 |
Incorrect |
15 ms |
2468 KB |
Output isn't correct |
39 |
Incorrect |
10 ms |
2764 KB |
Output isn't correct |
40 |
Incorrect |
10 ms |
2828 KB |
Output isn't correct |
41 |
Incorrect |
15 ms |
2876 KB |
Output isn't correct |
42 |
Incorrect |
13 ms |
3436 KB |
Output isn't correct |
43 |
Incorrect |
12 ms |
3432 KB |
Output isn't correct |
44 |
Incorrect |
18 ms |
3468 KB |
Output isn't correct |
45 |
Incorrect |
15 ms |
3848 KB |
Output isn't correct |
46 |
Incorrect |
15 ms |
3844 KB |
Output isn't correct |
47 |
Incorrect |
24 ms |
3916 KB |
Output isn't correct |
48 |
Incorrect |
17 ms |
4304 KB |
Output isn't correct |
49 |
Incorrect |
17 ms |
4300 KB |
Output isn't correct |
50 |
Incorrect |
32 ms |
4332 KB |
Output isn't correct |
51 |
Incorrect |
20 ms |
4676 KB |
Output isn't correct |
52 |
Incorrect |
21 ms |
4744 KB |
Output isn't correct |
53 |
Incorrect |
37 ms |
4756 KB |
Output isn't correct |
54 |
Incorrect |
23 ms |
5216 KB |
Output isn't correct |
55 |
Incorrect |
23 ms |
5204 KB |
Output isn't correct |
56 |
Incorrect |
46 ms |
5268 KB |
Output isn't correct |
57 |
Incorrect |
26 ms |
5708 KB |
Output isn't correct |
58 |
Incorrect |
26 ms |
5708 KB |
Output isn't correct |
59 |
Incorrect |
54 ms |
5732 KB |
Output isn't correct |
60 |
Incorrect |
30 ms |
6212 KB |
Output isn't correct |
61 |
Incorrect |
30 ms |
6212 KB |
Output isn't correct |
62 |
Incorrect |
60 ms |
6240 KB |
Output isn't correct |
63 |
Correct |
48 ms |
6224 KB |
Output is correct |
64 |
Incorrect |
56 ms |
6232 KB |
Output isn't correct |
65 |
Incorrect |
49 ms |
6240 KB |
Output isn't correct |
66 |
Incorrect |
51 ms |
6248 KB |
Output isn't correct |
67 |
Incorrect |
48 ms |
6244 KB |
Output isn't correct |
68 |
Correct |
55 ms |
6288 KB |
Output is correct |
69 |
Incorrect |
58 ms |
6264 KB |
Output isn't correct |
70 |
Incorrect |
66 ms |
6260 KB |
Output isn't correct |
71 |
Incorrect |
56 ms |
6272 KB |
Output isn't correct |
72 |
Incorrect |
61 ms |
6256 KB |
Output isn't correct |
73 |
Incorrect |
62 ms |
6300 KB |
Output isn't correct |
74 |
Incorrect |
75 ms |
6268 KB |
Output isn't correct |
75 |
Incorrect |
72 ms |
6288 KB |
Output isn't correct |
76 |
Incorrect |
55 ms |
6260 KB |
Output isn't correct |
77 |
Incorrect |
65 ms |
6288 KB |
Output isn't correct |
78 |
Incorrect |
66 ms |
6268 KB |
Output isn't correct |
79 |
Incorrect |
55 ms |
6296 KB |
Output isn't correct |
80 |
Incorrect |
54 ms |
6268 KB |
Output isn't correct |
81 |
Incorrect |
54 ms |
6316 KB |
Output isn't correct |
82 |
Incorrect |
55 ms |
6304 KB |
Output isn't correct |
83 |
Correct |
55 ms |
6276 KB |
Output is correct |
84 |
Incorrect |
55 ms |
6328 KB |
Output isn't correct |
85 |
Incorrect |
57 ms |
6276 KB |
Output isn't correct |
86 |
Incorrect |
67 ms |
6320 KB |
Output isn't correct |
87 |
Incorrect |
54 ms |
6308 KB |
Output isn't correct |
88 |
Correct |
55 ms |
6256 KB |
Output is correct |
89 |
Incorrect |
54 ms |
6292 KB |
Output isn't correct |
90 |
Incorrect |
65 ms |
6272 KB |
Output isn't correct |
91 |
Incorrect |
66 ms |
6264 KB |
Output isn't correct |
92 |
Incorrect |
55 ms |
6260 KB |
Output isn't correct |