#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/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/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/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/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 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
256 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
256 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
424 ms |
6512 KB |
Output is correct |
8 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Incorrect |
1 ms |
436 KB |
Output isn't correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Incorrect |
15 ms |
1520 KB |
Output isn't correct |
34 |
Correct |
17 ms |
1532 KB |
Output is correct |
35 |
Correct |
77 ms |
1516 KB |
Output is correct |
36 |
Incorrect |
19 ms |
1856 KB |
Output isn't correct |
37 |
Correct |
22 ms |
1876 KB |
Output is correct |
38 |
Correct |
102 ms |
1824 KB |
Output is correct |
39 |
Incorrect |
27 ms |
2396 KB |
Output isn't correct |
40 |
Correct |
28 ms |
2220 KB |
Output is correct |
41 |
Correct |
140 ms |
2220 KB |
Output is correct |
42 |
Incorrect |
30 ms |
2896 KB |
Output isn't correct |
43 |
Correct |
32 ms |
2904 KB |
Output is correct |
44 |
Correct |
173 ms |
2980 KB |
Output is correct |
45 |
Incorrect |
38 ms |
3356 KB |
Output isn't correct |
46 |
Correct |
39 ms |
3384 KB |
Output is correct |
47 |
Correct |
211 ms |
3324 KB |
Output is correct |
48 |
Incorrect |
44 ms |
3864 KB |
Output isn't correct |
49 |
Correct |
47 ms |
3812 KB |
Output is correct |
50 |
Correct |
252 ms |
3832 KB |
Output is correct |
51 |
Incorrect |
53 ms |
4360 KB |
Output isn't correct |
52 |
Correct |
56 ms |
4352 KB |
Output is correct |
53 |
Correct |
311 ms |
4344 KB |
Output is correct |
54 |
Incorrect |
61 ms |
5004 KB |
Output isn't correct |
55 |
Correct |
62 ms |
5016 KB |
Output is correct |
56 |
Correct |
361 ms |
5160 KB |
Output is correct |
57 |
Incorrect |
72 ms |
5596 KB |
Output isn't correct |
58 |
Correct |
74 ms |
5588 KB |
Output is correct |
59 |
Correct |
399 ms |
5648 KB |
Output is correct |
60 |
Incorrect |
81 ms |
6292 KB |
Output isn't correct |
61 |
Correct |
84 ms |
6288 KB |
Output is correct |
62 |
Correct |
474 ms |
6256 KB |
Output is correct |
63 |
Correct |
268 ms |
6280 KB |
Output is correct |
64 |
Correct |
404 ms |
6372 KB |
Output is correct |
65 |
Correct |
410 ms |
6528 KB |
Output is correct |
66 |
Incorrect |
321 ms |
6284 KB |
Output isn't correct |
67 |
Correct |
290 ms |
6248 KB |
Output is correct |
68 |
Correct |
134 ms |
6264 KB |
Output is correct |
69 |
Correct |
117 ms |
6252 KB |
Output is correct |
70 |
Correct |
112 ms |
6200 KB |
Output is correct |
71 |
Correct |
94 ms |
6328 KB |
Output is correct |
72 |
Correct |
94 ms |
6264 KB |
Output is correct |
73 |
Correct |
158 ms |
6196 KB |
Output is correct |
74 |
Correct |
295 ms |
6384 KB |
Output is correct |
75 |
Correct |
269 ms |
6336 KB |
Output is correct |
76 |
Correct |
278 ms |
6324 KB |
Output is correct |
77 |
Correct |
317 ms |
6348 KB |
Output is correct |
78 |
Correct |
328 ms |
6308 KB |
Output is correct |
79 |
Correct |
248 ms |
6340 KB |
Output is correct |
80 |
Correct |
276 ms |
6412 KB |
Output is correct |
81 |
Correct |
292 ms |
6280 KB |
Output is correct |
82 |
Correct |
270 ms |
6276 KB |
Output is correct |
83 |
Correct |
380 ms |
6348 KB |
Output is correct |
84 |
Correct |
309 ms |
6408 KB |
Output is correct |
85 |
Correct |
304 ms |
6340 KB |
Output is correct |
86 |
Correct |
376 ms |
6376 KB |
Output is correct |
87 |
Correct |
328 ms |
6532 KB |
Output is correct |
88 |
Correct |
384 ms |
6540 KB |
Output is correct |
89 |
Correct |
382 ms |
6352 KB |
Output is correct |
90 |
Correct |
413 ms |
6432 KB |
Output is correct |
91 |
Correct |
404 ms |
6556 KB |
Output is correct |
92 |
Correct |
386 ms |
6292 KB |
Output is correct |