#include <bits/stdc++.h>
#define MAX_N 800
using namespace std;
int n, s;
char grid[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
int beed[MAX_N][MAX_N];
int steps[MAX_N][MAX_N];
bool check(int t) {
pair<int, int> mecho_pos, home_pos;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
visited[i][j] = false;
if(grid[i][j] == 'M') {
mecho_pos = {i, j};
}
if(grid[i][j] == 'D') {
home_pos = {i, j};
}
}
}
if(t >= beed[mecho_pos.first][mecho_pos.second]) {
return false;
}
queue<pair<pair<int, int>, int>> newq;
visited[mecho_pos.first][mecho_pos.second] = true;
newq.push({mecho_pos, 0});
while(!newq.empty()) {
pair<pair<int, int>, int> curr = newq.front();
newq.pop();
int tmp = t + ((curr.second + 1) / s);
if(curr.first.first + 1 < n && tmp < beed[curr.first.first + 1][curr.first.second] && !visited[curr.first.first + 1][curr.first.second] && (grid[curr.first.first + 1][curr.first.second] == 'G' || grid[curr.first.first + 1][curr.first.second] == 'M')) {
visited[curr.first.first + 1][curr.first.second] = true;
newq.push({{curr.first.first + 1, curr.first.second}, curr.second + 1});
}
if(curr.first.first - 1 >= 0 && tmp < beed[curr.first.first - 1][curr.first.second] && !visited[curr.first.first - 1][curr.second] && (grid[curr.first.first - 1][curr.first.second] == 'G' || grid[curr.first.first - 1][curr.first.second] == 'M')) {
visited[curr.first.first - 1][curr.first.second] = true;
newq.push({{curr.first.first - 1, curr.first.second}, curr.second + 1});
}
if(curr.first.second + 1 < n && tmp < beed[curr.first.first][curr.first.second + 1] && !visited[curr.first.first][curr.first.second + 1] && (grid[curr.first.first][curr.first.second + 1] == 'G' || grid[curr.first.first][curr.first.second + 1] == 'M')) {
visited[curr.first.first][curr.first.second + 1] = true;
newq.push({{curr.first.first, curr.first.second + 1}, curr.second + 1});
}
if(curr.first.second - 1 >= 0 && tmp < beed[curr.first.first][curr.first.second - 1] && !visited[curr.first.first][curr.first.second - 1] && (grid[curr.first.first][curr.first.second - 1] == 'G' || grid[curr.first.first][curr.first.second - 1] == 'M')) {
visited[curr.first.first][curr.first.second - 1] = true;
newq.push({{curr.first.first, curr.first.second - 1}, curr.second + 1});
}
}
if(home_pos.first + 1 < n && visited[home_pos.first + 1][home_pos.second] && (grid[home_pos.first + 1][home_pos.second] == 'M' || grid[home_pos.first + 1][home_pos.second] == 'G')) {
return true;
}
if(home_pos.first - 1 >= 0 && visited[home_pos.first - 1][home_pos.second] && (grid[home_pos.first - 1][home_pos.second] == 'M' || grid[home_pos.first - 1][home_pos.second] == 'G')) {
return true;
}
if(home_pos.second + 1 < n && visited[home_pos.first][home_pos.second + 1] && (grid[home_pos.first][home_pos.second + 1] == 'M' || grid[home_pos.first][home_pos.second + 1] == 'G')) {
return true;
}
if(home_pos.second - 1 >= 0 && visited[home_pos.first][home_pos.second - 1] && (grid[home_pos.first][home_pos.second - 1] == 'M' || grid[home_pos.first][home_pos.second - 1] == 'G')) {
return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
vector<pair<int, int>> hives;
pair<int, int> mecho_pos;
cin >> n >> s;
for(int i = 0; i < n; i++) {
string tmp;
cin >> tmp;
for(int j = 0; j < n; j++) {
grid[i][j] = tmp[j];
beed[i][j] = INT_MAX - n * n;
if(grid[i][j] == 'H') {
hives.push_back({i, j});
}
if(grid[i][j] == 'M') {
mecho_pos = {i, j};
}
}
}
queue<pair<int, int>> q;
visited[mecho_pos.first][mecho_pos.second] = true;
steps[mecho_pos.first][mecho_pos.second] = 0;
q.push(mecho_pos);
while(!q.empty()) {
pair<int, int> curr = q.front();
q.pop();
if(curr.first + 1 < n && !visited[curr.first + 1][curr.second] && (grid[curr.first + 1][curr.second] == 'G' || grid[curr.first + 1][curr.second] == 'M')) {
visited[curr.first + 1][curr.second] = true;
steps[curr.first + 1][curr.second] = steps[curr.first][curr.second] + 1;
q.push({curr.first + 1, curr.second});
}
if(curr.first - 1 >= 0 && !visited[curr.first - 1][curr.second] && (grid[curr.first - 1][curr.second] == 'G' || grid[curr.first - 1][curr.second] == 'M')) {
visited[curr.first - 1][curr.second] = true;
steps[curr.first - 1][curr.second] = steps[curr.first][curr.second] + 1;
q.push({curr.first - 1, curr.second});
}
if(curr.second + 1 < n && !visited[curr.first][curr.second + 1] && (grid[curr.first][curr.second + 1] == 'G' || grid[curr.first][curr.second + 1] == 'M')) {
visited[curr.first][curr.second + 1] = true;
steps[curr.first][curr.second + 1] = steps[curr.first][curr.second] + 1;
q.push({curr.first, curr.second + 1});
}
if(curr.second - 1 >= 0 && !visited[curr.first][curr.second - 1] && (grid[curr.first][curr.second - 1] == 'G' || grid[curr.first][curr.second - 1] == 'M')) {
visited[curr.first][curr.second - 1] = true;
steps[curr.first][curr.second - 1] = steps[curr.first][curr.second] + 1;
q.push({curr.first, curr.second - 1});
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
visited[i][j] = false;
}
}
for(auto iter : hives) {
visited[iter.first][iter.second] = true;
beed[iter.first][iter.second] = 0;
q.push(iter);
}
while(!q.empty()) {
pair<int, int> curr = q.front();
q.pop();
if(curr.first + 1 < n && !visited[curr.first + 1][curr.second] && (grid[curr.first + 1][curr.second] == 'G' || grid[curr.first + 1][curr.second] == 'M')) {
visited[curr.first + 1][curr.second] = true;
beed[curr.first + 1][curr.second] = beed[curr.first][curr.second] + 1;
q.push({curr.first + 1, curr.second});
}
if(curr.first - 1 >= 0 && !visited[curr.first - 1][curr.second] && (grid[curr.first - 1][curr.second] == 'G' || grid[curr.first - 1][curr.second] == 'M')) {
visited[curr.first - 1][curr.second] = true;
beed[curr.first - 1][curr.second] = beed[curr.first][curr.second] + 1;
q.push({curr.first - 1, curr.second});
}
if(curr.second + 1 < n && !visited[curr.first][curr.second + 1] && (grid[curr.first][curr.second + 1] == 'G' || grid[curr.first][curr.second + 1] == 'M')) {
visited[curr.first][curr.second + 1] = true;
beed[curr.first][curr.second + 1] = beed[curr.first][curr.second] + 1;
q.push({curr.first, curr.second + 1});
}
if(curr.second - 1 >= 0 && !visited[curr.first][curr.second - 1] && (grid[curr.first][curr.second - 1] == 'G' || grid[curr.first][curr.second - 1] == 'M')) {
visited[curr.first][curr.second - 1] = true;
beed[curr.first][curr.second - 1] = beed[curr.first][curr.second] + 1;
q.push({curr.first, curr.second - 1});
}
}
int l = 0, r = n * n;
int ans = -1;
// true true true ... (true) false false
while(l <= r) {
int mid = l + (r - l) / 2;
if(check(mid)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2548 KB |
Output is correct |
7 |
Execution timed out |
1063 ms |
7212 KB |
Time limit exceeded |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2648 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2648 KB |
Output is correct |
24 |
Correct |
1 ms |
2648 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
2 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4696 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
6 ms |
5212 KB |
Output is correct |
34 |
Correct |
5 ms |
5212 KB |
Output is correct |
35 |
Correct |
331 ms |
5528 KB |
Output is correct |
36 |
Correct |
7 ms |
5384 KB |
Output is correct |
37 |
Correct |
8 ms |
5212 KB |
Output is correct |
38 |
Correct |
374 ms |
5532 KB |
Output is correct |
39 |
Correct |
9 ms |
5464 KB |
Output is correct |
40 |
Correct |
7 ms |
5212 KB |
Output is correct |
41 |
Correct |
349 ms |
5628 KB |
Output is correct |
42 |
Correct |
11 ms |
5468 KB |
Output is correct |
43 |
Correct |
9 ms |
5468 KB |
Output is correct |
44 |
Correct |
453 ms |
5660 KB |
Output is correct |
45 |
Correct |
13 ms |
5464 KB |
Output is correct |
46 |
Correct |
10 ms |
5604 KB |
Output is correct |
47 |
Correct |
518 ms |
5888 KB |
Output is correct |
48 |
Correct |
17 ms |
5732 KB |
Output is correct |
49 |
Correct |
12 ms |
5724 KB |
Output is correct |
50 |
Correct |
567 ms |
6252 KB |
Output is correct |
51 |
Correct |
20 ms |
5980 KB |
Output is correct |
52 |
Correct |
14 ms |
6060 KB |
Output is correct |
53 |
Correct |
558 ms |
6320 KB |
Output is correct |
54 |
Correct |
27 ms |
6232 KB |
Output is correct |
55 |
Correct |
23 ms |
6232 KB |
Output is correct |
56 |
Correct |
517 ms |
6576 KB |
Output is correct |
57 |
Correct |
24 ms |
6492 KB |
Output is correct |
58 |
Correct |
18 ms |
6492 KB |
Output is correct |
59 |
Correct |
467 ms |
6992 KB |
Output is correct |
60 |
Correct |
27 ms |
6748 KB |
Output is correct |
61 |
Correct |
23 ms |
6488 KB |
Output is correct |
62 |
Incorrect |
274 ms |
6948 KB |
Output isn't correct |
63 |
Incorrect |
61 ms |
6748 KB |
Output isn't correct |
64 |
Incorrect |
63 ms |
6744 KB |
Output isn't correct |
65 |
Incorrect |
55 ms |
6752 KB |
Output isn't correct |
66 |
Incorrect |
68 ms |
6748 KB |
Output isn't correct |
67 |
Correct |
55 ms |
6752 KB |
Output is correct |
68 |
Incorrect |
41 ms |
6744 KB |
Output isn't correct |
69 |
Incorrect |
45 ms |
6748 KB |
Output isn't correct |
70 |
Incorrect |
45 ms |
6748 KB |
Output isn't correct |
71 |
Incorrect |
48 ms |
6776 KB |
Output isn't correct |
72 |
Correct |
37 ms |
6744 KB |
Output is correct |
73 |
Correct |
338 ms |
7280 KB |
Output is correct |
74 |
Execution timed out |
1031 ms |
7252 KB |
Time limit exceeded |
75 |
Execution timed out |
1014 ms |
7612 KB |
Time limit exceeded |
76 |
Execution timed out |
1040 ms |
7284 KB |
Time limit exceeded |
77 |
Execution timed out |
1049 ms |
7220 KB |
Time limit exceeded |
78 |
Execution timed out |
1032 ms |
7264 KB |
Time limit exceeded |
79 |
Execution timed out |
1074 ms |
7204 KB |
Time limit exceeded |
80 |
Execution timed out |
1033 ms |
7336 KB |
Time limit exceeded |
81 |
Execution timed out |
1044 ms |
7248 KB |
Time limit exceeded |
82 |
Execution timed out |
1020 ms |
7216 KB |
Time limit exceeded |
83 |
Correct |
845 ms |
7320 KB |
Output is correct |
84 |
Correct |
739 ms |
7048 KB |
Output is correct |
85 |
Correct |
692 ms |
7060 KB |
Output is correct |
86 |
Correct |
772 ms |
7296 KB |
Output is correct |
87 |
Correct |
846 ms |
7236 KB |
Output is correct |
88 |
Correct |
883 ms |
7124 KB |
Output is correct |
89 |
Correct |
849 ms |
7068 KB |
Output is correct |
90 |
Correct |
907 ms |
7116 KB |
Output is correct |
91 |
Correct |
872 ms |
7244 KB |
Output is correct |
92 |
Correct |
979 ms |
7312 KB |
Output is correct |