#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i,l,r) for(int i = (l); i < r; i++)
#define per(i,r,l) for(int i = (r); i >= l; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define ss second
const ll mod = 998244353, maxn = 800 + 5, inf = 1e9 + 5;
struct node{
int x, y, rem, eq;
node(){}
node(int x, int y, int r, int e):
x(x), y(y), rem(r), eq(e){}
};
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
bool bad[maxn][maxn], vis[maxn][maxn];
int dist_bee[maxn][maxn];
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, s; cin >> n >> s;
pair<int, int> st, en;
deque<pair<int, int>> hol;
rep(i,0,n){
rep(j,0,n){
dist_bee[i][j] = inf;
char e; cin >> e;
if(e == 'M'){
st = {i, j};
}
if(e == 'D'){
en = {i, j};
}
if(e == 'H'){
hol.push_back({i, j});
dist_bee[i][j] = 0;
}
if(e == 'H' || e == 'T') bad[i][j] = 1;
}
}
while(sz(hol)){
pair<int, int> p = hol.front(); hol.pop_front();
rep(d,0,4){
int i = p.ff + dx[d], j = p.ss + dy[d];
if(i < 0 || i >= n || j < 0 || j >= n || dist_bee[i][j] != inf || bad[i][j]) continue;
dist_bee[i][j] = dist_bee[p.ff][p.ss] + 1;
hol.pb({i, j});
}
}
dist_bee[en.ff][en.ss] = inf;
// rep(i,0,n){
// rep(j,0,n){
// if(dist_bee[i][j] == inf) cout << -1 << ' ';
// else cout << dist_bee[i][j] << ' ';
// }
// cout << endl;
// }
auto chk = [&](int t) -> bool{
memset(vis, 0, sizeof vis);
deque<node> q[2];
bool act = 0;
q[act].push_back(node(st.ff, st.ss, s, t + 1));
vis[st.ff][st.ss] = 1;
while(1){
if(!sz(q[act])){
act ^= 1;
}
if(!sz(q[act])) break;
node p = q[act].front(); q[act].pop_front();
rep(d,0,4){
int i = p.x + dx[d], j = p.y + dy[d];
if(i < 0 || i >= n || j < 0 || j >= n || vis[i][j] || bad[i][j]
|| p.eq > dist_bee[i][j] || (p.rem == 1 && p.eq == dist_bee[i][j])) continue;
node r = node(i, j, p.rem - 1, p.eq);
vis[i][j] = 1;
if(!r.rem){
r.rem = s, r.eq++;
q[!act].pb(r);
}else{
q[act].pb(r);
}
}
}
return vis[en.ff][en.ss];
};
int l = -1, r = 1e6;
while(l + 1 < r){
int mid = (l + r)/2;
if(chk(mid)) l = mid;
else r = mid;
}
cout << l;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
844 KB |
Output is correct |
3 |
Correct |
1 ms |
844 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
107 ms |
4072 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
960 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
2 ms |
972 KB |
Output is correct |
19 |
Correct |
2 ms |
972 KB |
Output is correct |
20 |
Correct |
1 ms |
972 KB |
Output is correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
1 ms |
1100 KB |
Output is correct |
24 |
Correct |
1 ms |
1100 KB |
Output is correct |
25 |
Correct |
1 ms |
1100 KB |
Output is correct |
26 |
Correct |
2 ms |
1100 KB |
Output is correct |
27 |
Correct |
1 ms |
1100 KB |
Output is correct |
28 |
Correct |
2 ms |
1100 KB |
Output is correct |
29 |
Correct |
1 ms |
1228 KB |
Output is correct |
30 |
Correct |
1 ms |
1228 KB |
Output is correct |
31 |
Correct |
2 ms |
1228 KB |
Output is correct |
32 |
Correct |
2 ms |
1228 KB |
Output is correct |
33 |
Correct |
6 ms |
2252 KB |
Output is correct |
34 |
Correct |
7 ms |
2252 KB |
Output is correct |
35 |
Correct |
28 ms |
2252 KB |
Output is correct |
36 |
Correct |
7 ms |
2508 KB |
Output is correct |
37 |
Correct |
7 ms |
2508 KB |
Output is correct |
38 |
Correct |
36 ms |
2416 KB |
Output is correct |
39 |
Correct |
9 ms |
2712 KB |
Output is correct |
40 |
Correct |
9 ms |
2636 KB |
Output is correct |
41 |
Correct |
45 ms |
2720 KB |
Output is correct |
42 |
Correct |
12 ms |
2892 KB |
Output is correct |
43 |
Correct |
12 ms |
2932 KB |
Output is correct |
44 |
Correct |
61 ms |
2932 KB |
Output is correct |
45 |
Correct |
11 ms |
3024 KB |
Output is correct |
46 |
Correct |
11 ms |
3128 KB |
Output is correct |
47 |
Correct |
80 ms |
3140 KB |
Output is correct |
48 |
Correct |
14 ms |
3312 KB |
Output is correct |
49 |
Correct |
19 ms |
3240 KB |
Output is correct |
50 |
Correct |
87 ms |
3308 KB |
Output is correct |
51 |
Correct |
18 ms |
3404 KB |
Output is correct |
52 |
Correct |
17 ms |
3404 KB |
Output is correct |
53 |
Correct |
103 ms |
3520 KB |
Output is correct |
54 |
Correct |
19 ms |
3716 KB |
Output is correct |
55 |
Correct |
25 ms |
3760 KB |
Output is correct |
56 |
Correct |
118 ms |
3840 KB |
Output is correct |
57 |
Correct |
20 ms |
3916 KB |
Output is correct |
58 |
Correct |
19 ms |
3912 KB |
Output is correct |
59 |
Correct |
138 ms |
3904 KB |
Output is correct |
60 |
Correct |
22 ms |
4036 KB |
Output is correct |
61 |
Correct |
22 ms |
4044 KB |
Output is correct |
62 |
Correct |
167 ms |
3988 KB |
Output is correct |
63 |
Correct |
125 ms |
4088 KB |
Output is correct |
64 |
Correct |
188 ms |
4092 KB |
Output is correct |
65 |
Correct |
189 ms |
3984 KB |
Output is correct |
66 |
Correct |
165 ms |
4108 KB |
Output is correct |
67 |
Correct |
132 ms |
4036 KB |
Output is correct |
68 |
Correct |
58 ms |
4108 KB |
Output is correct |
69 |
Correct |
56 ms |
4104 KB |
Output is correct |
70 |
Correct |
50 ms |
4036 KB |
Output is correct |
71 |
Correct |
44 ms |
4008 KB |
Output is correct |
72 |
Incorrect |
38 ms |
4040 KB |
Output isn't correct |
73 |
Incorrect |
39 ms |
4256 KB |
Output isn't correct |
74 |
Correct |
74 ms |
4352 KB |
Output is correct |
75 |
Correct |
81 ms |
4360 KB |
Output is correct |
76 |
Correct |
75 ms |
4368 KB |
Output is correct |
77 |
Correct |
85 ms |
4292 KB |
Output is correct |
78 |
Correct |
109 ms |
4220 KB |
Output is correct |
79 |
Correct |
69 ms |
4348 KB |
Output is correct |
80 |
Correct |
76 ms |
4336 KB |
Output is correct |
81 |
Correct |
86 ms |
4292 KB |
Output is correct |
82 |
Correct |
68 ms |
4328 KB |
Output is correct |
83 |
Correct |
100 ms |
4320 KB |
Output is correct |
84 |
Correct |
93 ms |
4204 KB |
Output is correct |
85 |
Correct |
85 ms |
4228 KB |
Output is correct |
86 |
Correct |
96 ms |
4380 KB |
Output is correct |
87 |
Correct |
98 ms |
4300 KB |
Output is correct |
88 |
Correct |
131 ms |
4240 KB |
Output is correct |
89 |
Correct |
106 ms |
4248 KB |
Output is correct |
90 |
Correct |
114 ms |
4164 KB |
Output is correct |
91 |
Correct |
106 ms |
4176 KB |
Output is correct |
92 |
Correct |
108 ms |
4144 KB |
Output is correct |