#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
typedef long long llong;
const int MAXN = 800 + 10;
const int INF = 1e9;
int n, s;
char t[MAXN][MAXN];
int beeDist[MAXN][MAXN];
int myDist[MAXN][MAXN];
std::pair <int,int> delta[] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
std::queue <std::pair <int,int>> q;
bool inside(int row, int col)
{
return row >= 1 && row <= n && col >= 1 && col <= n;
}
bool check(int time)
{
time *= s;
for (int i = 1 ; i <= n ; ++i)
{
for (int j = 1 ; j <= n ; ++j)
{
myDist[i][j] = INF;
if (t[i][j] == 'M')
{
if (beeDist[i][j] <= time)
{
return false;
}
myDist[i][j] = time;
q.push({i, j});
}
}
}
while (q.size())
{
auto [row, col] = q.front();
q.pop();
for (const auto &[dx, dy] : delta)
{
if (inside(row + dx, col + dy) && myDist[row][col] + 1 < beeDist[row + dx][col + dy] && t[row + dx][col + dy] != 'T' && myDist[row + dx][col + dy] == INF)
{
myDist[row + dx][col + dy] = myDist[row][col] + 1;
q.push({row + dx, col + dy});
}
}
}
for (int i = 1 ; i <= n ; ++i)
{
for (int j = 1 ; j <= n ; ++j)
{
if (t[i][j] == 'D')
{
return (myDist[i][j] != INF);
}
}
}
assert(false);
}
void solve()
{
for (int i = 1 ; i <= n ; ++i)
{
for (int j = 1 ; j <= n ; ++j)
{
beeDist[i][j] = (t[i][j] == 'H' ? 0 : INF);
if (t[i][j] == 'H')
{
q.push({i, j});
beeDist[i][j] = 0;
}
}
}
while (q.size())
{
auto [row, col] = q.front();
q.pop();
for (const auto &[dx, dy] : delta)
{
if (inside(row + dx, col + dy) && t[row + dx][col + dy] != 'D' && t[row + dx][col + dy] != 'T' && beeDist[row + dx][col + dy] == INF)
{
beeDist[row + dx][col + dy] = beeDist[row][col] + s;
q.push({row + dx, col + dy});
}
}
}
int l = -1, r = n * n, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (check(mid)) l = mid;
else r = mid;
}
std::cout << l << '\n';
}
void input()
{
std::cin >> n >> s;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> t[i] + 1;
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
Compilation message
mecho.cpp: In function 'void input()':
mecho.cpp:120:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
120 | std::cin >> t[i] + 1;
| ~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
69 ms |
6300 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
4612 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2520 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
4 ms |
4952 KB |
Output is correct |
34 |
Correct |
6 ms |
4956 KB |
Output is correct |
35 |
Correct |
16 ms |
4956 KB |
Output is correct |
36 |
Correct |
4 ms |
4956 KB |
Output is correct |
37 |
Correct |
7 ms |
4956 KB |
Output is correct |
38 |
Correct |
21 ms |
4952 KB |
Output is correct |
39 |
Correct |
6 ms |
4956 KB |
Output is correct |
40 |
Correct |
8 ms |
4956 KB |
Output is correct |
41 |
Correct |
28 ms |
4956 KB |
Output is correct |
42 |
Correct |
7 ms |
4956 KB |
Output is correct |
43 |
Correct |
11 ms |
4956 KB |
Output is correct |
44 |
Correct |
35 ms |
4956 KB |
Output is correct |
45 |
Correct |
8 ms |
5208 KB |
Output is correct |
46 |
Correct |
12 ms |
5212 KB |
Output is correct |
47 |
Correct |
38 ms |
5208 KB |
Output is correct |
48 |
Correct |
10 ms |
5212 KB |
Output is correct |
49 |
Correct |
14 ms |
5408 KB |
Output is correct |
50 |
Correct |
47 ms |
5392 KB |
Output is correct |
51 |
Correct |
11 ms |
5464 KB |
Output is correct |
52 |
Correct |
16 ms |
5468 KB |
Output is correct |
53 |
Correct |
65 ms |
5720 KB |
Output is correct |
54 |
Correct |
13 ms |
5724 KB |
Output is correct |
55 |
Correct |
19 ms |
5724 KB |
Output is correct |
56 |
Correct |
70 ms |
5632 KB |
Output is correct |
57 |
Correct |
15 ms |
5976 KB |
Output is correct |
58 |
Correct |
23 ms |
5980 KB |
Output is correct |
59 |
Correct |
81 ms |
5992 KB |
Output is correct |
60 |
Correct |
16 ms |
5980 KB |
Output is correct |
61 |
Correct |
24 ms |
6232 KB |
Output is correct |
62 |
Correct |
92 ms |
5980 KB |
Output is correct |
63 |
Correct |
84 ms |
6188 KB |
Output is correct |
64 |
Correct |
156 ms |
5980 KB |
Output is correct |
65 |
Correct |
117 ms |
5980 KB |
Output is correct |
66 |
Correct |
96 ms |
6180 KB |
Output is correct |
67 |
Correct |
93 ms |
5980 KB |
Output is correct |
68 |
Correct |
41 ms |
6060 KB |
Output is correct |
69 |
Correct |
41 ms |
6236 KB |
Output is correct |
70 |
Correct |
32 ms |
6056 KB |
Output is correct |
71 |
Correct |
37 ms |
6052 KB |
Output is correct |
72 |
Correct |
27 ms |
5980 KB |
Output is correct |
73 |
Correct |
21 ms |
6232 KB |
Output is correct |
74 |
Correct |
42 ms |
6236 KB |
Output is correct |
75 |
Correct |
52 ms |
6464 KB |
Output is correct |
76 |
Correct |
45 ms |
6232 KB |
Output is correct |
77 |
Correct |
46 ms |
6236 KB |
Output is correct |
78 |
Correct |
59 ms |
6236 KB |
Output is correct |
79 |
Correct |
42 ms |
6236 KB |
Output is correct |
80 |
Correct |
49 ms |
6232 KB |
Output is correct |
81 |
Correct |
54 ms |
6232 KB |
Output is correct |
82 |
Correct |
49 ms |
6232 KB |
Output is correct |
83 |
Correct |
56 ms |
6236 KB |
Output is correct |
84 |
Correct |
50 ms |
6236 KB |
Output is correct |
85 |
Correct |
57 ms |
6236 KB |
Output is correct |
86 |
Correct |
53 ms |
6232 KB |
Output is correct |
87 |
Correct |
57 ms |
6392 KB |
Output is correct |
88 |
Correct |
58 ms |
6232 KB |
Output is correct |
89 |
Correct |
69 ms |
6348 KB |
Output is correct |
90 |
Correct |
61 ms |
6232 KB |
Output is correct |
91 |
Correct |
66 ms |
6348 KB |
Output is correct |
92 |
Correct |
72 ms |
6236 KB |
Output is correct |