#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 / S >= 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 |
4304 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 |
155 ms |
8336 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 |
4 ms |
4300 KB |
Output is correct |
12 |
Correct |
3 ms |
4556 KB |
Output is correct |
13 |
Incorrect |
3 ms |
4428 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 |
4 ms |
4300 KB |
Output is correct |
17 |
Correct |
4 ms |
4428 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 |
4496 KB |
Output is correct |
22 |
Correct |
3 ms |
4428 KB |
Output is correct |
23 |
Correct |
3 ms |
4428 KB |
Output is correct |
24 |
Correct |
4 ms |
4428 KB |
Output is correct |
25 |
Correct |
4 ms |
4556 KB |
Output is correct |
26 |
Correct |
3 ms |
4556 KB |
Output is correct |
27 |
Correct |
4 ms |
4556 KB |
Output is correct |
28 |
Correct |
3 ms |
4556 KB |
Output is correct |
29 |
Correct |
3 ms |
4556 KB |
Output is correct |
30 |
Correct |
4 ms |
4556 KB |
Output is correct |
31 |
Correct |
4 ms |
4556 KB |
Output is correct |
32 |
Correct |
4 ms |
4556 KB |
Output is correct |
33 |
Correct |
16 ms |
5924 KB |
Output is correct |
34 |
Correct |
19 ms |
6016 KB |
Output is correct |
35 |
Correct |
39 ms |
5964 KB |
Output is correct |
36 |
Correct |
18 ms |
6204 KB |
Output is correct |
37 |
Correct |
18 ms |
6236 KB |
Output is correct |
38 |
Correct |
48 ms |
6264 KB |
Output is correct |
39 |
Correct |
23 ms |
6532 KB |
Output is correct |
40 |
Correct |
23 ms |
6476 KB |
Output is correct |
41 |
Correct |
60 ms |
6500 KB |
Output is correct |
42 |
Correct |
26 ms |
6716 KB |
Output is correct |
43 |
Correct |
26 ms |
6664 KB |
Output is correct |
44 |
Correct |
80 ms |
6772 KB |
Output is correct |
45 |
Correct |
31 ms |
6980 KB |
Output is correct |
46 |
Correct |
31 ms |
6916 KB |
Output is correct |
47 |
Correct |
96 ms |
6952 KB |
Output is correct |
48 |
Correct |
39 ms |
7220 KB |
Output is correct |
49 |
Correct |
43 ms |
7164 KB |
Output is correct |
50 |
Correct |
113 ms |
7328 KB |
Output is correct |
51 |
Correct |
43 ms |
7448 KB |
Output is correct |
52 |
Correct |
45 ms |
7460 KB |
Output is correct |
53 |
Correct |
136 ms |
7508 KB |
Output is correct |
54 |
Correct |
59 ms |
7700 KB |
Output is correct |
55 |
Correct |
54 ms |
7748 KB |
Output is correct |
56 |
Correct |
155 ms |
7748 KB |
Output is correct |
57 |
Correct |
57 ms |
7988 KB |
Output is correct |
58 |
Correct |
54 ms |
7936 KB |
Output is correct |
59 |
Correct |
182 ms |
7988 KB |
Output is correct |
60 |
Correct |
61 ms |
8224 KB |
Output is correct |
61 |
Correct |
62 ms |
8144 KB |
Output is correct |
62 |
Correct |
211 ms |
8240 KB |
Output is correct |
63 |
Correct |
159 ms |
8132 KB |
Output is correct |
64 |
Correct |
240 ms |
8208 KB |
Output is correct |
65 |
Correct |
264 ms |
8260 KB |
Output is correct |
66 |
Correct |
174 ms |
8132 KB |
Output is correct |
67 |
Correct |
166 ms |
8152 KB |
Output is correct |
68 |
Correct |
96 ms |
8156 KB |
Output is correct |
69 |
Correct |
94 ms |
8288 KB |
Output is correct |
70 |
Correct |
77 ms |
8172 KB |
Output is correct |
71 |
Correct |
80 ms |
8260 KB |
Output is correct |
72 |
Incorrect |
78 ms |
8180 KB |
Output isn't correct |
73 |
Incorrect |
81 ms |
8512 KB |
Output isn't correct |
74 |
Correct |
116 ms |
8432 KB |
Output is correct |
75 |
Correct |
129 ms |
8516 KB |
Output is correct |
76 |
Correct |
119 ms |
8520 KB |
Output is correct |
77 |
Correct |
125 ms |
8420 KB |
Output is correct |
78 |
Correct |
135 ms |
8416 KB |
Output is correct |
79 |
Correct |
114 ms |
8388 KB |
Output is correct |
80 |
Correct |
128 ms |
8492 KB |
Output is correct |
81 |
Correct |
135 ms |
8400 KB |
Output is correct |
82 |
Correct |
117 ms |
8452 KB |
Output is correct |
83 |
Correct |
149 ms |
8388 KB |
Output is correct |
84 |
Correct |
130 ms |
8388 KB |
Output is correct |
85 |
Correct |
134 ms |
8432 KB |
Output is correct |
86 |
Correct |
144 ms |
8488 KB |
Output is correct |
87 |
Correct |
143 ms |
8344 KB |
Output is correct |
88 |
Correct |
150 ms |
8400 KB |
Output is correct |
89 |
Correct |
145 ms |
8280 KB |
Output is correct |
90 |
Correct |
152 ms |
8388 KB |
Output is correct |
91 |
Correct |
150 ms |
8436 KB |
Output is correct |
92 |
Correct |
151 ms |
8400 KB |
Output is correct |