#include <iostream>
#include <vector>
#include <array>
#include <numeric>
#include <cassert>
#include <tuple>
#include <queue>
using namespace std;
using ll = long long;
const int INF = 1e9;
const array<int, 4> dx{1, -1, 0, 0}, dy{0, 0, 1, -1};
bool check(int w, int s, vector<string> &forest, vector<vector<int>> &beeD)
{
vector<vector<int>> dist(beeD.size(), vector<int>(beeD.size(), INF));
queue<pair<int, int>> q;
for (int i = 0; i < beeD.size(); ++i)
{
for (int j = 0; j < beeD.size(); ++j)
{
if (forest[i][j] == 'M')
{
dist[i][j] = 0;
q.push({i, j});
}
}
}
while (!q.empty())
{
auto [i, j] = q.front();
q.pop();
for (int k = 0; k < 4; ++k)
{
if (i + dx[k] >= 0 && i + dx[k] < beeD.size() && j + dy[k] >= 0 && j + dy[k] < beeD.size())
{
if (dist[i + dx[k]][j + dy[k]] > dist[i][j] + 1 && forest[i + dx[k]][j + dy[k]] != 'T')
{
if ((dist[i][j] + 1) / s + w < beeD[i + dx[k]][j + dy[k]])
{
dist[i + dx[k]][j + dy[k]] = dist[i][j] + 1;
q.push({i + dx[k], j + dy[k]});
}
}
}
}
}
for (int i = 0; i < beeD.size(); ++i) for (int j = 0; j < beeD.size(); ++j) if (forest[i][j] == 'D') return dist[i][j] != INF;
return false;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, s;
cin >> n >> s;
vector<string> forest(n);
for (string &i : forest) cin >> i;
vector<vector<int>> beeD(n, vector<int>(n, INF));
queue<pair<int, int>> q;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (forest[i][j] == 'H')
{
beeD[i][j] = 0;
q.push({i, j});
}
}
}
while (!q.empty())
{
auto [i, j] = q.front();
q.pop();
for (int k = 0; k < 4; ++k)
{
if (i + dx[k] >= 0 && i + dx[k] < n && j + dy[k] >= 0 && j + dy[k] < n)
{
if (beeD[i + dx[k]][j + dy[k]] > beeD[i][j] + 1 && (forest[i + dx[k]][j + dy[k]] == 'G' || forest[i + dx[k]][j + dy[k]] == 'M'))
{
beeD[i + dx[k]][j + dy[k]] = beeD[i][j] + 1;
q.push({i + dx[k], j + dy[k]});
}
}
}
}
int l = 0, r = n * n;
while (l < r)
{
int m = (l + r) >> 1;
if (check(m, s, forest, beeD)) l = m + 1;
else r = m;
}
cout << l - 1;
return 0;
}
/*
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT
*/
Compilation message
mecho.cpp: In function 'bool check(int, int, std::vector<std::__cxx11::basic_string<char> >&, std::vector<std::vector<int> >&)':
mecho.cpp:21:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 0; i < beeD.size(); ++i)
| ~~^~~~~~~~~~~~~
mecho.cpp:23:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int j = 0; j < beeD.size(); ++j)
| ~~^~~~~~~~~~~~~
mecho.cpp:38:45: warning: comparison of integer expressions of different signedness: 'std::tuple_element<0, std::pair<int, int> >::type' {aka 'int'} and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if (i + dx[k] >= 0 && i + dx[k] < beeD.size() && j + dy[k] >= 0 && j + dy[k] < beeD.size())
| ~~~~~~~~~~^~~~~~~~~~~~~
mecho.cpp:38:90: warning: comparison of integer expressions of different signedness: 'std::tuple_element<1, std::pair<int, int> >::type' {aka 'int'} and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if (i + dx[k] >= 0 && i + dx[k] < beeD.size() && j + dy[k] >= 0 && j + dy[k] < beeD.size())
| ~~~~~~~~~~^~~~~~~~~~~~~
mecho.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 0; i < beeD.size(); ++i) for (int j = 0; j < beeD.size(); ++j) if (forest[i][j] == 'D') return dist[i][j] != INF;
| ~~^~~~~~~~~~~~~
mecho.cpp:51:61: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 0; i < beeD.size(); ++i) for (int j = 0; j < beeD.size(); ++j) if (forest[i][j] == 'D') return dist[i][j] != INF;
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
113 ms |
6156 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
8 ms |
1432 KB |
Output is correct |
34 |
Correct |
14 ms |
1484 KB |
Output is correct |
35 |
Correct |
30 ms |
1436 KB |
Output is correct |
36 |
Correct |
10 ms |
1748 KB |
Output is correct |
37 |
Correct |
13 ms |
1748 KB |
Output is correct |
38 |
Correct |
35 ms |
1788 KB |
Output is correct |
39 |
Correct |
14 ms |
2164 KB |
Output is correct |
40 |
Correct |
15 ms |
2152 KB |
Output is correct |
41 |
Correct |
43 ms |
2148 KB |
Output is correct |
42 |
Correct |
19 ms |
2624 KB |
Output is correct |
43 |
Correct |
24 ms |
2632 KB |
Output is correct |
44 |
Correct |
55 ms |
2612 KB |
Output is correct |
45 |
Correct |
20 ms |
3052 KB |
Output is correct |
46 |
Correct |
23 ms |
3044 KB |
Output is correct |
47 |
Correct |
70 ms |
3100 KB |
Output is correct |
48 |
Correct |
23 ms |
3620 KB |
Output is correct |
49 |
Correct |
26 ms |
3624 KB |
Output is correct |
50 |
Correct |
72 ms |
3600 KB |
Output is correct |
51 |
Correct |
30 ms |
4176 KB |
Output is correct |
52 |
Correct |
33 ms |
4168 KB |
Output is correct |
53 |
Correct |
107 ms |
4132 KB |
Output is correct |
54 |
Correct |
34 ms |
4772 KB |
Output is correct |
55 |
Correct |
38 ms |
4820 KB |
Output is correct |
56 |
Correct |
107 ms |
4768 KB |
Output is correct |
57 |
Correct |
40 ms |
5424 KB |
Output is correct |
58 |
Correct |
48 ms |
5412 KB |
Output is correct |
59 |
Correct |
137 ms |
5504 KB |
Output is correct |
60 |
Correct |
49 ms |
6124 KB |
Output is correct |
61 |
Correct |
51 ms |
6184 KB |
Output is correct |
62 |
Correct |
165 ms |
6084 KB |
Output is correct |
63 |
Correct |
135 ms |
6108 KB |
Output is correct |
64 |
Correct |
225 ms |
6104 KB |
Output is correct |
65 |
Correct |
204 ms |
6128 KB |
Output is correct |
66 |
Correct |
146 ms |
6092 KB |
Output is correct |
67 |
Correct |
173 ms |
6084 KB |
Output is correct |
68 |
Correct |
94 ms |
6124 KB |
Output is correct |
69 |
Correct |
75 ms |
6120 KB |
Output is correct |
70 |
Correct |
62 ms |
6140 KB |
Output is correct |
71 |
Correct |
59 ms |
6124 KB |
Output is correct |
72 |
Incorrect |
54 ms |
6208 KB |
Output isn't correct |
73 |
Incorrect |
54 ms |
6144 KB |
Output isn't correct |
74 |
Correct |
75 ms |
6116 KB |
Output is correct |
75 |
Correct |
113 ms |
6120 KB |
Output is correct |
76 |
Correct |
130 ms |
6124 KB |
Output is correct |
77 |
Correct |
101 ms |
6144 KB |
Output is correct |
78 |
Correct |
103 ms |
6120 KB |
Output is correct |
79 |
Correct |
67 ms |
6160 KB |
Output is correct |
80 |
Correct |
77 ms |
6152 KB |
Output is correct |
81 |
Correct |
95 ms |
6144 KB |
Output is correct |
82 |
Correct |
133 ms |
6124 KB |
Output is correct |
83 |
Correct |
160 ms |
6132 KB |
Output is correct |
84 |
Correct |
85 ms |
6128 KB |
Output is correct |
85 |
Correct |
100 ms |
6136 KB |
Output is correct |
86 |
Correct |
128 ms |
6132 KB |
Output is correct |
87 |
Correct |
128 ms |
6120 KB |
Output is correct |
88 |
Correct |
102 ms |
6232 KB |
Output is correct |
89 |
Correct |
96 ms |
6060 KB |
Output is correct |
90 |
Correct |
98 ms |
6160 KB |
Output is correct |
91 |
Correct |
103 ms |
6124 KB |
Output is correct |
92 |
Correct |
123 ms |
6124 KB |
Output is correct |