#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 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
142 ms |
6136 KB |
Output is correct |
8 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
588 KB |
Output is correct |
32 |
Correct |
1 ms |
588 KB |
Output is correct |
33 |
Correct |
8 ms |
2068 KB |
Output is correct |
34 |
Correct |
9 ms |
2080 KB |
Output is correct |
35 |
Correct |
31 ms |
2108 KB |
Output is correct |
36 |
Correct |
10 ms |
2380 KB |
Output is correct |
37 |
Correct |
11 ms |
2380 KB |
Output is correct |
38 |
Correct |
41 ms |
2380 KB |
Output is correct |
39 |
Correct |
13 ms |
2740 KB |
Output is correct |
40 |
Correct |
13 ms |
2748 KB |
Output is correct |
41 |
Correct |
55 ms |
2764 KB |
Output is correct |
42 |
Correct |
18 ms |
3364 KB |
Output is correct |
43 |
Correct |
17 ms |
3360 KB |
Output is correct |
44 |
Correct |
71 ms |
3348 KB |
Output is correct |
45 |
Correct |
20 ms |
3764 KB |
Output is correct |
46 |
Correct |
20 ms |
3796 KB |
Output is correct |
47 |
Correct |
80 ms |
3760 KB |
Output is correct |
48 |
Correct |
23 ms |
4236 KB |
Output is correct |
49 |
Correct |
22 ms |
4224 KB |
Output is correct |
50 |
Correct |
102 ms |
4320 KB |
Output is correct |
51 |
Correct |
28 ms |
4664 KB |
Output is correct |
52 |
Correct |
27 ms |
4680 KB |
Output is correct |
53 |
Correct |
123 ms |
4644 KB |
Output is correct |
54 |
Correct |
33 ms |
5152 KB |
Output is correct |
55 |
Correct |
30 ms |
5144 KB |
Output is correct |
56 |
Correct |
146 ms |
5116 KB |
Output is correct |
57 |
Correct |
37 ms |
5636 KB |
Output is correct |
58 |
Correct |
36 ms |
5636 KB |
Output is correct |
59 |
Correct |
171 ms |
5592 KB |
Output is correct |
60 |
Correct |
39 ms |
6160 KB |
Output is correct |
61 |
Correct |
46 ms |
6296 KB |
Output is correct |
62 |
Correct |
206 ms |
6152 KB |
Output is correct |
63 |
Correct |
141 ms |
6120 KB |
Output is correct |
64 |
Correct |
261 ms |
6240 KB |
Output is correct |
65 |
Correct |
202 ms |
6240 KB |
Output is correct |
66 |
Correct |
172 ms |
6112 KB |
Output is correct |
67 |
Incorrect |
164 ms |
6124 KB |
Output isn't correct |
68 |
Correct |
74 ms |
6140 KB |
Output is correct |
69 |
Correct |
70 ms |
6164 KB |
Output is correct |
70 |
Correct |
71 ms |
6136 KB |
Output is correct |
71 |
Correct |
80 ms |
6160 KB |
Output is correct |
72 |
Correct |
48 ms |
6144 KB |
Output is correct |
73 |
Correct |
56 ms |
6032 KB |
Output is correct |
74 |
Correct |
95 ms |
6136 KB |
Output is correct |
75 |
Correct |
120 ms |
6204 KB |
Output is correct |
76 |
Correct |
111 ms |
6140 KB |
Output is correct |
77 |
Correct |
118 ms |
6144 KB |
Output is correct |
78 |
Incorrect |
119 ms |
6156 KB |
Output isn't correct |
79 |
Correct |
81 ms |
6144 KB |
Output is correct |
80 |
Correct |
104 ms |
6140 KB |
Output is correct |
81 |
Correct |
117 ms |
6148 KB |
Output is correct |
82 |
Correct |
99 ms |
6136 KB |
Output is correct |
83 |
Correct |
129 ms |
6156 KB |
Output is correct |
84 |
Correct |
127 ms |
6148 KB |
Output is correct |
85 |
Correct |
132 ms |
6140 KB |
Output is correct |
86 |
Correct |
135 ms |
6152 KB |
Output is correct |
87 |
Correct |
127 ms |
6136 KB |
Output is correct |
88 |
Correct |
131 ms |
6152 KB |
Output is correct |
89 |
Correct |
149 ms |
6148 KB |
Output is correct |
90 |
Correct |
150 ms |
6164 KB |
Output is correct |
91 |
Correct |
139 ms |
6168 KB |
Output is correct |
92 |
Correct |
133 ms |
6140 KB |
Output is correct |