//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 810;
const double eps = 1e-9;
int a[MAXN][MAXN], vis[MAXN][MAXN];
int n, s, dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
char str[MAXN][MAXN];
pii st = mp(0, 0), en = mp(0, 0);
bool in(int x, int y) {
if (x < 1 || x > n || y < 1 || y > n)
return 0;
return 1;
}
bool ok(int t) {
mem(vis, 0x3f);
deque<pii> pq;
pq.pb(st);
vis[st.fi][st.se] = 0;
while (!pq.empty()) {
pii cur = pq.front(); pq.pop_front();
if (cur == en)
return 1;
for (int i = 0; i < 4; i++) {
int nx = cur.fi + dx[i];
int ny = cur.se + dy[i];
if (!in(nx, ny) || vis[cur.fi][cur.se] / s + t >= a[cur.fi][cur.se])
continue;
if (vis[nx][ny] > vis[cur.fi][cur.se] + 1) {
vis[nx][ny] = vis[cur.fi][cur.se] + 1;
pq.pb(mp(nx, ny));
}
}
}
return 0;
}
int main() {
mem(a, -1);
scanf("%d %d", &n, &s);
deque<pii> pq;
for (int i = 1; i <= n; i++) {
scanf("%s", str[i] + 1);
for (int j = 1; j <= n; j++) {
if (str[i][j] == 'G' || str[i][j] == 'M' || str[i][j] == 'D')
a[i][j] = INF;
if (str[i][j] == 'M')
st = mp(i, j);
if (str[i][j] == 'D')
en = mp(i, j);
if (str[i][j] == 'H') {
pq.pb(mp(i, j));
a[i][j] = 0;
}
}
}
while (!pq.empty()) {
pii cur = pq.front(); pq.pop_front();
for (int i = 0; i < 4; i++) {
int nx = cur.fi + dx[i];
int ny = cur.se + dy[i];
if (!in(nx, ny) || str[nx][ny] == 'D')
continue;
if (a[nx][ny] > a[cur.fi][cur.se] + 1) {
a[nx][ny] = a[cur.fi][cur.se] + 1;
pq.pb(mp(nx, ny));
}
}
}
int lo = 0, hi = n * n;
while (lo <= hi) {
int mi = (lo+hi) / 2;
if (ok(mi))
lo = mi + 1;
else
hi = mi - 1;
}
pri(lo - 1);
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &s);
~~~~~^~~~~~~~~~~~~~~~~
mecho.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", str[i] + 1);
~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5504 KB |
Output is correct |
2 |
Correct |
7 ms |
5504 KB |
Output is correct |
3 |
Correct |
7 ms |
5504 KB |
Output is correct |
4 |
Correct |
6 ms |
5504 KB |
Output is correct |
5 |
Correct |
7 ms |
5504 KB |
Output is correct |
6 |
Correct |
7 ms |
5504 KB |
Output is correct |
7 |
Correct |
122 ms |
7004 KB |
Output is correct |
8 |
Correct |
10 ms |
5504 KB |
Output is correct |
9 |
Correct |
7 ms |
5504 KB |
Output is correct |
10 |
Correct |
7 ms |
5504 KB |
Output is correct |
11 |
Correct |
7 ms |
5760 KB |
Output is correct |
12 |
Correct |
8 ms |
5504 KB |
Output is correct |
13 |
Correct |
8 ms |
5504 KB |
Output is correct |
14 |
Correct |
8 ms |
5504 KB |
Output is correct |
15 |
Correct |
7 ms |
5504 KB |
Output is correct |
16 |
Correct |
7 ms |
5476 KB |
Output is correct |
17 |
Correct |
7 ms |
5504 KB |
Output is correct |
18 |
Correct |
8 ms |
5504 KB |
Output is correct |
19 |
Correct |
7 ms |
5504 KB |
Output is correct |
20 |
Correct |
7 ms |
5504 KB |
Output is correct |
21 |
Correct |
7 ms |
5504 KB |
Output is correct |
22 |
Correct |
7 ms |
5492 KB |
Output is correct |
23 |
Correct |
7 ms |
5504 KB |
Output is correct |
24 |
Correct |
7 ms |
5504 KB |
Output is correct |
25 |
Correct |
7 ms |
5504 KB |
Output is correct |
26 |
Correct |
7 ms |
5504 KB |
Output is correct |
27 |
Correct |
8 ms |
5596 KB |
Output is correct |
28 |
Correct |
7 ms |
5504 KB |
Output is correct |
29 |
Correct |
7 ms |
5504 KB |
Output is correct |
30 |
Correct |
8 ms |
5504 KB |
Output is correct |
31 |
Correct |
9 ms |
5504 KB |
Output is correct |
32 |
Correct |
8 ms |
5504 KB |
Output is correct |
33 |
Correct |
11 ms |
6016 KB |
Output is correct |
34 |
Correct |
12 ms |
5888 KB |
Output is correct |
35 |
Correct |
40 ms |
5888 KB |
Output is correct |
36 |
Correct |
12 ms |
6016 KB |
Output is correct |
37 |
Correct |
11 ms |
6016 KB |
Output is correct |
38 |
Correct |
44 ms |
6012 KB |
Output is correct |
39 |
Correct |
12 ms |
6016 KB |
Output is correct |
40 |
Correct |
13 ms |
6016 KB |
Output is correct |
41 |
Correct |
65 ms |
6016 KB |
Output is correct |
42 |
Correct |
12 ms |
6136 KB |
Output is correct |
43 |
Correct |
14 ms |
6144 KB |
Output is correct |
44 |
Correct |
87 ms |
6144 KB |
Output is correct |
45 |
Correct |
14 ms |
6144 KB |
Output is correct |
46 |
Correct |
14 ms |
6144 KB |
Output is correct |
47 |
Correct |
87 ms |
6272 KB |
Output is correct |
48 |
Correct |
14 ms |
6392 KB |
Output is correct |
49 |
Correct |
15 ms |
6272 KB |
Output is correct |
50 |
Correct |
116 ms |
6272 KB |
Output is correct |
51 |
Correct |
16 ms |
6400 KB |
Output is correct |
52 |
Correct |
19 ms |
6400 KB |
Output is correct |
53 |
Correct |
134 ms |
6516 KB |
Output is correct |
54 |
Correct |
17 ms |
6528 KB |
Output is correct |
55 |
Correct |
19 ms |
6528 KB |
Output is correct |
56 |
Correct |
159 ms |
6620 KB |
Output is correct |
57 |
Correct |
21 ms |
6656 KB |
Output is correct |
58 |
Correct |
20 ms |
6652 KB |
Output is correct |
59 |
Correct |
192 ms |
6776 KB |
Output is correct |
60 |
Correct |
22 ms |
6784 KB |
Output is correct |
61 |
Correct |
20 ms |
6784 KB |
Output is correct |
62 |
Correct |
219 ms |
6748 KB |
Output is correct |
63 |
Correct |
168 ms |
6784 KB |
Output is correct |
64 |
Correct |
333 ms |
6904 KB |
Output is correct |
65 |
Correct |
240 ms |
6776 KB |
Output is correct |
66 |
Correct |
191 ms |
6904 KB |
Output is correct |
67 |
Correct |
180 ms |
6776 KB |
Output is correct |
68 |
Correct |
60 ms |
6776 KB |
Output is correct |
69 |
Correct |
60 ms |
6776 KB |
Output is correct |
70 |
Correct |
44 ms |
6776 KB |
Output is correct |
71 |
Correct |
54 ms |
6784 KB |
Output is correct |
72 |
Correct |
31 ms |
6776 KB |
Output is correct |
73 |
Correct |
36 ms |
7160 KB |
Output is correct |
74 |
Correct |
69 ms |
7032 KB |
Output is correct |
75 |
Correct |
85 ms |
7032 KB |
Output is correct |
76 |
Correct |
80 ms |
7032 KB |
Output is correct |
77 |
Correct |
77 ms |
7040 KB |
Output is correct |
78 |
Correct |
101 ms |
7160 KB |
Output is correct |
79 |
Correct |
59 ms |
7032 KB |
Output is correct |
80 |
Correct |
74 ms |
7032 KB |
Output is correct |
81 |
Correct |
87 ms |
7024 KB |
Output is correct |
82 |
Correct |
72 ms |
7008 KB |
Output is correct |
83 |
Correct |
95 ms |
6904 KB |
Output is correct |
84 |
Correct |
86 ms |
6904 KB |
Output is correct |
85 |
Correct |
114 ms |
6904 KB |
Output is correct |
86 |
Correct |
110 ms |
6988 KB |
Output is correct |
87 |
Correct |
114 ms |
6988 KB |
Output is correct |
88 |
Correct |
100 ms |
6984 KB |
Output is correct |
89 |
Correct |
106 ms |
7032 KB |
Output is correct |
90 |
Correct |
121 ms |
6936 KB |
Output is correct |
91 |
Correct |
121 ms |
6904 KB |
Output is correct |
92 |
Correct |
124 ms |
6948 KB |
Output is correct |