#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;
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});
}
}
}
// binary search
int l = 1, r = n * n * n, 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:83:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
83 | int mid = l + r >> 1;
| ~~^~~
mecho.cpp: At global scope:
mecho.cpp:101:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
101 | main ()
| ^~~~
mecho.cpp: In function 'int main()':
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 ".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:107:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen (task ".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
7 |
Correct |
84 ms |
7556 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
1356 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
1356 KB |
Output isn't correct |
14 |
Correct |
1 ms |
1356 KB |
Output is correct |
15 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
1356 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
1356 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
1356 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
1356 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
1356 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
1356 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
1356 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
1356 KB |
Output isn't correct |
31 |
Incorrect |
2 ms |
1484 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
1484 KB |
Output isn't correct |
33 |
Incorrect |
5 ms |
3660 KB |
Output isn't correct |
34 |
Incorrect |
5 ms |
3660 KB |
Output isn't correct |
35 |
Incorrect |
19 ms |
3660 KB |
Output isn't correct |
36 |
Incorrect |
6 ms |
4172 KB |
Output isn't correct |
37 |
Incorrect |
6 ms |
4172 KB |
Output isn't correct |
38 |
Incorrect |
24 ms |
4240 KB |
Output isn't correct |
39 |
Incorrect |
6 ms |
4556 KB |
Output isn't correct |
40 |
Incorrect |
7 ms |
4580 KB |
Output isn't correct |
41 |
Incorrect |
39 ms |
4684 KB |
Output isn't correct |
42 |
Incorrect |
9 ms |
4940 KB |
Output isn't correct |
43 |
Incorrect |
10 ms |
5004 KB |
Output isn't correct |
44 |
Incorrect |
56 ms |
5008 KB |
Output isn't correct |
45 |
Incorrect |
10 ms |
5324 KB |
Output isn't correct |
46 |
Incorrect |
10 ms |
5396 KB |
Output isn't correct |
47 |
Incorrect |
46 ms |
5416 KB |
Output isn't correct |
48 |
Incorrect |
11 ms |
5792 KB |
Output isn't correct |
49 |
Incorrect |
10 ms |
5708 KB |
Output isn't correct |
50 |
Incorrect |
63 ms |
5708 KB |
Output isn't correct |
51 |
Incorrect |
13 ms |
6092 KB |
Output isn't correct |
52 |
Incorrect |
12 ms |
6092 KB |
Output isn't correct |
53 |
Incorrect |
101 ms |
6196 KB |
Output isn't correct |
54 |
Incorrect |
17 ms |
6528 KB |
Output isn't correct |
55 |
Incorrect |
18 ms |
6572 KB |
Output isn't correct |
56 |
Incorrect |
81 ms |
6580 KB |
Output isn't correct |
57 |
Incorrect |
15 ms |
6860 KB |
Output isn't correct |
58 |
Incorrect |
15 ms |
6964 KB |
Output isn't correct |
59 |
Incorrect |
99 ms |
6968 KB |
Output isn't correct |
60 |
Incorrect |
19 ms |
7244 KB |
Output isn't correct |
61 |
Incorrect |
22 ms |
7208 KB |
Output isn't correct |
62 |
Incorrect |
112 ms |
7352 KB |
Output isn't correct |
63 |
Incorrect |
108 ms |
7236 KB |
Output isn't correct |
64 |
Incorrect |
160 ms |
7356 KB |
Output isn't correct |
65 |
Incorrect |
147 ms |
7340 KB |
Output isn't correct |
66 |
Incorrect |
153 ms |
7340 KB |
Output isn't correct |
67 |
Correct |
107 ms |
7228 KB |
Output is correct |
68 |
Incorrect |
43 ms |
7392 KB |
Output isn't correct |
69 |
Incorrect |
44 ms |
7396 KB |
Output isn't correct |
70 |
Incorrect |
40 ms |
7364 KB |
Output isn't correct |
71 |
Incorrect |
36 ms |
7404 KB |
Output isn't correct |
72 |
Incorrect |
30 ms |
7388 KB |
Output isn't correct |
73 |
Incorrect |
34 ms |
7780 KB |
Output isn't correct |
74 |
Incorrect |
59 ms |
7888 KB |
Output isn't correct |
75 |
Incorrect |
61 ms |
7884 KB |
Output isn't correct |
76 |
Incorrect |
61 ms |
7904 KB |
Output isn't correct |
77 |
Incorrect |
65 ms |
7892 KB |
Output isn't correct |
78 |
Correct |
66 ms |
7836 KB |
Output is correct |
79 |
Incorrect |
53 ms |
7812 KB |
Output isn't correct |
80 |
Incorrect |
62 ms |
7828 KB |
Output isn't correct |
81 |
Incorrect |
67 ms |
7756 KB |
Output isn't correct |
82 |
Incorrect |
47 ms |
7724 KB |
Output isn't correct |
83 |
Incorrect |
81 ms |
7748 KB |
Output isn't correct |
84 |
Incorrect |
79 ms |
7640 KB |
Output isn't correct |
85 |
Incorrect |
93 ms |
7668 KB |
Output isn't correct |
86 |
Incorrect |
69 ms |
7752 KB |
Output isn't correct |
87 |
Incorrect |
68 ms |
7876 KB |
Output isn't correct |
88 |
Incorrect |
73 ms |
7544 KB |
Output isn't correct |
89 |
Incorrect |
75 ms |
7660 KB |
Output isn't correct |
90 |
Incorrect |
107 ms |
7652 KB |
Output isn't correct |
91 |
Incorrect |
73 ms |
7652 KB |
Output isn't correct |
92 |
Incorrect |
74 ms |
7620 KB |
Output isn't correct |