#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 = 1e5, 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 |
4280 KB |
Output is correct |
4 |
Correct |
3 ms |
4276 KB |
Output is correct |
5 |
Correct |
3 ms |
4300 KB |
Output is correct |
6 |
Correct |
3 ms |
4300 KB |
Output is correct |
7 |
Correct |
159 ms |
8900 KB |
Output is correct |
8 |
Correct |
3 ms |
4300 KB |
Output is correct |
9 |
Correct |
3 ms |
4268 KB |
Output is correct |
10 |
Correct |
3 ms |
4300 KB |
Output is correct |
11 |
Correct |
3 ms |
4300 KB |
Output is correct |
12 |
Incorrect |
4 ms |
4556 KB |
Output isn't correct |
13 |
Incorrect |
3 ms |
4560 KB |
Output isn't 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 |
4428 KB |
Output is correct |
18 |
Correct |
3 ms |
4428 KB |
Output is correct |
19 |
Correct |
3 ms |
4440 KB |
Output is correct |
20 |
Correct |
3 ms |
4428 KB |
Output is correct |
21 |
Correct |
3 ms |
4428 KB |
Output is correct |
22 |
Correct |
3 ms |
4432 KB |
Output is correct |
23 |
Correct |
4 ms |
4428 KB |
Output is correct |
24 |
Correct |
3 ms |
4432 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 |
4 ms |
4560 KB |
Output is correct |
31 |
Correct |
4 ms |
4684 KB |
Output is correct |
32 |
Correct |
4 ms |
4684 KB |
Output is correct |
33 |
Correct |
14 ms |
6092 KB |
Output is correct |
34 |
Correct |
14 ms |
6124 KB |
Output is correct |
35 |
Correct |
37 ms |
6136 KB |
Output is correct |
36 |
Correct |
18 ms |
6392 KB |
Output is correct |
37 |
Correct |
18 ms |
6356 KB |
Output is correct |
38 |
Correct |
51 ms |
6436 KB |
Output is correct |
39 |
Incorrect |
24 ms |
6608 KB |
Output isn't correct |
40 |
Correct |
23 ms |
6668 KB |
Output is correct |
41 |
Correct |
65 ms |
6604 KB |
Output is correct |
42 |
Incorrect |
32 ms |
6980 KB |
Output isn't correct |
43 |
Correct |
28 ms |
6964 KB |
Output is correct |
44 |
Correct |
78 ms |
6964 KB |
Output is correct |
45 |
Incorrect |
44 ms |
7292 KB |
Output isn't correct |
46 |
Correct |
32 ms |
7244 KB |
Output is correct |
47 |
Correct |
94 ms |
7236 KB |
Output is correct |
48 |
Incorrect |
55 ms |
7620 KB |
Output isn't correct |
49 |
Correct |
37 ms |
7500 KB |
Output is correct |
50 |
Correct |
114 ms |
7612 KB |
Output is correct |
51 |
Incorrect |
69 ms |
7828 KB |
Output isn't correct |
52 |
Incorrect |
49 ms |
7900 KB |
Output isn't correct |
53 |
Correct |
132 ms |
7920 KB |
Output is correct |
54 |
Incorrect |
83 ms |
8140 KB |
Output isn't correct |
55 |
Incorrect |
72 ms |
8268 KB |
Output isn't correct |
56 |
Correct |
151 ms |
8148 KB |
Output is correct |
57 |
Incorrect |
100 ms |
8536 KB |
Output isn't correct |
58 |
Incorrect |
96 ms |
8524 KB |
Output isn't correct |
59 |
Correct |
180 ms |
8476 KB |
Output is correct |
60 |
Incorrect |
116 ms |
8864 KB |
Output isn't correct |
61 |
Incorrect |
114 ms |
8756 KB |
Output isn't correct |
62 |
Correct |
213 ms |
8936 KB |
Output is correct |
63 |
Correct |
156 ms |
8872 KB |
Output is correct |
64 |
Correct |
226 ms |
8868 KB |
Output is correct |
65 |
Correct |
217 ms |
8916 KB |
Output is correct |
66 |
Correct |
170 ms |
8752 KB |
Output is correct |
67 |
Correct |
173 ms |
8776 KB |
Output is correct |
68 |
Correct |
95 ms |
8904 KB |
Output is correct |
69 |
Correct |
81 ms |
8792 KB |
Output is correct |
70 |
Correct |
80 ms |
8868 KB |
Output is correct |
71 |
Correct |
82 ms |
8916 KB |
Output is correct |
72 |
Incorrect |
76 ms |
8852 KB |
Output isn't correct |
73 |
Incorrect |
79 ms |
9048 KB |
Output isn't correct |
74 |
Correct |
109 ms |
9144 KB |
Output is correct |
75 |
Correct |
119 ms |
9028 KB |
Output is correct |
76 |
Correct |
118 ms |
9120 KB |
Output is correct |
77 |
Correct |
122 ms |
9144 KB |
Output is correct |
78 |
Correct |
140 ms |
9028 KB |
Output is correct |
79 |
Correct |
121 ms |
9028 KB |
Output is correct |
80 |
Correct |
114 ms |
9116 KB |
Output is correct |
81 |
Correct |
125 ms |
9028 KB |
Output is correct |
82 |
Correct |
118 ms |
9116 KB |
Output is correct |
83 |
Correct |
150 ms |
8972 KB |
Output is correct |
84 |
Correct |
132 ms |
9028 KB |
Output is correct |
85 |
Correct |
140 ms |
9092 KB |
Output is correct |
86 |
Correct |
132 ms |
9032 KB |
Output is correct |
87 |
Correct |
140 ms |
9080 KB |
Output is correct |
88 |
Correct |
147 ms |
9008 KB |
Output is correct |
89 |
Correct |
144 ms |
9028 KB |
Output is correct |
90 |
Correct |
143 ms |
9032 KB |
Output is correct |
91 |
Correct |
150 ms |
8996 KB |
Output is correct |
92 |
Correct |
139 ms |
9028 KB |
Output is correct |