#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;
}
}
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 = 1e6, 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;
}
# |
Verdict |
Execution time |
Memory |
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 |
157 ms |
8312 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 |
Incorrect |
3 ms |
4556 KB |
Output isn't correct |
13 |
Incorrect |
4 ms |
4428 KB |
Output isn't correct |
14 |
Correct |
4 ms |
4568 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 |
4428 KB |
Output is correct |
18 |
Correct |
3 ms |
4300 KB |
Output is correct |
19 |
Correct |
3 ms |
4428 KB |
Output is correct |
20 |
Correct |
4 ms |
4428 KB |
Output is correct |
21 |
Correct |
3 ms |
4428 KB |
Output is correct |
22 |
Correct |
3 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 |
3 ms |
4556 KB |
Output is correct |
27 |
Correct |
3 ms |
4556 KB |
Output is correct |
28 |
Correct |
3 ms |
4556 KB |
Output is correct |
29 |
Correct |
4 ms |
4556 KB |
Output is correct |
30 |
Correct |
3 ms |
4556 KB |
Output is correct |
31 |
Correct |
4 ms |
4684 KB |
Output is correct |
32 |
Correct |
4 ms |
4556 KB |
Output is correct |
33 |
Correct |
15 ms |
6084 KB |
Output is correct |
34 |
Correct |
16 ms |
5952 KB |
Output is correct |
35 |
Correct |
37 ms |
6024 KB |
Output is correct |
36 |
Correct |
19 ms |
6220 KB |
Output is correct |
37 |
Correct |
18 ms |
6204 KB |
Output is correct |
38 |
Correct |
48 ms |
6212 KB |
Output is correct |
39 |
Correct |
22 ms |
6480 KB |
Output is correct |
40 |
Correct |
22 ms |
6500 KB |
Output is correct |
41 |
Correct |
60 ms |
6504 KB |
Output is correct |
42 |
Correct |
27 ms |
6708 KB |
Output is correct |
43 |
Correct |
29 ms |
6700 KB |
Output is correct |
44 |
Correct |
78 ms |
6764 KB |
Output is correct |
45 |
Correct |
31 ms |
7004 KB |
Output is correct |
46 |
Correct |
31 ms |
6980 KB |
Output is correct |
47 |
Correct |
104 ms |
6892 KB |
Output is correct |
48 |
Correct |
36 ms |
7172 KB |
Output is correct |
49 |
Correct |
36 ms |
7172 KB |
Output is correct |
50 |
Correct |
112 ms |
7180 KB |
Output is correct |
51 |
Correct |
41 ms |
7460 KB |
Output is correct |
52 |
Correct |
42 ms |
7484 KB |
Output is correct |
53 |
Correct |
132 ms |
7492 KB |
Output is correct |
54 |
Correct |
48 ms |
7708 KB |
Output is correct |
55 |
Correct |
50 ms |
7672 KB |
Output is correct |
56 |
Correct |
154 ms |
7676 KB |
Output is correct |
57 |
Correct |
53 ms |
7876 KB |
Output is correct |
58 |
Correct |
52 ms |
7876 KB |
Output is correct |
59 |
Correct |
178 ms |
7992 KB |
Output is correct |
60 |
Correct |
58 ms |
8228 KB |
Output is correct |
61 |
Correct |
63 ms |
8288 KB |
Output is correct |
62 |
Correct |
203 ms |
8240 KB |
Output is correct |
63 |
Correct |
158 ms |
8236 KB |
Output is correct |
64 |
Correct |
224 ms |
8240 KB |
Output is correct |
65 |
Correct |
207 ms |
8188 KB |
Output is correct |
66 |
Correct |
188 ms |
8264 KB |
Output is correct |
67 |
Correct |
158 ms |
8228 KB |
Output is correct |
68 |
Correct |
94 ms |
8268 KB |
Output is correct |
69 |
Correct |
89 ms |
8240 KB |
Output is correct |
70 |
Correct |
84 ms |
8160 KB |
Output is correct |
71 |
Correct |
81 ms |
8388 KB |
Output is correct |
72 |
Incorrect |
73 ms |
8180 KB |
Output isn't correct |
73 |
Incorrect |
78 ms |
8400 KB |
Output isn't correct |
74 |
Correct |
115 ms |
8580 KB |
Output is correct |
75 |
Correct |
131 ms |
8448 KB |
Output is correct |
76 |
Correct |
116 ms |
8516 KB |
Output is correct |
77 |
Correct |
122 ms |
8516 KB |
Output is correct |
78 |
Correct |
131 ms |
8384 KB |
Output is correct |
79 |
Correct |
108 ms |
8456 KB |
Output is correct |
80 |
Correct |
117 ms |
8432 KB |
Output is correct |
81 |
Correct |
130 ms |
8388 KB |
Output is correct |
82 |
Correct |
105 ms |
8400 KB |
Output is correct |
83 |
Correct |
147 ms |
8408 KB |
Output is correct |
84 |
Correct |
127 ms |
8388 KB |
Output is correct |
85 |
Correct |
131 ms |
8524 KB |
Output is correct |
86 |
Correct |
138 ms |
8512 KB |
Output is correct |
87 |
Correct |
137 ms |
8352 KB |
Output is correct |
88 |
Correct |
143 ms |
8288 KB |
Output is correct |
89 |
Correct |
150 ms |
8516 KB |
Output is correct |
90 |
Correct |
152 ms |
8400 KB |
Output is correct |
91 |
Correct |
145 ms |
8388 KB |
Output is correct |
92 |
Correct |
147 ms |
8388 KB |
Output is correct |