#include <bits/stdc++.h>
using namespace std;
#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 = 802, inf = 2e9;
const int H[] = {1, 0, -1, 0};
const int C[] = {0, 1, 0, -1};
int n, s;
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;
memset(cx, 0, sizeof cx);
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
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;
q.push({nh, nc});
}
}
}
t[fin.fi][fin.se] = inf;
// binary search
int l = 0, r = t[sta.fi][sta.se] - 1, 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:82:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
82 | int mid = l + r >> 1;
| ~~^~~
mecho.cpp: At global scope:
mecho.cpp:100:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
100 | main ()
| ^~~~
mecho.cpp: In function 'int main()':
mecho.cpp:105:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen (task ".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:106:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen (task ".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
82 ms |
4188 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
984 KB |
Output is correct |
11 |
Correct |
1 ms |
1024 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
1 ms |
1052 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
1 ms |
972 KB |
Output is correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
1 ms |
1100 KB |
Output is correct |
24 |
Correct |
1 ms |
1100 KB |
Output is correct |
25 |
Correct |
1 ms |
1100 KB |
Output is correct |
26 |
Correct |
1 ms |
1100 KB |
Output is correct |
27 |
Correct |
1 ms |
1100 KB |
Output is correct |
28 |
Correct |
1 ms |
1100 KB |
Output is correct |
29 |
Correct |
1 ms |
1228 KB |
Output is correct |
30 |
Correct |
1 ms |
1228 KB |
Output is correct |
31 |
Correct |
2 ms |
1228 KB |
Output is correct |
32 |
Correct |
1 ms |
1256 KB |
Output is correct |
33 |
Correct |
4 ms |
2240 KB |
Output is correct |
34 |
Correct |
4 ms |
2256 KB |
Output is correct |
35 |
Correct |
20 ms |
2336 KB |
Output is correct |
36 |
Correct |
5 ms |
2508 KB |
Output is correct |
37 |
Correct |
6 ms |
2508 KB |
Output is correct |
38 |
Correct |
26 ms |
2508 KB |
Output is correct |
39 |
Correct |
7 ms |
2692 KB |
Output is correct |
40 |
Correct |
10 ms |
2604 KB |
Output is correct |
41 |
Correct |
49 ms |
2728 KB |
Output is correct |
42 |
Correct |
8 ms |
2824 KB |
Output is correct |
43 |
Correct |
8 ms |
2892 KB |
Output is correct |
44 |
Correct |
39 ms |
2892 KB |
Output is correct |
45 |
Correct |
12 ms |
2992 KB |
Output is correct |
46 |
Correct |
16 ms |
2992 KB |
Output is correct |
47 |
Correct |
65 ms |
3100 KB |
Output is correct |
48 |
Correct |
15 ms |
3188 KB |
Output is correct |
49 |
Correct |
11 ms |
3192 KB |
Output is correct |
50 |
Correct |
62 ms |
3228 KB |
Output is correct |
51 |
Correct |
11 ms |
3388 KB |
Output is correct |
52 |
Correct |
13 ms |
3404 KB |
Output is correct |
53 |
Correct |
71 ms |
3492 KB |
Output is correct |
54 |
Correct |
18 ms |
3656 KB |
Output is correct |
55 |
Correct |
13 ms |
3696 KB |
Output is correct |
56 |
Correct |
122 ms |
3688 KB |
Output is correct |
57 |
Correct |
16 ms |
3900 KB |
Output is correct |
58 |
Correct |
16 ms |
3780 KB |
Output is correct |
59 |
Correct |
129 ms |
3896 KB |
Output is correct |
60 |
Correct |
17 ms |
3976 KB |
Output is correct |
61 |
Correct |
17 ms |
3972 KB |
Output is correct |
62 |
Correct |
129 ms |
4088 KB |
Output is correct |
63 |
Correct |
131 ms |
4072 KB |
Output is correct |
64 |
Correct |
220 ms |
4072 KB |
Output is correct |
65 |
Correct |
192 ms |
3952 KB |
Output is correct |
66 |
Correct |
143 ms |
4080 KB |
Output is correct |
67 |
Correct |
143 ms |
4072 KB |
Output is correct |
68 |
Correct |
61 ms |
3992 KB |
Output is correct |
69 |
Correct |
48 ms |
3980 KB |
Output is correct |
70 |
Correct |
44 ms |
4100 KB |
Output is correct |
71 |
Correct |
37 ms |
4100 KB |
Output is correct |
72 |
Correct |
37 ms |
3984 KB |
Output is correct |
73 |
Correct |
48 ms |
4260 KB |
Output is correct |
74 |
Correct |
85 ms |
4356 KB |
Output is correct |
75 |
Correct |
66 ms |
4348 KB |
Output is correct |
76 |
Correct |
67 ms |
4356 KB |
Output is correct |
77 |
Correct |
74 ms |
4240 KB |
Output is correct |
78 |
Correct |
104 ms |
4320 KB |
Output is correct |
79 |
Correct |
62 ms |
4224 KB |
Output is correct |
80 |
Correct |
75 ms |
4320 KB |
Output is correct |
81 |
Correct |
86 ms |
4324 KB |
Output is correct |
82 |
Correct |
70 ms |
4320 KB |
Output is correct |
83 |
Correct |
98 ms |
4184 KB |
Output is correct |
84 |
Correct |
94 ms |
4284 KB |
Output is correct |
85 |
Correct |
84 ms |
4284 KB |
Output is correct |
86 |
Correct |
100 ms |
4288 KB |
Output is correct |
87 |
Correct |
82 ms |
4168 KB |
Output is correct |
88 |
Correct |
104 ms |
4232 KB |
Output is correct |
89 |
Correct |
106 ms |
4112 KB |
Output is correct |
90 |
Correct |
107 ms |
4244 KB |
Output is correct |
91 |
Correct |
92 ms |
4112 KB |
Output is correct |
92 |
Correct |
100 ms |
4128 KB |
Output is correct |