#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
int n, k;
bool ok = 0;
int mid;
pair<int,int> sta, en;
vector<string> G;
vector<vector<int>> V;
vector<vector<int>> vis;
queue<pair<pair<int,int>,int>> Q, Q2;
void search2(pair<pair<int,int>,int> now){
if(vis[now.f.f][now.f.s])return;
vis[now.f.f][now.f.s] = 1;
if(now.f.f != n-1){
if(V[now.f.f+1][now.f.s] > (now.s+1)/k+mid){
Q2.push({{now.f.f+1,now.f.s},now.s+1});
}
}
if(now.f.s != n-1){
if(V[now.f.f][now.f.s+1] > (now.s+1)/k+mid){
Q2.push({{now.f.f,now.f.s+1},now.s+1});
}
}
if(now.f.f != 0){
if(V[now.f.f-1][now.f.s] > (now.s+1)/k+mid){
Q2.push({{now.f.f-1,now.f.s},now.s+1});
}
}
if(now.f.s != 0){
if(V[now.f.f][now.f.s-1] > (now.s+1)/k+mid){
Q2.push({{now.f.f,now.f.s-1},now.s+1});
}
}
}
void search(pair<pair<int,int>,int> now){
if(now.f.f < 0 || now.f.s < 0 || now.f.f >= n || now.f.s >= n)return;
if(V[now.f.f][now.f.s] != -1)return;
V[now.f.f][now.f.s] = now.s;
Q.push({{now.f.f-1,now.f.s},now.s+1});
Q.push({{now.f.f+1,now.f.s},now.s+1});
Q.push({{now.f.f,now.f.s-1},now.s+1});
Q.push({{now.f.f,now.f.s+1},now.s+1});
}
int main(){
cin >> n >> k;
G.resize(n);
V.resize(n, vector<int> (n,-1));
for(auto &x : G)cin >> x;
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(G[i][j] == 'T')V[i][j] = -2;
if(G[i][j] == 'M')sta = {i,j};
if(G[i][j] == 'D'){en = {i,j};V[i][j] = 1000000;}
if(G[i][j] == 'H')Q.push({{i,j},0});
}
}
while(!Q.empty()){
auto x = Q.front();
Q.pop();
search(x);
}
//for(int i = 0; i < n; ++i){
//for(int j = 0; j < n; ++j){
//cout << V[i][j] << " ";
//}
//cout << "\n";
//}
int best = -1;
int hi = V[sta.f][sta.s]-1, lo = 0;
while(lo <= hi){
mid = (hi+lo)/2;
vis.clear();
vis.resize(n, vector<int> (n, 0));
Q2.push({sta,0});
ok = 0;
while(!Q2.empty()){
auto x = Q2.front();
Q2.pop();
if(x.f == en){ok = 1;break;}
search2(x);
}
Q2 = queue<pair<pair<int,int>,int>>();
if(ok){lo = mid+1;best = mid;}
else hi = mid-1;
}
cout << best << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
422 ms |
6512 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
416 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
15 ms |
1540 KB |
Output is correct |
34 |
Correct |
16 ms |
1560 KB |
Output is correct |
35 |
Correct |
83 ms |
1628 KB |
Output is correct |
36 |
Correct |
19 ms |
1912 KB |
Output is correct |
37 |
Correct |
22 ms |
1756 KB |
Output is correct |
38 |
Correct |
105 ms |
1976 KB |
Output is correct |
39 |
Correct |
25 ms |
2208 KB |
Output is correct |
40 |
Correct |
26 ms |
2208 KB |
Output is correct |
41 |
Correct |
140 ms |
2204 KB |
Output is correct |
42 |
Correct |
31 ms |
2860 KB |
Output is correct |
43 |
Correct |
32 ms |
2896 KB |
Output is correct |
44 |
Correct |
175 ms |
2884 KB |
Output is correct |
45 |
Correct |
37 ms |
3308 KB |
Output is correct |
46 |
Correct |
39 ms |
3436 KB |
Output is correct |
47 |
Correct |
211 ms |
3324 KB |
Output is correct |
48 |
Correct |
45 ms |
3888 KB |
Output is correct |
49 |
Correct |
47 ms |
3832 KB |
Output is correct |
50 |
Correct |
246 ms |
3820 KB |
Output is correct |
51 |
Correct |
53 ms |
4376 KB |
Output is correct |
52 |
Correct |
55 ms |
4368 KB |
Output is correct |
53 |
Correct |
296 ms |
4464 KB |
Output is correct |
54 |
Correct |
61 ms |
5004 KB |
Output is correct |
55 |
Correct |
64 ms |
5004 KB |
Output is correct |
56 |
Correct |
349 ms |
5028 KB |
Output is correct |
57 |
Correct |
71 ms |
5596 KB |
Output is correct |
58 |
Correct |
75 ms |
5620 KB |
Output is correct |
59 |
Correct |
414 ms |
5652 KB |
Output is correct |
60 |
Correct |
83 ms |
6284 KB |
Output is correct |
61 |
Correct |
83 ms |
6280 KB |
Output is correct |
62 |
Correct |
476 ms |
6264 KB |
Output is correct |
63 |
Correct |
266 ms |
6228 KB |
Output is correct |
64 |
Correct |
407 ms |
6256 KB |
Output is correct |
65 |
Correct |
409 ms |
6228 KB |
Output is correct |
66 |
Correct |
305 ms |
6224 KB |
Output is correct |
67 |
Correct |
280 ms |
6244 KB |
Output is correct |
68 |
Correct |
129 ms |
6264 KB |
Output is correct |
69 |
Correct |
114 ms |
6252 KB |
Output is correct |
70 |
Correct |
110 ms |
6264 KB |
Output is correct |
71 |
Correct |
100 ms |
6264 KB |
Output is correct |
72 |
Correct |
94 ms |
6252 KB |
Output is correct |
73 |
Correct |
155 ms |
6296 KB |
Output is correct |
74 |
Correct |
285 ms |
6352 KB |
Output is correct |
75 |
Correct |
263 ms |
6208 KB |
Output is correct |
76 |
Correct |
294 ms |
6240 KB |
Output is correct |
77 |
Correct |
291 ms |
6328 KB |
Output is correct |
78 |
Correct |
320 ms |
6400 KB |
Output is correct |
79 |
Correct |
250 ms |
6488 KB |
Output is correct |
80 |
Correct |
282 ms |
6304 KB |
Output is correct |
81 |
Correct |
293 ms |
6360 KB |
Output is correct |
82 |
Correct |
260 ms |
6292 KB |
Output is correct |
83 |
Correct |
371 ms |
6400 KB |
Output is correct |
84 |
Correct |
318 ms |
6280 KB |
Output is correct |
85 |
Correct |
322 ms |
6280 KB |
Output is correct |
86 |
Correct |
380 ms |
6228 KB |
Output is correct |
87 |
Correct |
309 ms |
6284 KB |
Output is correct |
88 |
Correct |
408 ms |
6260 KB |
Output is correct |
89 |
Correct |
373 ms |
6280 KB |
Output is correct |
90 |
Correct |
432 ms |
6376 KB |
Output is correct |
91 |
Correct |
411 ms |
6356 KB |
Output is correct |
92 |
Correct |
386 ms |
6544 KB |
Output is correct |