#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 = 2 * 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 |
1 ms |
292 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 |
300 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
92 ms |
6680 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Incorrect |
1 ms |
684 KB |
Output isn't correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
424 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
424 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
33 |
Incorrect |
18 ms |
2892 KB |
Output isn't correct |
34 |
Incorrect |
24 ms |
2892 KB |
Output isn't correct |
35 |
Correct |
22 ms |
2892 KB |
Output is correct |
36 |
Incorrect |
18 ms |
3220 KB |
Output isn't correct |
37 |
Incorrect |
24 ms |
3244 KB |
Output isn't correct |
38 |
Correct |
29 ms |
3256 KB |
Output is correct |
39 |
Incorrect |
24 ms |
3692 KB |
Output isn't correct |
40 |
Incorrect |
29 ms |
3632 KB |
Output isn't correct |
41 |
Correct |
52 ms |
3680 KB |
Output is correct |
42 |
Incorrect |
38 ms |
4024 KB |
Output isn't correct |
43 |
Incorrect |
37 ms |
4100 KB |
Output isn't correct |
44 |
Correct |
46 ms |
4120 KB |
Output is correct |
45 |
Incorrect |
35 ms |
4428 KB |
Output isn't correct |
46 |
Incorrect |
64 ms |
4420 KB |
Output isn't correct |
47 |
Correct |
62 ms |
4456 KB |
Output is correct |
48 |
Incorrect |
77 ms |
4800 KB |
Output isn't correct |
49 |
Incorrect |
58 ms |
4940 KB |
Output isn't correct |
50 |
Correct |
67 ms |
4876 KB |
Output is correct |
51 |
Incorrect |
49 ms |
5260 KB |
Output isn't correct |
52 |
Incorrect |
67 ms |
5316 KB |
Output isn't correct |
53 |
Correct |
86 ms |
5272 KB |
Output is correct |
54 |
Incorrect |
57 ms |
5660 KB |
Output isn't correct |
55 |
Incorrect |
82 ms |
5700 KB |
Output isn't correct |
56 |
Correct |
88 ms |
5744 KB |
Output is correct |
57 |
Incorrect |
65 ms |
6060 KB |
Output isn't correct |
58 |
Incorrect |
92 ms |
6184 KB |
Output isn't correct |
59 |
Correct |
117 ms |
6272 KB |
Output is correct |
60 |
Incorrect |
75 ms |
6580 KB |
Output isn't correct |
61 |
Incorrect |
120 ms |
6584 KB |
Output isn't correct |
62 |
Correct |
171 ms |
6836 KB |
Output is correct |
63 |
Correct |
112 ms |
6620 KB |
Output is correct |
64 |
Correct |
146 ms |
6580 KB |
Output is correct |
65 |
Correct |
186 ms |
6624 KB |
Output is correct |
66 |
Correct |
117 ms |
6508 KB |
Output is correct |
67 |
Correct |
111 ms |
6596 KB |
Output is correct |
68 |
Correct |
67 ms |
6540 KB |
Output is correct |
69 |
Correct |
59 ms |
6648 KB |
Output is correct |
70 |
Correct |
60 ms |
6628 KB |
Output is correct |
71 |
Correct |
74 ms |
6644 KB |
Output is correct |
72 |
Correct |
48 ms |
6548 KB |
Output is correct |
73 |
Correct |
56 ms |
6872 KB |
Output is correct |
74 |
Correct |
72 ms |
6844 KB |
Output is correct |
75 |
Correct |
94 ms |
6904 KB |
Output is correct |
76 |
Correct |
99 ms |
6832 KB |
Output is correct |
77 |
Correct |
75 ms |
6876 KB |
Output is correct |
78 |
Correct |
86 ms |
6832 KB |
Output is correct |
79 |
Correct |
86 ms |
6868 KB |
Output is correct |
80 |
Correct |
91 ms |
6852 KB |
Output is correct |
81 |
Correct |
111 ms |
6852 KB |
Output is correct |
82 |
Correct |
76 ms |
6792 KB |
Output is correct |
83 |
Correct |
88 ms |
6832 KB |
Output is correct |
84 |
Correct |
85 ms |
6828 KB |
Output is correct |
85 |
Correct |
88 ms |
6832 KB |
Output is correct |
86 |
Correct |
93 ms |
6732 KB |
Output is correct |
87 |
Correct |
87 ms |
6724 KB |
Output is correct |
88 |
Correct |
90 ms |
6664 KB |
Output is correct |
89 |
Correct |
121 ms |
6724 KB |
Output is correct |
90 |
Correct |
124 ms |
6912 KB |
Output is correct |
91 |
Correct |
101 ms |
6724 KB |
Output is correct |
92 |
Correct |
90 ms |
6780 KB |
Output is correct |