#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 = 1000000, 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 |
384 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
384 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 |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
423 ms |
7072 KB |
Output is correct |
8 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
384 KB |
Output isn't 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 |
384 KB |
Output isn't correct |
32 |
Correct |
2 ms |
384 KB |
Output is correct |
33 |
Incorrect |
16 ms |
1648 KB |
Output isn't correct |
34 |
Correct |
17 ms |
1648 KB |
Output is correct |
35 |
Correct |
85 ms |
1636 KB |
Output is correct |
36 |
Incorrect |
21 ms |
2004 KB |
Output isn't correct |
37 |
Correct |
23 ms |
2064 KB |
Output is correct |
38 |
Correct |
111 ms |
2020 KB |
Output is correct |
39 |
Incorrect |
27 ms |
2404 KB |
Output isn't correct |
40 |
Correct |
26 ms |
2468 KB |
Output is correct |
41 |
Correct |
138 ms |
2412 KB |
Output is correct |
42 |
Incorrect |
38 ms |
3168 KB |
Output isn't correct |
43 |
Correct |
35 ms |
3120 KB |
Output is correct |
44 |
Correct |
195 ms |
3128 KB |
Output is correct |
45 |
Incorrect |
38 ms |
3656 KB |
Output isn't correct |
46 |
Correct |
42 ms |
3676 KB |
Output is correct |
47 |
Correct |
229 ms |
3608 KB |
Output is correct |
48 |
Incorrect |
46 ms |
4320 KB |
Output isn't correct |
49 |
Correct |
48 ms |
4232 KB |
Output is correct |
50 |
Correct |
282 ms |
4192 KB |
Output is correct |
51 |
Incorrect |
54 ms |
4924 KB |
Output isn't correct |
52 |
Correct |
58 ms |
4816 KB |
Output is correct |
53 |
Correct |
329 ms |
5056 KB |
Output is correct |
54 |
Incorrect |
63 ms |
5492 KB |
Output isn't correct |
55 |
Correct |
66 ms |
5620 KB |
Output is correct |
56 |
Correct |
385 ms |
5508 KB |
Output is correct |
57 |
Incorrect |
72 ms |
6172 KB |
Output isn't correct |
58 |
Correct |
75 ms |
6172 KB |
Output is correct |
59 |
Correct |
443 ms |
6284 KB |
Output is correct |
60 |
Incorrect |
85 ms |
6888 KB |
Output isn't correct |
61 |
Correct |
85 ms |
6992 KB |
Output is correct |
62 |
Correct |
495 ms |
6880 KB |
Output is correct |
63 |
Correct |
270 ms |
6888 KB |
Output is correct |
64 |
Correct |
387 ms |
6912 KB |
Output is correct |
65 |
Correct |
372 ms |
6944 KB |
Output is correct |
66 |
Incorrect |
344 ms |
7064 KB |
Output isn't correct |
67 |
Correct |
279 ms |
6960 KB |
Output is correct |
68 |
Correct |
141 ms |
6776 KB |
Output is correct |
69 |
Correct |
139 ms |
6776 KB |
Output is correct |
70 |
Correct |
132 ms |
6780 KB |
Output is correct |
71 |
Correct |
112 ms |
6904 KB |
Output is correct |
72 |
Incorrect |
110 ms |
6944 KB |
Output isn't correct |
73 |
Incorrect |
139 ms |
7064 KB |
Output isn't correct |
74 |
Correct |
283 ms |
6924 KB |
Output is correct |
75 |
Correct |
311 ms |
6936 KB |
Output is correct |
76 |
Correct |
285 ms |
6988 KB |
Output is correct |
77 |
Correct |
302 ms |
6908 KB |
Output is correct |
78 |
Correct |
332 ms |
7220 KB |
Output is correct |
79 |
Correct |
255 ms |
6968 KB |
Output is correct |
80 |
Correct |
271 ms |
6940 KB |
Output is correct |
81 |
Correct |
322 ms |
7000 KB |
Output is correct |
82 |
Correct |
237 ms |
7036 KB |
Output is correct |
83 |
Correct |
388 ms |
7052 KB |
Output is correct |
84 |
Correct |
341 ms |
7048 KB |
Output is correct |
85 |
Correct |
330 ms |
6928 KB |
Output is correct |
86 |
Correct |
371 ms |
7012 KB |
Output is correct |
87 |
Correct |
355 ms |
6952 KB |
Output is correct |
88 |
Correct |
394 ms |
6952 KB |
Output is correct |
89 |
Correct |
378 ms |
7064 KB |
Output is correct |
90 |
Correct |
404 ms |
7008 KB |
Output is correct |
91 |
Correct |
384 ms |
6948 KB |
Output is correct |
92 |
Correct |
398 ms |
6912 KB |
Output is correct |