#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
const int MAXN = 805;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int n, s;
pair<int, int> mecho, home;
queue<pair<int, int>> q;
char grid[MAXN][MAXN];
int depth[2][MAXN][MAXN];
bool bbound(int x, int y){
return (x >= 0 && y >= 0 && x < n && y < n && (grid[x][y] == 'G' || grid[x][y] == 'H' || grid[x][y] == 'M'));
}
bool mbound(int x, int y){
return (x >= 0 && y >= 0 && x < n && y < n && (grid[x][y] == 'G' || grid[x][y] == 'M' || grid[x][y] == 'D'));
}
bool check(int lim){
if(depth[0][mecho.first][mecho.second] <= lim) return false;
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) depth[1][i][j] = 0;
depth[1][mecho.first][mecho.second] = 1;
while(!q.empty()) q.pop();
q.push(mecho);
while(!q.empty()){
pair<int, int> curr = q.front();
q.pop();
for(int i = 0; i < 4; i++){
int x = curr.first + dx[i];
int y = curr.second + dy[i];
int d = depth[1][curr.first][curr.second] + 1;
// cout << d << '\n';
if(mbound(x, y) && !depth[1][x][y]){
// cout << d << '\n';
if((d - 1) / s + 1 + lim <= depth[0][x][y]){
depth[1][x][y] = d;
q.push(make_pair(x, y));
}
if(x == home.first && y == home.second && (d - 1) / s + lim - ((d-1) % s == 0) <= depth[0][x][y]) return true;
}
}
}
return depth[1][home.first][home.second];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n >> s;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> grid[i][j];
if(grid[i][j] == 'H') {
q.push(make_pair(i, j));
depth[0][i][j] = 1;
}
else if(grid[i][j] == 'M') mecho = make_pair(i, j);
else if(grid[i][j] == 'D') home = make_pair(i, j);
}
}
while(!q.empty()){
pair<int, int> curr = q.front();
q.pop();
for(int i = 0; i < 4; i++){
int x = curr.first + dx[i];
int y = curr.second + dy[i];
if(bbound(x, y) && !depth[0][x][y]){
depth[0][x][y] = depth[0][curr.first][curr.second] + 1;
q.push(make_pair(x, y));
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++) {
// depth[1][i][j] = (depth[1][i][j] + s - 1) / s;
depth[0][i][j] -= (depth[0][i][j] > 0);
}
}
depth[0][home.first][home.second] = 1e9;
int low = -1, high = 1e9;
while (low < high) {
int mid = low + (high - low + 1) / 2;
if (check(mid)) low = mid;
else high = mid - 1;
}
cout << (low) << '\n';
// cout << check(1) << '\n';
// for(int i = 0; i < n; i++){
// for(int j = 0; j < n; j++){
// cout << depth[0][i][j] << ' ';
// }
// cout << " ";
// for(int j = 0; j < n; j++){
// cout << depth[1][i][j] << ' ';
// }
// cout << '\n';
// }
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
87 ms |
6224 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
712 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
580 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
4 ms |
2900 KB |
Output is correct |
34 |
Correct |
4 ms |
2896 KB |
Output is correct |
35 |
Correct |
19 ms |
2872 KB |
Output is correct |
36 |
Correct |
5 ms |
3288 KB |
Output is correct |
37 |
Correct |
5 ms |
3284 KB |
Output is correct |
38 |
Correct |
25 ms |
3204 KB |
Output is correct |
39 |
Correct |
6 ms |
3584 KB |
Output is correct |
40 |
Correct |
6 ms |
3524 KB |
Output is correct |
41 |
Correct |
31 ms |
3540 KB |
Output is correct |
42 |
Correct |
9 ms |
3980 KB |
Output is correct |
43 |
Correct |
7 ms |
3912 KB |
Output is correct |
44 |
Correct |
41 ms |
3968 KB |
Output is correct |
45 |
Correct |
9 ms |
4296 KB |
Output is correct |
46 |
Correct |
8 ms |
4292 KB |
Output is correct |
47 |
Correct |
50 ms |
4320 KB |
Output is correct |
48 |
Correct |
10 ms |
4688 KB |
Output is correct |
49 |
Correct |
10 ms |
4632 KB |
Output is correct |
50 |
Correct |
62 ms |
4692 KB |
Output is correct |
51 |
Correct |
12 ms |
4948 KB |
Output is correct |
52 |
Correct |
11 ms |
4992 KB |
Output is correct |
53 |
Correct |
72 ms |
5000 KB |
Output is correct |
54 |
Correct |
13 ms |
5332 KB |
Output is correct |
55 |
Correct |
14 ms |
5412 KB |
Output is correct |
56 |
Correct |
88 ms |
5404 KB |
Output is correct |
57 |
Correct |
15 ms |
5716 KB |
Output is correct |
58 |
Correct |
14 ms |
5700 KB |
Output is correct |
59 |
Correct |
104 ms |
5764 KB |
Output is correct |
60 |
Correct |
17 ms |
6028 KB |
Output is correct |
61 |
Correct |
16 ms |
6016 KB |
Output is correct |
62 |
Correct |
107 ms |
6116 KB |
Output is correct |
63 |
Correct |
93 ms |
6120 KB |
Output is correct |
64 |
Correct |
165 ms |
6152 KB |
Output is correct |
65 |
Correct |
151 ms |
6068 KB |
Output is correct |
66 |
Correct |
108 ms |
6120 KB |
Output is correct |
67 |
Correct |
97 ms |
6120 KB |
Output is correct |
68 |
Correct |
44 ms |
6060 KB |
Output is correct |
69 |
Correct |
42 ms |
6072 KB |
Output is correct |
70 |
Correct |
29 ms |
6024 KB |
Output is correct |
71 |
Correct |
32 ms |
6064 KB |
Output is correct |
72 |
Correct |
29 ms |
6132 KB |
Output is correct |
73 |
Correct |
36 ms |
6312 KB |
Output is correct |
74 |
Correct |
64 ms |
6356 KB |
Output is correct |
75 |
Correct |
64 ms |
6268 KB |
Output is correct |
76 |
Correct |
57 ms |
6400 KB |
Output is correct |
77 |
Correct |
61 ms |
6388 KB |
Output is correct |
78 |
Correct |
68 ms |
6372 KB |
Output is correct |
79 |
Correct |
53 ms |
6372 KB |
Output is correct |
80 |
Correct |
60 ms |
6372 KB |
Output is correct |
81 |
Correct |
84 ms |
6248 KB |
Output is correct |
82 |
Correct |
55 ms |
6372 KB |
Output is correct |
83 |
Correct |
75 ms |
6280 KB |
Output is correct |
84 |
Correct |
66 ms |
6328 KB |
Output is correct |
85 |
Correct |
67 ms |
6220 KB |
Output is correct |
86 |
Correct |
75 ms |
6264 KB |
Output is correct |
87 |
Correct |
70 ms |
6324 KB |
Output is correct |
88 |
Correct |
76 ms |
6276 KB |
Output is correct |
89 |
Correct |
87 ms |
6392 KB |
Output is correct |
90 |
Correct |
81 ms |
6276 KB |
Output is correct |
91 |
Correct |
76 ms |
6228 KB |
Output is correct |
92 |
Correct |
80 ms |
6280 KB |
Output is correct |