#include <bits/stdc++.h>
#define ff first
#define ss second
#define cont continue;
#define sz size()
#define pb push
using namespace std;
typedef long long ll;
const int N = 950;
int tr[4] = {1, -1, 0, 0};
int rt[4] = {0, 0, 1, -1};
void solve();
char a[N][N];
int dis[N][N], dis2[N][N];
int vis[N][N];
int a1,a2, a3, a4, jogap = -1, n, s;
queue < pair<int,int> > q;
bool barla (int x) {
memset (vis, 0, sizeof (vis));
memset (dis2, 0, sizeof (dis2));
while (!q.empty()) q.pop();
q.push({a1, a2});
vis[a1][a2] = 1;
if (x >= dis[a1][a2]) return 0;
dis2[a1][a2] = 0;
while (!q.empty()) {
int b1 = q.front().ff;
int b2 = q.front().ss;
q.pop();
for (int i = 0; i < 4; ++i) {
int c1 = b1 + tr[i];
int c2 = b2 + rt[i];
if (c1 < 0 or c2 < 0 or c1 >= n or c2 >= n or a[c1][c2] == 'T' or a[c1][c2] == 'H') continue;
if (vis[c1][c2]) continue;
int kl = ceil((dis2[b1][b2] + 1) / (double) s) + x;
int jp = ceil (dis2[b1][b2] / (double) s) + x;
if (dis[b1][b2] == jp) {
if (kl != jp) continue;
}
if (kl <= dis[c1][c2]) {
dis2[c1][c2] = dis2[b1][b2] + 1;
q.push({c1,c2});
vis[c1][c2] = 1;
if (a[c1][c2] == 'D') return 1;
}
}
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n >> s;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
dis[i][j] = 1e9;
cin >> a[i][j];
if (a[i][j] == 'M') {
a1 = i;
a[i][j] = 'G';
a2 = j;
}
else if (a[i][j] == 'D') {
a3 = i;
a4 = j;
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (a[i][j] == 'H') {
q.push ({i, j});
dis[i][j] = 0;
vis[i][j] = 1;
}
}
}
while (!q.empty()) {
int b1 = q.front().ff;
int b2 = q.front().ss;
q.pop();
for (int i = 0; i < 4; ++i) {
int c1 = b1 + tr[i];
int c2 = b2 + rt[i];
if (c1 < 0 or c2 < 0 or c1 >= n or c2 >= n or a[c1][c2] == 'T' or a[c1][c2] == 'D' or a[c1][c2] == 'H') continue;
if (vis[c1][c2]) continue;
dis[c1][c2] = dis[b1][b2] + 1;
vis[c1][c2] = 1;
q.push({c1, c2});
}
}
int l = 0, r = 850 * 850;
while (l <= r) {
int md = (l + r) / 2;
if (barla(md)) {
l = md + 1;
jogap = md;
}
else r = md - 1;
}
cout << jogap;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
7380 KB |
Output is correct |
2 |
Correct |
10 ms |
7392 KB |
Output is correct |
3 |
Correct |
9 ms |
7420 KB |
Output is correct |
4 |
Correct |
9 ms |
7368 KB |
Output is correct |
5 |
Correct |
9 ms |
7380 KB |
Output is correct |
6 |
Correct |
9 ms |
7380 KB |
Output is correct |
7 |
Correct |
121 ms |
11796 KB |
Output is correct |
8 |
Correct |
9 ms |
7380 KB |
Output is correct |
9 |
Correct |
10 ms |
7380 KB |
Output is correct |
10 |
Correct |
11 ms |
7380 KB |
Output is correct |
11 |
Correct |
13 ms |
7380 KB |
Output is correct |
12 |
Correct |
17 ms |
7680 KB |
Output is correct |
13 |
Correct |
13 ms |
7636 KB |
Output is correct |
14 |
Correct |
11 ms |
7692 KB |
Output is correct |
15 |
Correct |
9 ms |
7380 KB |
Output is correct |
16 |
Correct |
10 ms |
7492 KB |
Output is correct |
17 |
Correct |
8 ms |
7508 KB |
Output is correct |
18 |
Correct |
12 ms |
7516 KB |
Output is correct |
19 |
Correct |
12 ms |
7540 KB |
Output is correct |
20 |
Correct |
11 ms |
7540 KB |
Output is correct |
21 |
Correct |
12 ms |
7500 KB |
Output is correct |
22 |
Correct |
11 ms |
7588 KB |
Output is correct |
23 |
Correct |
11 ms |
7632 KB |
Output is correct |
24 |
Correct |
13 ms |
7508 KB |
Output is correct |
25 |
Correct |
11 ms |
7636 KB |
Output is correct |
26 |
Correct |
11 ms |
7680 KB |
Output is correct |
27 |
Correct |
9 ms |
7636 KB |
Output is correct |
28 |
Correct |
9 ms |
7708 KB |
Output is correct |
29 |
Correct |
10 ms |
7636 KB |
Output is correct |
30 |
Correct |
10 ms |
7732 KB |
Output is correct |
31 |
Correct |
11 ms |
7632 KB |
Output is correct |
32 |
Correct |
10 ms |
7748 KB |
Output is correct |
33 |
Correct |
19 ms |
9144 KB |
Output is correct |
34 |
Correct |
13 ms |
9044 KB |
Output is correct |
35 |
Correct |
46 ms |
9036 KB |
Output is correct |
36 |
Correct |
15 ms |
9416 KB |
Output is correct |
37 |
Correct |
16 ms |
9368 KB |
Output is correct |
38 |
Correct |
46 ms |
9348 KB |
Output is correct |
39 |
Correct |
17 ms |
9684 KB |
Output is correct |
40 |
Correct |
19 ms |
9704 KB |
Output is correct |
41 |
Correct |
51 ms |
9676 KB |
Output is correct |
42 |
Correct |
18 ms |
9940 KB |
Output is correct |
43 |
Correct |
18 ms |
9892 KB |
Output is correct |
44 |
Correct |
73 ms |
9964 KB |
Output is correct |
45 |
Correct |
19 ms |
10184 KB |
Output is correct |
46 |
Correct |
18 ms |
10224 KB |
Output is correct |
47 |
Correct |
80 ms |
10324 KB |
Output is correct |
48 |
Correct |
21 ms |
10480 KB |
Output is correct |
49 |
Correct |
23 ms |
10452 KB |
Output is correct |
50 |
Correct |
92 ms |
10452 KB |
Output is correct |
51 |
Correct |
22 ms |
10700 KB |
Output is correct |
52 |
Correct |
23 ms |
10756 KB |
Output is correct |
53 |
Correct |
110 ms |
10700 KB |
Output is correct |
54 |
Correct |
31 ms |
11052 KB |
Output is correct |
55 |
Correct |
26 ms |
11088 KB |
Output is correct |
56 |
Correct |
126 ms |
11124 KB |
Output is correct |
57 |
Correct |
27 ms |
11344 KB |
Output is correct |
58 |
Correct |
25 ms |
11340 KB |
Output is correct |
59 |
Correct |
142 ms |
11420 KB |
Output is correct |
60 |
Correct |
36 ms |
11576 KB |
Output is correct |
61 |
Correct |
27 ms |
11680 KB |
Output is correct |
62 |
Correct |
171 ms |
11668 KB |
Output is correct |
63 |
Correct |
117 ms |
11596 KB |
Output is correct |
64 |
Correct |
227 ms |
11564 KB |
Output is correct |
65 |
Correct |
170 ms |
11684 KB |
Output is correct |
66 |
Correct |
162 ms |
11572 KB |
Output is correct |
67 |
Correct |
141 ms |
11716 KB |
Output is correct |
68 |
Correct |
77 ms |
11672 KB |
Output is correct |
69 |
Correct |
51 ms |
11724 KB |
Output is correct |
70 |
Correct |
53 ms |
11608 KB |
Output is correct |
71 |
Correct |
52 ms |
11708 KB |
Output is correct |
72 |
Correct |
40 ms |
11616 KB |
Output is correct |
73 |
Correct |
59 ms |
11844 KB |
Output is correct |
74 |
Correct |
86 ms |
11972 KB |
Output is correct |
75 |
Correct |
96 ms |
11956 KB |
Output is correct |
76 |
Correct |
85 ms |
11968 KB |
Output is correct |
77 |
Correct |
98 ms |
11980 KB |
Output is correct |
78 |
Correct |
125 ms |
11808 KB |
Output is correct |
79 |
Correct |
83 ms |
11856 KB |
Output is correct |
80 |
Correct |
89 ms |
11932 KB |
Output is correct |
81 |
Correct |
89 ms |
11940 KB |
Output is correct |
82 |
Correct |
97 ms |
11832 KB |
Output is correct |
83 |
Correct |
116 ms |
11792 KB |
Output is correct |
84 |
Correct |
103 ms |
11896 KB |
Output is correct |
85 |
Correct |
113 ms |
11896 KB |
Output is correct |
86 |
Correct |
99 ms |
11800 KB |
Output is correct |
87 |
Correct |
119 ms |
11896 KB |
Output is correct |
88 |
Correct |
112 ms |
11916 KB |
Output is correct |
89 |
Correct |
132 ms |
11756 KB |
Output is correct |
90 |
Correct |
110 ms |
11844 KB |
Output is correct |
91 |
Correct |
118 ms |
11840 KB |
Output is correct |
92 |
Correct |
118 ms |
11840 KB |
Output is correct |