#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' || grid[x][y] == 'D'));
}
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){
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 + 1 + 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);
}
}
int low = -1, high = 5000;
while (low < high) {
int mid = low + (high - low + 1) / 2;
if (check(mid)) low = mid;
else high = mid - 1;
}
cout << (low) << '\n';
// cout << check(3) << '\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 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Incorrect |
78 ms |
6728 KB |
Output isn't correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
464 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 |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
460 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 |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
712 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
712 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 |
840 KB |
Output is correct |
32 |
Correct |
1 ms |
840 KB |
Output is correct |
33 |
Incorrect |
4 ms |
2900 KB |
Output isn't correct |
34 |
Incorrect |
5 ms |
2900 KB |
Output isn't correct |
35 |
Correct |
23 ms |
2900 KB |
Output is correct |
36 |
Incorrect |
5 ms |
3284 KB |
Output isn't correct |
37 |
Incorrect |
5 ms |
3284 KB |
Output isn't correct |
38 |
Correct |
25 ms |
3284 KB |
Output is correct |
39 |
Incorrect |
6 ms |
3668 KB |
Output isn't correct |
40 |
Incorrect |
6 ms |
3700 KB |
Output isn't correct |
41 |
Correct |
31 ms |
3716 KB |
Output is correct |
42 |
Incorrect |
7 ms |
4052 KB |
Output isn't correct |
43 |
Incorrect |
7 ms |
4052 KB |
Output isn't correct |
44 |
Correct |
39 ms |
4116 KB |
Output is correct |
45 |
Incorrect |
9 ms |
4436 KB |
Output isn't correct |
46 |
Incorrect |
10 ms |
4432 KB |
Output isn't correct |
47 |
Correct |
46 ms |
4472 KB |
Output is correct |
48 |
Incorrect |
11 ms |
4820 KB |
Output isn't correct |
49 |
Incorrect |
10 ms |
4816 KB |
Output isn't correct |
50 |
Correct |
55 ms |
4820 KB |
Output is correct |
51 |
Incorrect |
11 ms |
5360 KB |
Output isn't correct |
52 |
Incorrect |
11 ms |
5256 KB |
Output isn't correct |
53 |
Correct |
68 ms |
5324 KB |
Output is correct |
54 |
Incorrect |
13 ms |
5716 KB |
Output isn't correct |
55 |
Incorrect |
14 ms |
5820 KB |
Output isn't correct |
56 |
Correct |
80 ms |
5684 KB |
Output is correct |
57 |
Incorrect |
14 ms |
6100 KB |
Output isn't correct |
58 |
Incorrect |
14 ms |
6084 KB |
Output isn't correct |
59 |
Correct |
91 ms |
6184 KB |
Output is correct |
60 |
Incorrect |
16 ms |
6604 KB |
Output isn't correct |
61 |
Incorrect |
16 ms |
6572 KB |
Output isn't correct |
62 |
Correct |
104 ms |
6504 KB |
Output is correct |
63 |
Correct |
94 ms |
6532 KB |
Output is correct |
64 |
Correct |
175 ms |
6532 KB |
Output is correct |
65 |
Correct |
134 ms |
6628 KB |
Output is correct |
66 |
Correct |
107 ms |
6532 KB |
Output is correct |
67 |
Correct |
103 ms |
6640 KB |
Output is correct |
68 |
Correct |
43 ms |
6608 KB |
Output is correct |
69 |
Correct |
43 ms |
6524 KB |
Output is correct |
70 |
Correct |
33 ms |
6604 KB |
Output is correct |
71 |
Correct |
37 ms |
6572 KB |
Output is correct |
72 |
Incorrect |
27 ms |
6572 KB |
Output isn't correct |
73 |
Incorrect |
36 ms |
6772 KB |
Output isn't correct |
74 |
Correct |
53 ms |
6800 KB |
Output is correct |
75 |
Correct |
67 ms |
6888 KB |
Output is correct |
76 |
Correct |
59 ms |
6920 KB |
Output is correct |
77 |
Correct |
55 ms |
6896 KB |
Output is correct |
78 |
Correct |
70 ms |
6868 KB |
Output is correct |
79 |
Correct |
45 ms |
6788 KB |
Output is correct |
80 |
Correct |
55 ms |
6788 KB |
Output is correct |
81 |
Correct |
64 ms |
6808 KB |
Output is correct |
82 |
Correct |
55 ms |
6868 KB |
Output is correct |
83 |
Correct |
73 ms |
6828 KB |
Output is correct |
84 |
Correct |
63 ms |
6716 KB |
Output is correct |
85 |
Correct |
70 ms |
6724 KB |
Output is correct |
86 |
Correct |
70 ms |
6832 KB |
Output is correct |
87 |
Correct |
69 ms |
6836 KB |
Output is correct |
88 |
Correct |
72 ms |
6784 KB |
Output is correct |
89 |
Correct |
75 ms |
6776 KB |
Output is correct |
90 |
Correct |
78 ms |
6744 KB |
Output is correct |
91 |
Correct |
74 ms |
6776 KB |
Output is correct |
92 |
Correct |
82 ms |
6732 KB |
Output is correct |