#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n, s;
cin >> n>> s;
vector<vector<char> > grid(n, vector<char>(n));
queue<pair<ll ,ll> > q1;
queue<pair<ll ,ll> > q2;
pair<ll ,ll> home;
pair<ll ,ll> start;
vector<vector<ll> > hdist(n, vector<ll>(n, -1));
for (ll i = 0; i < n; i++) for (ll j = 0; j < n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'H') {
q1.push({i, j});
hdist[i][j] = 0;
}
else if (grid[i][j] == 'M') {
// q2.push({i, j});
start = {i, j};
}
else if (grid[i][j] == 'D') home = {i, j};
}
vector<ll> dx = {1, -1, 0, 0};
vector<ll> dy = {0, 0, -1, 1};
while (!q1.empty() && hdist[home.first][home.second] == -1) {
pair<ll, ll> curr = q1.front();
q1.pop();
for (ll i = 0; i < 4; i++) {
ll x = curr.first + dx[i];
ll y = curr.second + dy[i];
if (x >= 0 && x < n && y >= 0 && y < n) {
if (hdist[x][y] == -1 && (grid[x][y] == 'G' || grid[x][y] == 'D')) {
hdist[x][y] = hdist[curr.first][curr.second] + s;
q1.push({x, y});
}
}
}
}
vector<vector<pair<ll, ll> > > parent(n, vector<pair<ll, ll> >(n, {-1, -1}));
ll hi = n*n;
ll lo = 0;
while (hi > lo || (hi == 0 && lo == 0)) {
ll mid = (hi + lo + 1)/2;
// cout << hi << lo << endl;
vector<vector<ll> > mdist(n, vector<ll>(n, -1));
mdist[start.first][start.second] = mid * s;
q2.push(start);
// cout << hdist[3][1] << endl;
while (!q2.empty()) {
pair<ll, ll> curr = q2.front();
// cout << mdist[curr.first][curr.second] << endl;
q2.pop();
for (ll i = 0; i < 4; i++) {
ll x = curr.first + dx[i];
ll y = curr.second + dy[i];
if (x >= 0 && x < n && y >= 0 && y < n) {
if (mdist[x][y] == -1 && (grid[x][y] == 'G' || grid[x][y] == 'D') && (hdist[x][y] > mdist[curr.first][curr.second] + 1 || hdist[x][y ] == -1)) {
mdist[x][y] = mdist[curr.first][curr.second] + 1;
q2.push({x, y});
parent[x][y] = {curr.first, curr.second};
}
}
}
}
if (mdist[home.first][home.second] != -1) {
lo = mid;
if (hi == 0 && lo == 0) break;
} else {
hi = mid - 1;
}
}
// ll ans = 100000000;
// pair<ll ,ll> current = home;
// // cout << mdist[home.first][home.second] << endl;
// if (mdist[home.first][home.second] == -1) {
// cout << -1 << endl;
// return 0;
// }
// while (current != start) {
// ll x = current.first;
// ll y = current.second;
// if (hdist[x][y] != -1) {
// // cout << x << y << hdist[x][y] << endl;
// ans = min(ans, hdist[x][y]-mdist[x][y]);
// }
// current = parent[x][y];
// }
// for (ll i = 0; i < 4; i++) {
// ll x = home.first + dx[i];
// ll y = home.second + dy[i];
// if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] != 'T') {
// ans = max(hdist[x][y] - mdist[x][y], ans);
// cout << mdist[x][y] << hdist[x][y] << x << y << endl;
// }
// }
cout << hi << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Incorrect |
296 ms |
21428 KB |
Output isn't correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
320 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
19 ms |
4308 KB |
Output is correct |
34 |
Correct |
19 ms |
4336 KB |
Output is correct |
35 |
Correct |
65 ms |
4328 KB |
Output is correct |
36 |
Correct |
21 ms |
5524 KB |
Output is correct |
37 |
Correct |
26 ms |
5516 KB |
Output is correct |
38 |
Correct |
95 ms |
5548 KB |
Output is correct |
39 |
Correct |
30 ms |
6868 KB |
Output is correct |
40 |
Correct |
38 ms |
6940 KB |
Output is correct |
41 |
Correct |
115 ms |
6896 KB |
Output is correct |
42 |
Correct |
35 ms |
8500 KB |
Output is correct |
43 |
Correct |
41 ms |
8508 KB |
Output is correct |
44 |
Correct |
155 ms |
8452 KB |
Output is correct |
45 |
Correct |
49 ms |
10204 KB |
Output is correct |
46 |
Correct |
50 ms |
10184 KB |
Output is correct |
47 |
Correct |
186 ms |
10160 KB |
Output is correct |
48 |
Correct |
50 ms |
12124 KB |
Output is correct |
49 |
Correct |
59 ms |
12172 KB |
Output is correct |
50 |
Correct |
253 ms |
12012 KB |
Output is correct |
51 |
Correct |
63 ms |
14140 KB |
Output is correct |
52 |
Correct |
61 ms |
14068 KB |
Output is correct |
53 |
Correct |
267 ms |
14092 KB |
Output is correct |
54 |
Correct |
77 ms |
16312 KB |
Output is correct |
55 |
Correct |
89 ms |
16304 KB |
Output is correct |
56 |
Correct |
370 ms |
16244 KB |
Output is correct |
57 |
Correct |
91 ms |
18624 KB |
Output is correct |
58 |
Correct |
94 ms |
18692 KB |
Output is correct |
59 |
Correct |
357 ms |
18612 KB |
Output is correct |
60 |
Correct |
95 ms |
21164 KB |
Output is correct |
61 |
Correct |
106 ms |
21180 KB |
Output is correct |
62 |
Correct |
435 ms |
21136 KB |
Output is correct |
63 |
Correct |
232 ms |
21180 KB |
Output is correct |
64 |
Correct |
338 ms |
21148 KB |
Output is correct |
65 |
Correct |
274 ms |
21172 KB |
Output is correct |
66 |
Correct |
240 ms |
21200 KB |
Output is correct |
67 |
Correct |
252 ms |
21144 KB |
Output is correct |
68 |
Correct |
147 ms |
21204 KB |
Output is correct |
69 |
Correct |
133 ms |
21236 KB |
Output is correct |
70 |
Correct |
117 ms |
21220 KB |
Output is correct |
71 |
Correct |
112 ms |
21184 KB |
Output is correct |
72 |
Incorrect |
127 ms |
21136 KB |
Output isn't correct |
73 |
Incorrect |
274 ms |
21248 KB |
Output isn't correct |
74 |
Correct |
187 ms |
21288 KB |
Output is correct |
75 |
Correct |
225 ms |
21240 KB |
Output is correct |
76 |
Correct |
222 ms |
21216 KB |
Output is correct |
77 |
Correct |
231 ms |
21280 KB |
Output is correct |
78 |
Correct |
326 ms |
21244 KB |
Output is correct |
79 |
Correct |
191 ms |
21244 KB |
Output is correct |
80 |
Correct |
189 ms |
21232 KB |
Output is correct |
81 |
Correct |
251 ms |
21208 KB |
Output is correct |
82 |
Correct |
199 ms |
21248 KB |
Output is correct |
83 |
Correct |
329 ms |
21212 KB |
Output is correct |
84 |
Correct |
303 ms |
21224 KB |
Output is correct |
85 |
Correct |
272 ms |
21240 KB |
Output is correct |
86 |
Correct |
329 ms |
21332 KB |
Output is correct |
87 |
Correct |
310 ms |
21304 KB |
Output is correct |
88 |
Correct |
396 ms |
21248 KB |
Output is correct |
89 |
Correct |
327 ms |
21228 KB |
Output is correct |
90 |
Correct |
372 ms |
21296 KB |
Output is correct |
91 |
Correct |
310 ms |
21260 KB |
Output is correct |
92 |
Correct |
296 ms |
21220 KB |
Output is correct |