#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define eb emplace_back
#define task ""
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
#define fi first
#define se second
#define pii pair <int, int>
#define tii tuple <int, int, int, int>
using namespace std;
const int nmax = 802;
const int mvx[] = {1, -1, 0, 0};
const int mvy[] = {0, 0, 1, -1};
int n, s;
char a[nmax][nmax];
pii Start;
int reach_time[nmax][nmax];
void Bee_timetable()
{
queue <pii> q;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (a[i][j] == 'H')
{
reach_time[i][j] = 0;
q.emplace(i, j);
}
while(q.size())
{
auto [u, v] = q.front();
q.pop();
for (int k = 0; k < 4; ++k)
{
int x = u + mvx[k];
int y = v + mvy[k];
if (x < 1 || x > n) continue;
if (y < 1 || y > n) continue;
if (a[x][y] == 'D' || a[x][y] == 'T') continue;
if (reach_time[x][y] != 1e9) continue;
reach_time[x][y] = reach_time[u][v] + 1;
q.emplace(x, y);
}
}
}
bool check(int mid)
{
queue <tii> q;
q.emplace(Start.fi, Start.se, mid, s);
vector <vector<bool> > vs(n + 2, vector <bool> (n + 2, 0));
while(q.size())
{
auto [u, v, time, rem_step] = q.front();
q.pop();
if (a[u][v] == 'D') return 1;
if (!rem_step) rem_step = s, ++time;
if (reach_time[u][v] <= time) continue;
for (int k = 0; k < 4; ++k)
{
int x = u + mvx[k];
int y = v + mvy[k];
if (x < 1 || x > n) continue;
if (y < 1 || y > n) continue;
if (vs[x][y]) continue;
if (a[x][y] == 'T') continue;
vs[x][y] = 1;
q.emplace(x, y, time, rem_step - 1);
}
}
return 0;
}
int main()
{
if (ifstream(task".inp")) nx
fast
cin >> n >> s;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
{
cin >> a[i][j];
reach_time[i][j] = 1e9;
if (a[i][j] == 'M') Start = {i, j};
}
Bee_timetable();
int l = 0, r = n * n, ans = -1;
while(l <= r)
{
int mid = l + r >> 1;
if (check(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
cout << ans;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:89:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
89 | int mid = l + r >> 1;
| ~~^~~
mecho.cpp:7:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
7 | #define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:75:31: note: in expansion of macro 'nx'
75 | if (ifstream(task".inp")) nx
| ^~
mecho.cpp:7:52: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
7 | #define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:75:31: note: in expansion of macro 'nx'
75 | if (ifstream(task".inp")) nx
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
516 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
68 ms |
4224 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
2648 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
2648 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
2 ms |
2648 KB |
Output is correct |
28 |
Correct |
1 ms |
2648 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2648 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2648 KB |
Output is correct |
33 |
Correct |
7 ms |
2908 KB |
Output is correct |
34 |
Correct |
6 ms |
3064 KB |
Output is correct |
35 |
Correct |
19 ms |
2904 KB |
Output is correct |
36 |
Correct |
5 ms |
3164 KB |
Output is correct |
37 |
Correct |
4 ms |
3164 KB |
Output is correct |
38 |
Correct |
20 ms |
3160 KB |
Output is correct |
39 |
Correct |
9 ms |
3160 KB |
Output is correct |
40 |
Correct |
6 ms |
3164 KB |
Output is correct |
41 |
Correct |
38 ms |
3300 KB |
Output is correct |
42 |
Correct |
7 ms |
3160 KB |
Output is correct |
43 |
Correct |
7 ms |
3160 KB |
Output is correct |
44 |
Correct |
34 ms |
3164 KB |
Output is correct |
45 |
Correct |
13 ms |
3416 KB |
Output is correct |
46 |
Correct |
9 ms |
3256 KB |
Output is correct |
47 |
Correct |
43 ms |
3420 KB |
Output is correct |
48 |
Correct |
8 ms |
3420 KB |
Output is correct |
49 |
Correct |
14 ms |
3420 KB |
Output is correct |
50 |
Correct |
57 ms |
3588 KB |
Output is correct |
51 |
Correct |
10 ms |
3672 KB |
Output is correct |
52 |
Correct |
10 ms |
3492 KB |
Output is correct |
53 |
Correct |
66 ms |
3696 KB |
Output is correct |
54 |
Correct |
12 ms |
3592 KB |
Output is correct |
55 |
Correct |
20 ms |
3792 KB |
Output is correct |
56 |
Correct |
70 ms |
3676 KB |
Output is correct |
57 |
Correct |
21 ms |
3932 KB |
Output is correct |
58 |
Correct |
13 ms |
3932 KB |
Output is correct |
59 |
Correct |
98 ms |
4024 KB |
Output is correct |
60 |
Correct |
23 ms |
4184 KB |
Output is correct |
61 |
Correct |
18 ms |
4188 KB |
Output is correct |
62 |
Correct |
93 ms |
4236 KB |
Output is correct |
63 |
Correct |
92 ms |
4188 KB |
Output is correct |
64 |
Correct |
161 ms |
4220 KB |
Output is correct |
65 |
Correct |
128 ms |
4216 KB |
Output is correct |
66 |
Correct |
102 ms |
4216 KB |
Output is correct |
67 |
Correct |
125 ms |
4216 KB |
Output is correct |
68 |
Correct |
42 ms |
4184 KB |
Output is correct |
69 |
Correct |
41 ms |
4196 KB |
Output is correct |
70 |
Correct |
29 ms |
4148 KB |
Output is correct |
71 |
Correct |
35 ms |
4184 KB |
Output is correct |
72 |
Correct |
30 ms |
4180 KB |
Output is correct |
73 |
Correct |
28 ms |
4292 KB |
Output is correct |
74 |
Correct |
50 ms |
4188 KB |
Output is correct |
75 |
Correct |
61 ms |
4188 KB |
Output is correct |
76 |
Correct |
49 ms |
4372 KB |
Output is correct |
77 |
Correct |
78 ms |
4184 KB |
Output is correct |
78 |
Correct |
60 ms |
4352 KB |
Output is correct |
79 |
Correct |
38 ms |
4188 KB |
Output is correct |
80 |
Correct |
48 ms |
4352 KB |
Output is correct |
81 |
Correct |
55 ms |
4348 KB |
Output is correct |
82 |
Correct |
52 ms |
4184 KB |
Output is correct |
83 |
Correct |
62 ms |
4152 KB |
Output is correct |
84 |
Correct |
53 ms |
4188 KB |
Output is correct |
85 |
Correct |
66 ms |
4308 KB |
Output is correct |
86 |
Correct |
60 ms |
4312 KB |
Output is correct |
87 |
Correct |
71 ms |
4292 KB |
Output is correct |
88 |
Correct |
65 ms |
4252 KB |
Output is correct |
89 |
Correct |
75 ms |
4252 KB |
Output is correct |
90 |
Correct |
65 ms |
4356 KB |
Output is correct |
91 |
Correct |
61 ms |
4256 KB |
Output is correct |
92 |
Correct |
68 ms |
4184 KB |
Output is correct |