#include <bits/stdc++.h>
#define pi pair<int, int>
#define ti tuple<int, int, int>
#define tii tuple<int, int, int, int>
using namespace std;
int n, s, bee[810][810], stx, sty, enx, eny, cx, cy, cs, cdi, l=0, r=1e9, mid;
int di[4] = {0, 1, 0, -1}, dj[4] = {1, 0, -1, 0};
pi mec[810][810];
vector<pi> hi;
char arr[810][810];
bool solve(int mn) {
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
mec[i][j] = {1e9, 1e9};
}
}
queue<tii> q;
q.push({stx, sty, 0, 0});
while(q.size()) {
cx = get<0>(q.front());
cy = get<1>(q.front());
cdi = get<2>(q.front());
cs = get<3>(q.front());
q.pop();
if(cs == s) {
cs = 0;
cdi++;
}
if(cx < 1 || cx > n || cy < 1 || cy > n || arr[cx][cy] == 'T' || mec[cx][cy].first <= cdi) continue;
if(mec[cx][cy].first == cdi && mec[cx][cy].second <= cs) continue;
if(cdi+mn >= bee[cx][cy]) continue;
mec[cx][cy] = {cdi, cs};
for(int i=0; i<4; i++) {
q.push({cx+di[i], cy+dj[i], cdi, cs+1});
}
}
return mec[enx][eny].first != 1e9;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> s;
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
bee[i][j] = 1e9;
cin >> arr[i][j];
if(arr[i][j] == 'M') {
stx = i;
sty = j;
} else if(arr[i][j] == 'D') {
enx = i;
eny = j;
} else if(arr[i][j] == 'H') {
hi.push_back({i, j});
}
}
}
queue<ti> q;
for(auto i: hi) {
q.push({i.first, i.second, 0});
}
while(q.size()) {
cx = get<0>(q.front());
cy = get<1>(q.front());
cdi = get<2>(q.front());
q.pop();
if(cx < 1 || cx > n || cy < 1 || cy > n || arr[cx][cy] == 'T' || arr[cx][cy] == 'D' || bee[cx][cy] <= cdi) continue;
bee[cx][cy] = cdi;
for(int i=0; i<4; i++) {
q.push({cx+di[i], cy+dj[i], cdi+1});
}
}
while(l < r) {
mid = (l+r)>>1;
if(solve(mid)) {
l = mid+1;
} else {
r = mid;
}
}
cout << l-1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
141 ms |
9420 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4440 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
8 ms |
7260 KB |
Output is correct |
34 |
Correct |
9 ms |
7260 KB |
Output is correct |
35 |
Correct |
40 ms |
7256 KB |
Output is correct |
36 |
Correct |
11 ms |
7260 KB |
Output is correct |
37 |
Correct |
10 ms |
7508 KB |
Output is correct |
38 |
Correct |
44 ms |
7524 KB |
Output is correct |
39 |
Correct |
11 ms |
7516 KB |
Output is correct |
40 |
Correct |
12 ms |
7516 KB |
Output is correct |
41 |
Correct |
57 ms |
7700 KB |
Output is correct |
42 |
Correct |
13 ms |
7772 KB |
Output is correct |
43 |
Correct |
14 ms |
7716 KB |
Output is correct |
44 |
Correct |
75 ms |
7772 KB |
Output is correct |
45 |
Correct |
18 ms |
7768 KB |
Output is correct |
46 |
Correct |
18 ms |
7772 KB |
Output is correct |
47 |
Correct |
99 ms |
8012 KB |
Output is correct |
48 |
Correct |
21 ms |
8148 KB |
Output is correct |
49 |
Correct |
22 ms |
8028 KB |
Output is correct |
50 |
Correct |
112 ms |
8184 KB |
Output is correct |
51 |
Correct |
23 ms |
8284 KB |
Output is correct |
52 |
Correct |
25 ms |
8284 KB |
Output is correct |
53 |
Correct |
128 ms |
8280 KB |
Output is correct |
54 |
Correct |
30 ms |
8284 KB |
Output is correct |
55 |
Correct |
35 ms |
8540 KB |
Output is correct |
56 |
Correct |
160 ms |
8504 KB |
Output is correct |
57 |
Correct |
28 ms |
8540 KB |
Output is correct |
58 |
Correct |
28 ms |
8624 KB |
Output is correct |
59 |
Correct |
179 ms |
8664 KB |
Output is correct |
60 |
Correct |
32 ms |
8792 KB |
Output is correct |
61 |
Correct |
40 ms |
8536 KB |
Output is correct |
62 |
Correct |
216 ms |
8812 KB |
Output is correct |
63 |
Correct |
150 ms |
8784 KB |
Output is correct |
64 |
Correct |
220 ms |
8800 KB |
Output is correct |
65 |
Correct |
222 ms |
8792 KB |
Output is correct |
66 |
Correct |
166 ms |
8788 KB |
Output is correct |
67 |
Correct |
166 ms |
8776 KB |
Output is correct |
68 |
Correct |
70 ms |
8948 KB |
Output is correct |
69 |
Correct |
63 ms |
8704 KB |
Output is correct |
70 |
Correct |
57 ms |
8796 KB |
Output is correct |
71 |
Correct |
50 ms |
8784 KB |
Output is correct |
72 |
Correct |
48 ms |
8792 KB |
Output is correct |
73 |
Correct |
57 ms |
10272 KB |
Output is correct |
74 |
Correct |
112 ms |
10256 KB |
Output is correct |
75 |
Correct |
110 ms |
10432 KB |
Output is correct |
76 |
Correct |
99 ms |
10324 KB |
Output is correct |
77 |
Correct |
106 ms |
10308 KB |
Output is correct |
78 |
Correct |
127 ms |
10264 KB |
Output is correct |
79 |
Correct |
96 ms |
10260 KB |
Output is correct |
80 |
Correct |
107 ms |
10076 KB |
Output is correct |
81 |
Correct |
139 ms |
10324 KB |
Output is correct |
82 |
Correct |
98 ms |
10076 KB |
Output is correct |
83 |
Correct |
134 ms |
9972 KB |
Output is correct |
84 |
Correct |
119 ms |
9816 KB |
Output is correct |
85 |
Correct |
115 ms |
10072 KB |
Output is correct |
86 |
Correct |
134 ms |
9816 KB |
Output is correct |
87 |
Correct |
120 ms |
10008 KB |
Output is correct |
88 |
Correct |
133 ms |
9808 KB |
Output is correct |
89 |
Correct |
153 ms |
9944 KB |
Output is correct |
90 |
Correct |
148 ms |
9808 KB |
Output is correct |
91 |
Correct |
133 ms |
9516 KB |
Output is correct |
92 |
Correct |
169 ms |
9808 KB |
Output is correct |