#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#if 0
const int MOD = ;
inline int lgput(int a, int b) {
int ans = 1;
while(b > 0) {
if(b & 1) ans = (1LL * ans * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return ans;
}
inline void mod(int &x) {
if(x >= MOD)
x -= MOD;
}
inline void add(int &x, int y) {
x += y;
mod(x);
}
inline void sub(int &x, int y) {
x += MOD - y;
mod(x);
}
inline void mul(int &x, int y) {
x = (1LL * x * y) % MOD;
}
inline int inv(int x) {
return lgput(x, MOD - 2);
}
#endif
#if 0
int fact[], invfact[];
inline void prec(int n) {
fact[0] = 1;
for(int i = 1; i <= n; i++) {
fact[i] = (1LL * fact[i - 1] * i) % MOD;
}
invfact[n] = lgput(fact[n], MOD - 2);
for(int i = n - 1; i >= 0; i--) {
invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
}
}
inline int comb(int n, int k) {
if(n < k) return 0;
return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif
using namespace std;
const int INF = 1e9;
const int MAXN = 800;
char mat[MAXN + 1][MAXN + 1];
int dl[] = {-1, 0, 1, 0}, dc[] = {0, -1, 0, 1}, n;
bool in(int l, int c) {
return l > 0 && c > 0 && l <= n && c <= n;
}
int main() {
#if 0
ifstream cin("A.in");
ofstream cout("A.out");
#endif
int i, j, s;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> s;
int lm, cm, ld, cd;
for(i = 1; i <= n; i++) {
cin >> mat[i] + 1;
for(j = 1; j <= n; j++) {
if(mat[i][j] == 'M') {
lm = i, cm = j;
}
if(mat[i][j] == 'D') {
ld = i, cd = j;
}
}
}
auto bfs = [&](vector < vector <int> > &dst, char ch) -> void {
queue < pair <int, int> > Q;
int i, j;
for(i = 1; i <= n; i++) {
for(j = 1; j <= n; j++) {
dst[i][j] = INF;
if(mat[i][j] == ch) {
Q.push({i, j});
dst[i][j] = 0;
}
}
}
while(Q.size()) {
auto cur = Q.front();
Q.pop();
for(int i = 0; i < 4; i++) {
int l = cur.first + dl[i], c = cur.second + dc[i];
if(in(l, c) && dst[l][c] == INF && (mat[l][c] == 'G' || mat[l][c] == 'M')) {
dst[l][c] = dst[cur.first][cur.second] + 1;
Q.push({l, c});
}
}
}
};
vector < vector <int> > H(n + 1, vector <int>(n + 1));
vector < vector <int> > M(n + 1, vector <int>(n + 1));
bfs(H, 'H');
auto check = [&](int lim) -> bool {
int i, j;
for(i = 1; i <= n; i++) {
for(j = 1; j <= n; j++) {
M[i][j] = INF;
}
}
if(lim >= H[lm][cm]) {
return 0;
}
queue < pair <int, int> > Q; Q.push({lm, cm});
M[lm][cm] = 0;
while(Q.size()) {
auto cur = Q.front();
Q.pop();
for(int i = 0; i < 4; i++) {
int l = cur.first + dl[i], c = cur.second + dc[i];
int d = (M[cur.first][cur.second] + 1) / s + lim;
if(in(l, c) && M[l][c] == INF && d < H[l][c] && (mat[l][c] == 'G' || mat[l][c] == 'D')) {
Q.push({l, c});
M[l][c] = M[cur.first][cur.second] + 1;
}
}
}
return M[ld][cd] != INF;
};
int res = 0;
for(int step = 1 << 19; step; step >>= 1) {
if(check(res + step)) {
res += step;
}
}
cout << (check(res) ? res : -1);
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:88:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
cin >> mat[i] + 1;
~~~~~~~^~~
mecho.cpp:152:24: warning: 'cd' may be used uninitialized in this function [-Wmaybe-uninitialized]
return M[ld][cd] != INF;
^
mecho.cpp:152:20: warning: 'ld' may be used uninitialized in this function [-Wmaybe-uninitialized]
return M[ld][cd] != INF;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
132 ms |
6904 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
248 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
504 KB |
Output is correct |
27 |
Correct |
2 ms |
504 KB |
Output is correct |
28 |
Correct |
2 ms |
504 KB |
Output is correct |
29 |
Correct |
3 ms |
504 KB |
Output is correct |
30 |
Correct |
3 ms |
504 KB |
Output is correct |
31 |
Correct |
2 ms |
504 KB |
Output is correct |
32 |
Correct |
2 ms |
508 KB |
Output is correct |
33 |
Correct |
8 ms |
1656 KB |
Output is correct |
34 |
Correct |
9 ms |
1656 KB |
Output is correct |
35 |
Correct |
33 ms |
1784 KB |
Output is correct |
36 |
Correct |
9 ms |
2064 KB |
Output is correct |
37 |
Correct |
10 ms |
2040 KB |
Output is correct |
38 |
Correct |
45 ms |
2296 KB |
Output is correct |
39 |
Correct |
11 ms |
2552 KB |
Output is correct |
40 |
Correct |
12 ms |
2552 KB |
Output is correct |
41 |
Correct |
58 ms |
2552 KB |
Output is correct |
42 |
Correct |
15 ms |
2936 KB |
Output is correct |
43 |
Correct |
16 ms |
2936 KB |
Output is correct |
44 |
Correct |
78 ms |
2936 KB |
Output is correct |
45 |
Correct |
15 ms |
3452 KB |
Output is correct |
46 |
Correct |
16 ms |
3448 KB |
Output is correct |
47 |
Correct |
93 ms |
3580 KB |
Output is correct |
48 |
Correct |
19 ms |
4088 KB |
Output is correct |
49 |
Correct |
19 ms |
4088 KB |
Output is correct |
50 |
Correct |
123 ms |
4188 KB |
Output is correct |
51 |
Correct |
21 ms |
4572 KB |
Output is correct |
52 |
Correct |
23 ms |
4600 KB |
Output is correct |
53 |
Correct |
156 ms |
4696 KB |
Output is correct |
54 |
Correct |
28 ms |
5240 KB |
Output is correct |
55 |
Correct |
30 ms |
5240 KB |
Output is correct |
56 |
Correct |
165 ms |
5340 KB |
Output is correct |
57 |
Correct |
25 ms |
5880 KB |
Output is correct |
58 |
Correct |
25 ms |
6008 KB |
Output is correct |
59 |
Correct |
198 ms |
6136 KB |
Output is correct |
60 |
Correct |
30 ms |
6648 KB |
Output is correct |
61 |
Correct |
31 ms |
6652 KB |
Output is correct |
62 |
Correct |
237 ms |
6648 KB |
Output is correct |
63 |
Correct |
175 ms |
6648 KB |
Output is correct |
64 |
Correct |
291 ms |
6648 KB |
Output is correct |
65 |
Correct |
278 ms |
6648 KB |
Output is correct |
66 |
Correct |
230 ms |
6648 KB |
Output is correct |
67 |
Correct |
188 ms |
6648 KB |
Output is correct |
68 |
Correct |
79 ms |
6648 KB |
Output is correct |
69 |
Correct |
72 ms |
6872 KB |
Output is correct |
70 |
Correct |
63 ms |
6652 KB |
Output is correct |
71 |
Correct |
63 ms |
6776 KB |
Output is correct |
72 |
Correct |
47 ms |
6776 KB |
Output is correct |
73 |
Correct |
64 ms |
6904 KB |
Output is correct |
74 |
Correct |
94 ms |
6908 KB |
Output is correct |
75 |
Correct |
97 ms |
6904 KB |
Output is correct |
76 |
Correct |
89 ms |
6904 KB |
Output is correct |
77 |
Correct |
96 ms |
6904 KB |
Output is correct |
78 |
Correct |
121 ms |
6912 KB |
Output is correct |
79 |
Correct |
78 ms |
6904 KB |
Output is correct |
80 |
Correct |
92 ms |
6904 KB |
Output is correct |
81 |
Correct |
103 ms |
6912 KB |
Output is correct |
82 |
Correct |
94 ms |
6904 KB |
Output is correct |
83 |
Correct |
135 ms |
6904 KB |
Output is correct |
84 |
Correct |
108 ms |
6948 KB |
Output is correct |
85 |
Correct |
111 ms |
6904 KB |
Output is correct |
86 |
Correct |
123 ms |
6904 KB |
Output is correct |
87 |
Correct |
109 ms |
6904 KB |
Output is correct |
88 |
Correct |
128 ms |
6872 KB |
Output is correct |
89 |
Correct |
124 ms |
7000 KB |
Output is correct |
90 |
Correct |
132 ms |
6848 KB |
Output is correct |
91 |
Correct |
119 ms |
6904 KB |
Output is correct |
92 |
Correct |
128 ms |
6904 KB |
Output is correct |