#include <bits/stdc++.h>
using namespace std;
const int dydis = 1e3 + 10;
const int inf = 1e9;
int n, S;
char mas[dydis][dydis];
int dist[dydis][dydis];
vector<pair<int, int> > dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
void bfs(){
for(int i = 0; i < dydis; i++) for(int j = 0; j < dydis; j++) dist[i][j] = inf;
queue<pair<int, int> > q;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(mas[i][j] != 'H') continue;
dist[i][j] = 0;
q.push({i, j});
}
}
while(!q.empty()){
int e = q.front().first;
int s = q.front().second;
int pe, ps;
q.pop();
for(auto x : dir){
pe = x.first + e;
ps = x.second + s;
if(pe < 0 || ps < 0 || pe >= n || ps >= n) continue;
if(mas[pe][ps] == 'D' || mas[pe][ps] == 'T') continue;
if(dist[pe][ps] > dist[e][s] + 1){
dist[pe][ps] = dist[e][s] + 1;
q.push({pe, ps});
}
}
}
}
int d[dydis][dydis];
int sE, sS, eE, eS;
bool f(int X){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
d[i][j] = inf;
}
}
if(X >= dist[sE][sS]) return false;
queue<pair<int, int> > q;
d[sE][sS] = 0;
q.push({sE, sS});
while(!q.empty()){
int e = q.front().first;
int s = q.front().second;
int pe, ps;
q.pop();
for(auto x : dir){
pe = e + x.first;
ps = s + x.second;
if(pe < 0 || ps < 0 || pe >= n || ps >= n) continue;
if(mas[pe][ps] == 'H' || mas[pe][ps] == 'T') continue;
int bus = d[e][s] + 1;
int b1 = bus / S;
if(b1 >= dist[pe][ps] - X) continue;
if(d[pe][ps] > bus){
d[pe][ps] = bus;
q.push({pe, ps});
}
}
}
return d[eE][eS] < inf;
}
int main(){
cin >> n >> S;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> mas[i][j];
if(mas[i][j] == 'M') sE = i, sS = j;
if(mas[i][j] == 'D') eE = i, eS = j;
}
}
bfs();
int l = 0, r = 1e9, mid, ans = -1;
while(l <= r){
mid = (l + r) / 2;
if(f(mid)){
ans = max(ans, mid);
l = mid+1;
}else{
r = mid-1;
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4300 KB |
Output is correct |
2 |
Correct |
3 ms |
4300 KB |
Output is correct |
3 |
Correct |
3 ms |
4300 KB |
Output is correct |
4 |
Correct |
3 ms |
4300 KB |
Output is correct |
5 |
Correct |
3 ms |
4300 KB |
Output is correct |
6 |
Correct |
3 ms |
4300 KB |
Output is correct |
7 |
Correct |
169 ms |
8340 KB |
Output is correct |
8 |
Correct |
3 ms |
4300 KB |
Output is correct |
9 |
Correct |
3 ms |
4300 KB |
Output is correct |
10 |
Correct |
3 ms |
4300 KB |
Output is correct |
11 |
Correct |
3 ms |
4300 KB |
Output is correct |
12 |
Correct |
3 ms |
4556 KB |
Output is correct |
13 |
Correct |
3 ms |
4428 KB |
Output is correct |
14 |
Correct |
4 ms |
4556 KB |
Output is correct |
15 |
Correct |
3 ms |
4300 KB |
Output is correct |
16 |
Correct |
3 ms |
4300 KB |
Output is correct |
17 |
Correct |
3 ms |
4300 KB |
Output is correct |
18 |
Correct |
3 ms |
4428 KB |
Output is correct |
19 |
Correct |
3 ms |
4428 KB |
Output is correct |
20 |
Correct |
3 ms |
4428 KB |
Output is correct |
21 |
Correct |
3 ms |
4428 KB |
Output is correct |
22 |
Correct |
4 ms |
4428 KB |
Output is correct |
23 |
Correct |
3 ms |
4428 KB |
Output is correct |
24 |
Correct |
3 ms |
4428 KB |
Output is correct |
25 |
Correct |
3 ms |
4556 KB |
Output is correct |
26 |
Correct |
4 ms |
4556 KB |
Output is correct |
27 |
Correct |
3 ms |
4556 KB |
Output is correct |
28 |
Correct |
3 ms |
4560 KB |
Output is correct |
29 |
Correct |
4 ms |
4644 KB |
Output is correct |
30 |
Correct |
4 ms |
4556 KB |
Output is correct |
31 |
Correct |
3 ms |
4556 KB |
Output is correct |
32 |
Correct |
4 ms |
4556 KB |
Output is correct |
33 |
Correct |
16 ms |
5964 KB |
Output is correct |
34 |
Correct |
16 ms |
5924 KB |
Output is correct |
35 |
Correct |
38 ms |
6056 KB |
Output is correct |
36 |
Correct |
19 ms |
6220 KB |
Output is correct |
37 |
Correct |
19 ms |
6248 KB |
Output is correct |
38 |
Correct |
48 ms |
6220 KB |
Output is correct |
39 |
Correct |
23 ms |
6468 KB |
Output is correct |
40 |
Correct |
23 ms |
6492 KB |
Output is correct |
41 |
Correct |
63 ms |
6416 KB |
Output is correct |
42 |
Correct |
26 ms |
6736 KB |
Output is correct |
43 |
Correct |
26 ms |
6632 KB |
Output is correct |
44 |
Correct |
81 ms |
6720 KB |
Output is correct |
45 |
Correct |
32 ms |
6912 KB |
Output is correct |
46 |
Correct |
33 ms |
6932 KB |
Output is correct |
47 |
Correct |
111 ms |
7012 KB |
Output is correct |
48 |
Correct |
39 ms |
7164 KB |
Output is correct |
49 |
Correct |
39 ms |
7180 KB |
Output is correct |
50 |
Correct |
114 ms |
7248 KB |
Output is correct |
51 |
Correct |
44 ms |
7440 KB |
Output is correct |
52 |
Correct |
47 ms |
7484 KB |
Output is correct |
53 |
Correct |
139 ms |
7620 KB |
Output is correct |
54 |
Correct |
55 ms |
7684 KB |
Output is correct |
55 |
Correct |
55 ms |
7620 KB |
Output is correct |
56 |
Correct |
154 ms |
7740 KB |
Output is correct |
57 |
Correct |
60 ms |
7876 KB |
Output is correct |
58 |
Correct |
59 ms |
7876 KB |
Output is correct |
59 |
Correct |
187 ms |
7992 KB |
Output is correct |
60 |
Correct |
63 ms |
8176 KB |
Output is correct |
61 |
Correct |
66 ms |
8180 KB |
Output is correct |
62 |
Correct |
208 ms |
8256 KB |
Output is correct |
63 |
Correct |
164 ms |
8256 KB |
Output is correct |
64 |
Correct |
260 ms |
8300 KB |
Output is correct |
65 |
Correct |
237 ms |
8132 KB |
Output is correct |
66 |
Correct |
175 ms |
8352 KB |
Output is correct |
67 |
Correct |
162 ms |
8260 KB |
Output is correct |
68 |
Correct |
100 ms |
8268 KB |
Output is correct |
69 |
Correct |
97 ms |
8212 KB |
Output is correct |
70 |
Correct |
92 ms |
8188 KB |
Output is correct |
71 |
Correct |
80 ms |
8260 KB |
Output is correct |
72 |
Correct |
77 ms |
8260 KB |
Output is correct |
73 |
Correct |
95 ms |
8500 KB |
Output is correct |
74 |
Correct |
126 ms |
8488 KB |
Output is correct |
75 |
Correct |
132 ms |
8620 KB |
Output is correct |
76 |
Correct |
121 ms |
8404 KB |
Output is correct |
77 |
Correct |
127 ms |
8564 KB |
Output is correct |
78 |
Correct |
136 ms |
8488 KB |
Output is correct |
79 |
Correct |
114 ms |
8472 KB |
Output is correct |
80 |
Correct |
123 ms |
8388 KB |
Output is correct |
81 |
Correct |
158 ms |
8448 KB |
Output is correct |
82 |
Correct |
121 ms |
8476 KB |
Output is correct |
83 |
Correct |
147 ms |
8364 KB |
Output is correct |
84 |
Correct |
135 ms |
8392 KB |
Output is correct |
85 |
Correct |
158 ms |
8552 KB |
Output is correct |
86 |
Correct |
146 ms |
8420 KB |
Output is correct |
87 |
Correct |
151 ms |
8388 KB |
Output is correct |
88 |
Correct |
150 ms |
8516 KB |
Output is correct |
89 |
Correct |
149 ms |
8388 KB |
Output is correct |
90 |
Correct |
158 ms |
8352 KB |
Output is correct |
91 |
Correct |
149 ms |
8292 KB |
Output is correct |
92 |
Correct |
167 ms |
8308 KB |
Output is correct |