#include <bits/stdc++.h>
using namespace std;
int n, S;
vector<string> board;
vector<vector<int>> vis;
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
struct point{
int i, j;
point(int i, int j) {
this->i = i;
this->j = j;
}
};
void solve()
{
cin >> n >> S;
board = vector<string>(n);
vis = vector<vector<int>> (n, vector<int> (n));
for (int i = 0; i < n; ++i) cin >> board[i];
point mecho(0,0), home(0,0);
vector<point> honey;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 'H') honey.emplace_back(i, j);
else if (board[i][j] == 'M') mecho = {i, j};
else if (board[i][j] == 'D') home = {i, j};
}
}
queue<point> mq;
vector<vector<int>> dist(n, vector<int> (n, 0 ));
for (auto x : honey) {
mq.push(x);
vis[x.i][x.j] = 1;
}
while (!mq.empty()) {
point u = mq.front();
mq.pop();
for (int i = 0; i < 4; ++i) {
if (u.i + dx[i] < 0 || u.i + dx[i] >= n || u.j + dy[i] < 0 || u.j + dy[i] >= n || vis[u.i+dx[i]][u.j+dy[i]] || (board[u.i+dx[i]][u.j+dy[i]] != 'G' && board[u.i+dx[i]][u.j+dy[i]] != 'M')) continue;
mq.emplace(u.i + dx[i], u.j + dy[i]);
vis[u.i + dx[i]][u.j + dy[i]] = 1;
dist[u.i + dx[i]][u.j + dy[i]] = dist[u.i][u.j] + 1;
}
}
// for (auto &x : dist)
// {
// for (auto y : x) cout << y << ' ';
// cout << '\n';
// }
int l = 0, r = 1e9;
// return;
while (l <= r) {
vector<vector<int>> md(n, vector<int> (n));
int m = (l + r) / 2;
vis = vector<vector<int>> (n, vector<int> (n));
queue<point> q;
q.emplace(mecho.i, mecho.j);
vis[mecho.i][mecho.j] = 1;
if (m >= dist[mecho.i][mecho.j]) q.pop();
while (!q.empty()) {
point u = q.front();
q.pop();
for (int i = 0; i < 4; ++i) {
int ni = u.i + dx[i], nj = u.j + dy[i];
if (u.i + dx[i] < 0 || u.i + dx[i] >= n || u.j + dy[i] < 0 || u.j + dy[i] >= n || vis[u.i+dx[i]][u.j+dy[i]] || (board[u.i+dx[i]][u.j+dy[i]] != 'G' && board[u.i+dx[i]][u.j+dy[i]] != 'M')) continue;
if (((md[u.i][u.j]+1) / S >= (dist[u.i][u.j] - m))) continue;
q.emplace(ni, nj);
md[ni][nj] = md[u.i][u.j] + 1;
vis[ni][nj] = 1;
}
}
bool ok = false;
for (int i = 0; i < 4; ++i) {
point u = home;
if (u.i + dx[i] < 0 || u.i + dx[i] >= n || u.j + dy[i] < 0 || u.j + dy[i] >= n || (board[u.i+dx[i]][u.j+dy[i]] != 'G' && board[u.i+dx[i]][u.j+dy[i]] != 'M')) continue;
if (((md[u.i][u.j]) / S >= (dist[u.i + dx[i]][u.j + dy[i]] - m))) continue;
if (vis[home.i + dx[i]][home.j+dy[i]]) ok = true;
}
if (ok) {
l = m+1;
} else r = m-1;
}
cout << l-1;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(nullptr);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
96 ms |
11304 KB |
Output isn't correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Incorrect |
10 ms |
2584 KB |
Output isn't correct |
34 |
Correct |
7 ms |
2548 KB |
Output is correct |
35 |
Incorrect |
20 ms |
2560 KB |
Output isn't correct |
36 |
Incorrect |
7 ms |
3416 KB |
Output isn't correct |
37 |
Correct |
7 ms |
3040 KB |
Output is correct |
38 |
Incorrect |
27 ms |
3164 KB |
Output isn't correct |
39 |
Incorrect |
9 ms |
3900 KB |
Output isn't correct |
40 |
Correct |
10 ms |
3884 KB |
Output is correct |
41 |
Incorrect |
33 ms |
3912 KB |
Output isn't correct |
42 |
Incorrect |
10 ms |
4720 KB |
Output isn't correct |
43 |
Correct |
10 ms |
4712 KB |
Output is correct |
44 |
Incorrect |
44 ms |
4740 KB |
Output isn't correct |
45 |
Incorrect |
16 ms |
5576 KB |
Output isn't correct |
46 |
Correct |
13 ms |
5596 KB |
Output is correct |
47 |
Incorrect |
54 ms |
5652 KB |
Output isn't correct |
48 |
Incorrect |
16 ms |
6608 KB |
Output isn't correct |
49 |
Correct |
16 ms |
6588 KB |
Output is correct |
50 |
Incorrect |
68 ms |
6608 KB |
Output isn't correct |
51 |
Incorrect |
18 ms |
7668 KB |
Output isn't correct |
52 |
Correct |
20 ms |
7512 KB |
Output is correct |
53 |
Incorrect |
77 ms |
7608 KB |
Output isn't correct |
54 |
Incorrect |
22 ms |
8808 KB |
Output isn't correct |
55 |
Correct |
24 ms |
8784 KB |
Output is correct |
56 |
Incorrect |
91 ms |
8784 KB |
Output isn't correct |
57 |
Incorrect |
22 ms |
9980 KB |
Output isn't correct |
58 |
Correct |
23 ms |
10016 KB |
Output is correct |
59 |
Incorrect |
106 ms |
10012 KB |
Output isn't correct |
60 |
Incorrect |
26 ms |
11324 KB |
Output isn't correct |
61 |
Correct |
38 ms |
11300 KB |
Output is correct |
62 |
Incorrect |
129 ms |
11324 KB |
Output isn't correct |
63 |
Incorrect |
92 ms |
11324 KB |
Output isn't correct |
64 |
Incorrect |
174 ms |
11312 KB |
Output isn't correct |
65 |
Correct |
175 ms |
11380 KB |
Output is correct |
66 |
Correct |
118 ms |
11324 KB |
Output is correct |
67 |
Correct |
107 ms |
11320 KB |
Output is correct |
68 |
Correct |
63 ms |
11368 KB |
Output is correct |
69 |
Incorrect |
62 ms |
11348 KB |
Output isn't correct |
70 |
Correct |
49 ms |
11344 KB |
Output is correct |
71 |
Correct |
51 ms |
11404 KB |
Output is correct |
72 |
Correct |
48 ms |
11388 KB |
Output is correct |
73 |
Correct |
51 ms |
11448 KB |
Output is correct |
74 |
Incorrect |
82 ms |
11436 KB |
Output isn't correct |
75 |
Incorrect |
73 ms |
11412 KB |
Output isn't correct |
76 |
Incorrect |
73 ms |
11400 KB |
Output isn't correct |
77 |
Incorrect |
71 ms |
11380 KB |
Output isn't correct |
78 |
Correct |
77 ms |
11348 KB |
Output is correct |
79 |
Incorrect |
67 ms |
11376 KB |
Output isn't correct |
80 |
Incorrect |
69 ms |
11468 KB |
Output isn't correct |
81 |
Incorrect |
77 ms |
11284 KB |
Output isn't correct |
82 |
Incorrect |
68 ms |
11376 KB |
Output isn't correct |
83 |
Incorrect |
88 ms |
11432 KB |
Output isn't correct |
84 |
Incorrect |
86 ms |
11444 KB |
Output isn't correct |
85 |
Incorrect |
78 ms |
11376 KB |
Output isn't correct |
86 |
Incorrect |
84 ms |
11352 KB |
Output isn't correct |
87 |
Incorrect |
81 ms |
11384 KB |
Output isn't correct |
88 |
Incorrect |
92 ms |
11376 KB |
Output isn't correct |
89 |
Incorrect |
100 ms |
11872 KB |
Output isn't correct |
90 |
Incorrect |
90 ms |
11416 KB |
Output isn't correct |
91 |
Incorrect |
88 ms |
11336 KB |
Output isn't correct |
92 |
Incorrect |
89 ms |
11384 KB |
Output isn't correct |