#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using il = pair<ll, ll>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
void setIO(string name) {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (!name.empty()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
const int LIM = 1e9;
const int N = 808;
int n, s, vi[N][N];
ii st, en;
vector<ii> hives;
char a[N][N];
bool e(int i, int j) { return i >= 1 && i <= n && j >= 1 && j <= n; }
void propaganda(vector<ii> &lst, int xet, int dis) {
queue<pair<ii, int>> bfs;
for(auto x : lst) bfs.push({x, 0});
lst.clear();
while (!bfs.empty()) {
auto at = bfs.front(); bfs.pop();
int x = at.fi.fi, y = at.fi.se;
if (vi[x][y] == 2 && xet == 1) {
continue;
}
if (at.se == dis) {
lst.emplace_back(at.fi);
continue;
}
foru(d, 0, 3) {
int nx = x+dx[d], ny = y+dy[d];
if (!e(nx, ny) || a[nx][ny] == 'T') continue;
if (xet == 2 && a[nx][ny] == 'D') continue;
if (vi[nx][ny] != 0) {
if (xet == 1) continue;
if (vi[nx][ny] == 2) continue;
}
vi[nx][ny] = xet;
bfs.push({{nx, ny}, at.se+1});
}
}
}
void out() {
foru(i, 1, n) {
foru(j, 1, n) cout << vi[i][j] << " ";
cout << "\n";
}
cout << "---------\n";
}
bool can(int eat_time) {
memset(vi, 0, sizeof(vi));
vector<ii> me, bees;
me.emplace_back(st);
bees = hives;
propaganda(bees, 2, eat_time);
while (!me.empty()) {
propaganda(me, 1, s);
propaganda(bees, 2, 1);
}
return vi[en.fi][en.se] == 1;
}
int main() {
setIO("");
cin >> n >> s;
foru(i, 1, n)
foru(j, 1, n) {
cin >> a[i][j];
if (a[i][j] == 'M') st = {i, j};
else if (a[i][j] == 'D') en = {i, j};
else if (a[i][j] == 'H') hives.emplace_back(i, j);
}
int ans = -1;
for(int z=n*n; z>0; z>>=1)
while (ans+z <= n*n && can(ans+z))
ans += z;
cout << ans;
}
Compilation message
mecho.cpp: In function 'void setIO(std::string)':
mecho.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
1 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
330 ms |
3856 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2900 KB |
Output is correct |
13 |
Correct |
2 ms |
2932 KB |
Output is correct |
14 |
Correct |
3 ms |
2900 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2900 KB |
Output is correct |
17 |
Correct |
2 ms |
2900 KB |
Output is correct |
18 |
Correct |
2 ms |
2900 KB |
Output is correct |
19 |
Correct |
2 ms |
2900 KB |
Output is correct |
20 |
Correct |
2 ms |
2900 KB |
Output is correct |
21 |
Correct |
2 ms |
2900 KB |
Output is correct |
22 |
Correct |
2 ms |
2900 KB |
Output is correct |
23 |
Correct |
3 ms |
2900 KB |
Output is correct |
24 |
Correct |
2 ms |
2900 KB |
Output is correct |
25 |
Correct |
3 ms |
2900 KB |
Output is correct |
26 |
Correct |
3 ms |
2900 KB |
Output is correct |
27 |
Correct |
3 ms |
2900 KB |
Output is correct |
28 |
Correct |
3 ms |
2900 KB |
Output is correct |
29 |
Correct |
3 ms |
2900 KB |
Output is correct |
30 |
Correct |
3 ms |
2900 KB |
Output is correct |
31 |
Correct |
3 ms |
2944 KB |
Output is correct |
32 |
Correct |
3 ms |
2900 KB |
Output is correct |
33 |
Correct |
34 ms |
3160 KB |
Output is correct |
34 |
Correct |
27 ms |
3028 KB |
Output is correct |
35 |
Correct |
35 ms |
3168 KB |
Output is correct |
36 |
Correct |
47 ms |
3180 KB |
Output is correct |
37 |
Correct |
35 ms |
3156 KB |
Output is correct |
38 |
Correct |
45 ms |
3208 KB |
Output is correct |
39 |
Correct |
58 ms |
3156 KB |
Output is correct |
40 |
Correct |
43 ms |
3156 KB |
Output is correct |
41 |
Correct |
58 ms |
3248 KB |
Output is correct |
42 |
Correct |
79 ms |
3256 KB |
Output is correct |
43 |
Correct |
58 ms |
3156 KB |
Output is correct |
44 |
Correct |
84 ms |
3276 KB |
Output is correct |
45 |
Correct |
87 ms |
3296 KB |
Output is correct |
46 |
Correct |
71 ms |
3284 KB |
Output is correct |
47 |
Correct |
85 ms |
3284 KB |
Output is correct |
48 |
Correct |
106 ms |
3284 KB |
Output is correct |
49 |
Correct |
78 ms |
3348 KB |
Output is correct |
50 |
Correct |
105 ms |
3352 KB |
Output is correct |
51 |
Correct |
125 ms |
3376 KB |
Output is correct |
52 |
Correct |
100 ms |
3376 KB |
Output is correct |
53 |
Correct |
122 ms |
3408 KB |
Output is correct |
54 |
Correct |
154 ms |
3412 KB |
Output is correct |
55 |
Correct |
104 ms |
3412 KB |
Output is correct |
56 |
Correct |
146 ms |
3436 KB |
Output is correct |
57 |
Correct |
176 ms |
3456 KB |
Output is correct |
58 |
Correct |
149 ms |
3464 KB |
Output is correct |
59 |
Correct |
170 ms |
3488 KB |
Output is correct |
60 |
Correct |
186 ms |
3500 KB |
Output is correct |
61 |
Correct |
144 ms |
3412 KB |
Output is correct |
62 |
Correct |
195 ms |
3540 KB |
Output is correct |
63 |
Correct |
282 ms |
3508 KB |
Output is correct |
64 |
Correct |
273 ms |
3512 KB |
Output is correct |
65 |
Correct |
287 ms |
3520 KB |
Output is correct |
66 |
Correct |
357 ms |
3520 KB |
Output is correct |
67 |
Correct |
279 ms |
3512 KB |
Output is correct |
68 |
Correct |
294 ms |
3568 KB |
Output is correct |
69 |
Correct |
242 ms |
3568 KB |
Output is correct |
70 |
Correct |
286 ms |
3616 KB |
Output is correct |
71 |
Correct |
266 ms |
3556 KB |
Output is correct |
72 |
Correct |
398 ms |
3556 KB |
Output is correct |
73 |
Correct |
233 ms |
4408 KB |
Output is correct |
74 |
Correct |
329 ms |
3980 KB |
Output is correct |
75 |
Correct |
358 ms |
3968 KB |
Output is correct |
76 |
Correct |
384 ms |
3960 KB |
Output is correct |
77 |
Correct |
311 ms |
3972 KB |
Output is correct |
78 |
Correct |
286 ms |
4236 KB |
Output is correct |
79 |
Correct |
301 ms |
3924 KB |
Output is correct |
80 |
Correct |
331 ms |
3924 KB |
Output is correct |
81 |
Correct |
325 ms |
3924 KB |
Output is correct |
82 |
Correct |
338 ms |
3888 KB |
Output is correct |
83 |
Correct |
310 ms |
4252 KB |
Output is correct |
84 |
Correct |
319 ms |
3832 KB |
Output is correct |
85 |
Correct |
322 ms |
3832 KB |
Output is correct |
86 |
Correct |
330 ms |
3928 KB |
Output is correct |
87 |
Correct |
323 ms |
3844 KB |
Output is correct |
88 |
Correct |
306 ms |
4000 KB |
Output is correct |
89 |
Correct |
339 ms |
3760 KB |
Output is correct |
90 |
Correct |
305 ms |
3880 KB |
Output is correct |
91 |
Correct |
335 ms |
3916 KB |
Output is correct |
92 |
Correct |
328 ms |
4060 KB |
Output is correct |