#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 * 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 |
153 ms |
6676 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 |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is 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 |
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 |
460 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 |
2 ms |
588 KB |
Output is correct |
32 |
Correct |
1 ms |
588 KB |
Output is correct |
33 |
Correct |
9 ms |
2164 KB |
Output is correct |
34 |
Correct |
10 ms |
2164 KB |
Output is correct |
35 |
Correct |
38 ms |
2152 KB |
Output is correct |
36 |
Correct |
12 ms |
2508 KB |
Output is correct |
37 |
Correct |
14 ms |
2544 KB |
Output is correct |
38 |
Correct |
44 ms |
2512 KB |
Output is correct |
39 |
Correct |
15 ms |
2872 KB |
Output is correct |
40 |
Correct |
17 ms |
2884 KB |
Output is correct |
41 |
Correct |
60 ms |
2868 KB |
Output is correct |
42 |
Correct |
19 ms |
3500 KB |
Output is correct |
43 |
Correct |
21 ms |
3528 KB |
Output is correct |
44 |
Correct |
92 ms |
3508 KB |
Output is correct |
45 |
Correct |
22 ms |
4012 KB |
Output is correct |
46 |
Correct |
28 ms |
3932 KB |
Output is correct |
47 |
Correct |
89 ms |
3940 KB |
Output is correct |
48 |
Correct |
26 ms |
4484 KB |
Output is correct |
49 |
Correct |
30 ms |
4472 KB |
Output is correct |
50 |
Correct |
110 ms |
4468 KB |
Output is correct |
51 |
Correct |
31 ms |
4996 KB |
Output is correct |
52 |
Correct |
32 ms |
4980 KB |
Output is correct |
53 |
Correct |
145 ms |
4956 KB |
Output is correct |
54 |
Correct |
44 ms |
5476 KB |
Output is correct |
55 |
Correct |
36 ms |
5476 KB |
Output is correct |
56 |
Correct |
157 ms |
5592 KB |
Output is correct |
57 |
Correct |
41 ms |
6100 KB |
Output is correct |
58 |
Correct |
42 ms |
6084 KB |
Output is correct |
59 |
Correct |
189 ms |
6084 KB |
Output is correct |
60 |
Correct |
56 ms |
6664 KB |
Output is correct |
61 |
Correct |
52 ms |
6748 KB |
Output is correct |
62 |
Correct |
207 ms |
6652 KB |
Output is correct |
63 |
Correct |
149 ms |
6636 KB |
Output is correct |
64 |
Correct |
299 ms |
6664 KB |
Output is correct |
65 |
Correct |
206 ms |
6660 KB |
Output is correct |
66 |
Correct |
214 ms |
6636 KB |
Output is correct |
67 |
Incorrect |
181 ms |
6676 KB |
Output isn't correct |
68 |
Correct |
87 ms |
6676 KB |
Output is correct |
69 |
Correct |
84 ms |
6672 KB |
Output is correct |
70 |
Correct |
80 ms |
6684 KB |
Output is correct |
71 |
Correct |
73 ms |
6700 KB |
Output is correct |
72 |
Incorrect |
95 ms |
6756 KB |
Output isn't correct |
73 |
Incorrect |
76 ms |
6776 KB |
Output isn't correct |
74 |
Correct |
111 ms |
6668 KB |
Output is correct |
75 |
Correct |
153 ms |
6700 KB |
Output is correct |
76 |
Correct |
132 ms |
6700 KB |
Output is correct |
77 |
Correct |
130 ms |
6684 KB |
Output is correct |
78 |
Incorrect |
137 ms |
6668 KB |
Output isn't correct |
79 |
Correct |
99 ms |
6688 KB |
Output is correct |
80 |
Correct |
110 ms |
6672 KB |
Output is correct |
81 |
Correct |
122 ms |
6780 KB |
Output is correct |
82 |
Correct |
116 ms |
6724 KB |
Output is correct |
83 |
Correct |
162 ms |
6684 KB |
Output is correct |
84 |
Correct |
148 ms |
6716 KB |
Output is correct |
85 |
Correct |
148 ms |
6688 KB |
Output is correct |
86 |
Correct |
165 ms |
6716 KB |
Output is correct |
87 |
Correct |
148 ms |
6688 KB |
Output is correct |
88 |
Correct |
192 ms |
6692 KB |
Output is correct |
89 |
Correct |
179 ms |
6684 KB |
Output is correct |
90 |
Correct |
169 ms |
6688 KB |
Output is correct |
91 |
Correct |
150 ms |
6676 KB |
Output is correct |
92 |
Correct |
143 ms |
6660 KB |
Output is correct |