#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
template<typename T> bool chkmin(T &a, T b){return b < a ? a = b, 1 : 0;}
template<typename T> bool chkmax(T &a, T b){return b > a ? a = b, 1 : 0;}
typedef pair<int, int> pii;
typedef long long ll;
const int MAXN = 810, inf = 1e9;
const int mv[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int n, s, mx, my, dx, dy, d1[MAXN][MAXN], d2[MAXN][MAXN];
char g[MAXN][MAXN];
// bfs
bool valid(int x, int y){
return x >= 1 && x <= n && y >= 1 && y <= n;
}
void bfs1(){
queue<pii> q;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(g[i][j] == 'H')
d1[i][j] = 0, q.push(mp(i, j));
else d1[i][j] = inf;
while(!q.empty()){
pii u = q.front(); q.pop();
for(int k = 0; k < 4; k++){
int x = u.fi + mv[k][0];
int y = u.se + mv[k][1];
if(valid(x, y) && g[x][y] != 'T' && g[x][y] != 'D' && d1[x][y] == inf){
d1[x][y] = d1[u.fi][u.se] + 1;
q.push(mp(x, y));
}
}
}
}
bool check(int t){
queue<pii> q;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
d2[i][j] = inf;
if(t < d1[mx][my])
d2[mx][my] = 0, q.push(mp(mx, my));
while(!q.empty()){
pii u = q.front(); q.pop();
for(int k = 0; k < 4; k++){
int x = u.fi + mv[k][0];
int y = u.se + mv[k][1];
if(valid(x, y) && g[x][y] != 'T' && d2[x][y] == inf && (t + (d2[u.fi][u.se] + 1) / s < d1[x][y])){
d2[x][y] = d2[u.fi][u.se] + 1;
q.push(mp(x, y));
}
}
}
return d2[dx][dy] != inf;
}
int main(){
// input
scanf("%d %d", &n, &s);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++){
scanf(" %c", &g[i][j]);
if(g[i][j] == 'M')
mx = i, my = j;
if(g[i][j] == 'D')
dx = i, dy = j;
}
// prework(bfs)
bfs1();
// binary search
int l = -1, r = n * n, mid;
while(l < r){
mid = (l + r + 1) / 2;
if(check(mid))
l = mid;
else r = mid - 1;
}
printf("%d\n", l);
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:66:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d %d", &n, &s);
| ~~~~~^~~~~~~~~~~~~~~~~
mecho.cpp:69:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf(" %c", &g[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
94 ms |
6008 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
0 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
716 KB |
Output is correct |
26 |
Correct |
1 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
716 KB |
Output is correct |
28 |
Correct |
1 ms |
716 KB |
Output is correct |
29 |
Correct |
1 ms |
716 KB |
Output is correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Correct |
1 ms |
844 KB |
Output is correct |
32 |
Correct |
1 ms |
844 KB |
Output is correct |
33 |
Correct |
9 ms |
2752 KB |
Output is correct |
34 |
Correct |
9 ms |
2712 KB |
Output is correct |
35 |
Correct |
22 ms |
2800 KB |
Output is correct |
36 |
Correct |
11 ms |
3096 KB |
Output is correct |
37 |
Correct |
12 ms |
3076 KB |
Output is correct |
38 |
Correct |
28 ms |
3144 KB |
Output is correct |
39 |
Correct |
14 ms |
3512 KB |
Output is correct |
40 |
Correct |
15 ms |
3512 KB |
Output is correct |
41 |
Correct |
38 ms |
3404 KB |
Output is correct |
42 |
Correct |
17 ms |
3872 KB |
Output is correct |
43 |
Correct |
18 ms |
3820 KB |
Output is correct |
44 |
Correct |
51 ms |
3864 KB |
Output is correct |
45 |
Correct |
20 ms |
4164 KB |
Output is correct |
46 |
Correct |
23 ms |
4192 KB |
Output is correct |
47 |
Correct |
54 ms |
4172 KB |
Output is correct |
48 |
Correct |
23 ms |
4524 KB |
Output is correct |
49 |
Correct |
26 ms |
4568 KB |
Output is correct |
50 |
Correct |
68 ms |
4572 KB |
Output is correct |
51 |
Correct |
33 ms |
4932 KB |
Output is correct |
52 |
Correct |
31 ms |
4820 KB |
Output is correct |
53 |
Correct |
85 ms |
4928 KB |
Output is correct |
54 |
Correct |
32 ms |
5244 KB |
Output is correct |
55 |
Correct |
35 ms |
5236 KB |
Output is correct |
56 |
Correct |
93 ms |
5280 KB |
Output is correct |
57 |
Correct |
38 ms |
5632 KB |
Output is correct |
58 |
Correct |
40 ms |
5512 KB |
Output is correct |
59 |
Correct |
113 ms |
5700 KB |
Output is correct |
60 |
Correct |
42 ms |
5964 KB |
Output is correct |
61 |
Correct |
44 ms |
5984 KB |
Output is correct |
62 |
Correct |
121 ms |
6000 KB |
Output is correct |
63 |
Correct |
108 ms |
5992 KB |
Output is correct |
64 |
Correct |
188 ms |
5988 KB |
Output is correct |
65 |
Correct |
143 ms |
6072 KB |
Output is correct |
66 |
Correct |
121 ms |
5888 KB |
Output is correct |
67 |
Correct |
118 ms |
5900 KB |
Output is correct |
68 |
Correct |
64 ms |
6020 KB |
Output is correct |
69 |
Correct |
68 ms |
6068 KB |
Output is correct |
70 |
Correct |
56 ms |
5912 KB |
Output is correct |
71 |
Correct |
60 ms |
6020 KB |
Output is correct |
72 |
Correct |
49 ms |
5948 KB |
Output is correct |
73 |
Correct |
52 ms |
6268 KB |
Output is correct |
74 |
Correct |
71 ms |
6224 KB |
Output is correct |
75 |
Correct |
89 ms |
6268 KB |
Output is correct |
76 |
Correct |
77 ms |
6228 KB |
Output is correct |
77 |
Correct |
73 ms |
6268 KB |
Output is correct |
78 |
Correct |
83 ms |
6136 KB |
Output is correct |
79 |
Correct |
66 ms |
6244 KB |
Output is correct |
80 |
Correct |
70 ms |
6236 KB |
Output is correct |
81 |
Correct |
76 ms |
6152 KB |
Output is correct |
82 |
Correct |
76 ms |
6212 KB |
Output is correct |
83 |
Correct |
91 ms |
6140 KB |
Output is correct |
84 |
Correct |
78 ms |
6204 KB |
Output is correct |
85 |
Correct |
87 ms |
6084 KB |
Output is correct |
86 |
Correct |
82 ms |
6084 KB |
Output is correct |
87 |
Correct |
83 ms |
6200 KB |
Output is correct |
88 |
Correct |
93 ms |
6100 KB |
Output is correct |
89 |
Correct |
88 ms |
6152 KB |
Output is correct |
90 |
Correct |
87 ms |
6048 KB |
Output is correct |
91 |
Correct |
88 ms |
6044 KB |
Output is correct |
92 |
Correct |
95 ms |
6056 KB |
Output is correct |