#include <bits/stdc++.h>
using namespace std;
#define int long long
#define forinc(i,a,b) for(int i=a;i<=b;++i)
#define fordec(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define eb emplace_back
#define pii pair <int,int>
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int N = 888, inf = 1e15;
const int H[] = {1, 0, -1, 0};
const int C[] = {0, 1, 0, -1};
int n, s, cti = inf;
char a[N][N];
int t[N][N];
bool cx[N][N];
pii sta, fin;
bool valid (const int& x, const int& y)
{
return x && y && x <= n && y <= n
&& a[x][y] != 'T';
}
struct adt {int x, y, step;};
bool ok (const int& tg)
{
queue <adt> q;
forinc(i,1,n) forinc(j,1,n) cx[i][j] = 0;
q.push({sta.fi, sta.se, 0});
cx[sta.fi][sta.se] = 1;
while(q.size()) {
auto tmp = q.front(); q.pop();
if(tmp.x == fin.fi && tmp.y == fin.se) return 1;
forinc(i,0,3) {
int nh = tmp.x + H[i];
int nc = tmp.y + C[i];
if(valid(nh, nc) && !cx[nh][nc] && (tmp.step + 1) / s < t[nh][nc] - tg) {
q.push({nh, nc, tmp.step + 1});
cx[nh][nc] = 1;
}
}
}
return 0;
}
void solve ()
{
queue <pii> q;
forinc(i,1,n) forinc(j,1,n) {
t[i][j] = inf;
if(a[i][j] == 'H') {
q.push({i, j});
t[i][j] = 0;
}
else if(a[i][j] == 'M') sta = {i, j};
else if(a[i][j] == 'D') fin = {i, j};
}
// bfs bee
int last = 0;
while(q.size()) {
auto x = q.front(); q.pop();
forinc(i,0,3) {
int nh = x.fi + H[i];
int nc = x.se + C[i];
if(valid(nh, nc) && t[nh][nc] > t[x.fi][x.se] + 1) {
t[nh][nc] = t[x.fi][x.se] + 1;
last = max(last, t[nh][nc]);
q.push({nh, nc});
}
}
}
// binary search
int l = 0, r = last, ans = -1;
while(l <= r) {
int mid = l + r >> 1;
if(ok(mid)) {
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
cout << ans;
}
void read ()
{
cin >> n >> s;
forinc(i,1,n) forinc(j,1,n)
cin >> a[i][j];
solve();
}
main ()
{
#define task "ioi09_mecho"
cin.tie(0) -> sync_with_stdio(0);
if(fopen (task ".inp", "r")) {
freopen (task ".inp", "r", stdin);
freopen (task ".out", "w", stdout);
}
read();
return 0;
}
Compilation message
mecho.cpp: In function 'void solve()':
mecho.cpp:85:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
85 | int mid = l + r >> 1;
| ~~^~~
mecho.cpp: At global scope:
mecho.cpp:103:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
103 | main ()
| ^~~~
mecho.cpp: In function 'int main()':
mecho.cpp:108:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen (task ".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:109:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen (task ".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Incorrect |
109 ms |
7460 KB |
Output isn't correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 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 |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
716 KB |
Output is correct |
28 |
Correct |
1 ms |
716 KB |
Output is correct |
29 |
Correct |
1 ms |
716 KB |
Output is correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Correct |
1 ms |
716 KB |
Output is correct |
32 |
Correct |
1 ms |
720 KB |
Output is correct |
33 |
Correct |
7 ms |
3280 KB |
Output is correct |
34 |
Correct |
5 ms |
3268 KB |
Output is correct |
35 |
Correct |
42 ms |
3308 KB |
Output is correct |
36 |
Correct |
6 ms |
3708 KB |
Output is correct |
37 |
Correct |
8 ms |
3788 KB |
Output is correct |
38 |
Correct |
30 ms |
3712 KB |
Output is correct |
39 |
Correct |
10 ms |
4172 KB |
Output is correct |
40 |
Correct |
6 ms |
4172 KB |
Output is correct |
41 |
Correct |
39 ms |
4256 KB |
Output is correct |
42 |
Correct |
10 ms |
4572 KB |
Output is correct |
43 |
Correct |
8 ms |
4644 KB |
Output is correct |
44 |
Correct |
47 ms |
4596 KB |
Output is correct |
45 |
Correct |
10 ms |
4992 KB |
Output is correct |
46 |
Correct |
10 ms |
5068 KB |
Output is correct |
47 |
Correct |
61 ms |
5120 KB |
Output is correct |
48 |
Correct |
16 ms |
5452 KB |
Output is correct |
49 |
Correct |
14 ms |
5532 KB |
Output is correct |
50 |
Correct |
93 ms |
5556 KB |
Output is correct |
51 |
Correct |
19 ms |
5940 KB |
Output is correct |
52 |
Correct |
20 ms |
5976 KB |
Output is correct |
53 |
Correct |
84 ms |
5956 KB |
Output is correct |
54 |
Correct |
21 ms |
6312 KB |
Output is correct |
55 |
Correct |
16 ms |
6416 KB |
Output is correct |
56 |
Correct |
109 ms |
6440 KB |
Output is correct |
57 |
Correct |
20 ms |
6780 KB |
Output is correct |
58 |
Correct |
19 ms |
6836 KB |
Output is correct |
59 |
Correct |
154 ms |
6852 KB |
Output is correct |
60 |
Correct |
22 ms |
7196 KB |
Output is correct |
61 |
Correct |
18 ms |
7176 KB |
Output is correct |
62 |
Correct |
150 ms |
7288 KB |
Output is correct |
63 |
Correct |
111 ms |
7268 KB |
Output is correct |
64 |
Correct |
206 ms |
7236 KB |
Output is correct |
65 |
Correct |
196 ms |
7272 KB |
Output is correct |
66 |
Correct |
136 ms |
7264 KB |
Output is correct |
67 |
Correct |
119 ms |
7272 KB |
Output is correct |
68 |
Correct |
52 ms |
7328 KB |
Output is correct |
69 |
Correct |
46 ms |
7320 KB |
Output is correct |
70 |
Correct |
43 ms |
7284 KB |
Output is correct |
71 |
Correct |
49 ms |
7276 KB |
Output is correct |
72 |
Incorrect |
34 ms |
7272 KB |
Output isn't correct |
73 |
Incorrect |
47 ms |
7768 KB |
Output isn't correct |
74 |
Correct |
76 ms |
7824 KB |
Output is correct |
75 |
Correct |
68 ms |
7788 KB |
Output is correct |
76 |
Correct |
69 ms |
7764 KB |
Output is correct |
77 |
Correct |
85 ms |
7812 KB |
Output is correct |
78 |
Correct |
90 ms |
7704 KB |
Output is correct |
79 |
Correct |
56 ms |
7748 KB |
Output is correct |
80 |
Correct |
69 ms |
7716 KB |
Output is correct |
81 |
Correct |
70 ms |
7712 KB |
Output is correct |
82 |
Correct |
59 ms |
7760 KB |
Output is correct |
83 |
Correct |
96 ms |
7684 KB |
Output is correct |
84 |
Correct |
84 ms |
7624 KB |
Output is correct |
85 |
Correct |
80 ms |
7580 KB |
Output is correct |
86 |
Correct |
89 ms |
7644 KB |
Output is correct |
87 |
Correct |
88 ms |
7680 KB |
Output is correct |
88 |
Correct |
94 ms |
7548 KB |
Output is correct |
89 |
Correct |
94 ms |
7592 KB |
Output is correct |
90 |
Correct |
121 ms |
7532 KB |
Output is correct |
91 |
Correct |
97 ms |
7492 KB |
Output is correct |
92 |
Correct |
95 ms |
7584 KB |
Output is correct |