#include <bits/stdc++.h>
#define MAX_N 801
#define UNVISITED 0
#define BEAR 1
#define BEE 2
using namespace std;
int n, s;
char grid[MAX_N][MAX_N];
int visited[MAX_N][MAX_N];
int beed[MAX_N][MAX_N];
bool check(int t) {
pair<int, int> mecho_pos;
pair<int, int> home_pos;
vector<pair<int, int>> hive_edges;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
visited[i][j] = UNVISITED;
beed[i][j] = -69;
if(grid[i][j] == 'H') {
hive_edges.push_back({i, j});
}
if(grid[i][j] == 'M') {
mecho_pos = {i, j};
}
if(grid[i][j] == 'D') {
home_pos = {i, j};
}
}
}
queue<pair<int, int>> beeq;
vector<pair<int, int>> bees_edge;
for(auto iter : hive_edges) {
visited[iter.first][iter.second] = BEE;
beed[iter.first][iter.second] = 0;
beeq.push(iter);
}
while(!beeq.empty()) {
pair<int, int> curr = beeq.front();
beeq.pop();
if(beed[curr.first][curr.second] == t) {
bees_edge.push_back(curr);
continue;
}
if(curr.first + 1 < n && visited[curr.first + 1][curr.second] == UNVISITED && (grid[curr.first + 1][curr.second] == 'G' || grid[curr.first + 1][curr.second] == 'M')) {
visited[curr.first + 1][curr.second] = BEE;
beed[curr.first + 1][curr.second] = beed[curr.first][curr.second] + 1;
beeq.push({curr.first + 1, curr.second});
}
if(curr.first - 1 >= 0 && visited[curr.first - 1][curr.second] == UNVISITED && (grid[curr.first - 1][curr.second] == 'G' || grid[curr.first - 1][curr.second] == 'M')) {
visited[curr.first - 1][curr.second] = BEE;
beed[curr.first - 1][curr.second] = beed[curr.first][curr.second] + 1;
beeq.push({curr.first - 1, curr.second});
}
if(curr.second + 1 < n && visited[curr.first][curr.second + 1] == UNVISITED && (grid[curr.first][curr.second + 1] == 'G' || grid[curr.first][curr.second + 1] == 'M')) {
visited[curr.first][curr.second + 1] = BEE;
beed[curr.first][curr.second + 1] = beed[curr.first][curr.second] + 1;
beeq.push({curr.first, curr.second + 1});
}
if(curr.second - 1 >= 0 && visited[curr.first][curr.second - 1] == UNVISITED && (grid[curr.first][curr.second - 1] == 'G' || grid[curr.first][curr.second - 1] == 'M')) {
visited[curr.first][curr.second - 1] = BEE;
beed[curr.first][curr.second - 1] = beed[curr.first][curr.second] + 1;
beeq.push({curr.first, curr.second - 1});
}
}
if(visited[mecho_pos.first][mecho_pos.second] == BEE) {
return false;
}
set<pair<int, int>> mecho_edge;
mecho_edge.insert(mecho_pos);
bool found = false;
while(!found) {
if(mecho_edge.size() == 0) {
return false;
}
queue<pair<pair<int, int>, int>> q; // mecho queue
for(auto iter : mecho_edge) {
visited[iter.first][iter.second] = BEAR;
q.push({iter, 0});
}
mecho_edge.clear();
while(!q.empty()) {
pair<pair<int, int>, int> curr = q.front();
q.pop();
if(curr.first.first + 1 < n && visited[curr.first.first + 1][curr.first.second] == UNVISITED && (grid[curr.first.first + 1][curr.first.second] == 'G' || grid[curr.first.first + 1][curr.first.second] == 'D')){
if(curr.second == s - 1) {
visited[curr.first.first + 1][curr.first.second] = BEAR;
mecho_edge.insert({curr.first.first + 1, curr.first.second});
} else {
visited[curr.first.first + 1][curr.first.second] = BEAR;
q.push({{curr.first.first + 1, curr.first.second}, curr.second + 1});
}
}
if(curr.first.first - 1 >= 0 && visited[curr.first.first - 1][curr.first.second] == UNVISITED && (grid[curr.first.first - 1][curr.first.second] == 'G' || grid[curr.first.first - 1][curr.first.second] == 'D')){
if(curr.second == s - 1) {
visited[curr.first.first - 1][curr.first.second] = BEAR;
mecho_edge.insert({curr.first.first - 1, curr.first.second});
} else {
visited[curr.first.first - 1][curr.first.second] = BEAR;
q.push({{curr.first.first - 1, curr.first.second}, curr.second + 1});
}
}
if(curr.first.second + 1 < n && visited[curr.first.first][curr.first.second + 1] == UNVISITED && (grid[curr.first.first][curr.first.second + 1] == 'G' || grid[curr.first.first][curr.first.second + 1] == 'D')){
if(curr.second == s - 1) {
visited[curr.first.first][curr.first.second + 1] = BEAR;
mecho_edge.insert({curr.first.first, curr.first.second + 1});
} else {
visited[curr.first.first][curr.first.second + 1] = BEAR;
q.push({{curr.first.first, curr.first.second + 1}, curr.second + 1});
}
}
if(curr.first.second - 1 >= 0 && visited[curr.first.first][curr.first.second - 1] == UNVISITED && (grid[curr.first.first][curr.first.second - 1] == 'G' || grid[curr.first.first][curr.first.second - 1] == 'D')){
if(curr.second == s - 1) {
visited[curr.first.first][curr.first.second - 1] = BEAR;
mecho_edge.insert({curr.first.first, curr.first.second - 1});
} else {
visited[curr.first.first][curr.first.second - 1] = BEAR;
q.push({{curr.first.first, curr.first.second - 1}, curr.second + 1});
}
}
}
if(visited[home_pos.first][home_pos.second] == BEAR) {
found = true;
break;
}
vector<pair<int, int>> tmpedge;
for(auto iter : bees_edge) {
if(iter.first + 1 < n && visited[iter.first + 1][iter.second] == UNVISITED && grid[iter.first + 1][iter.second] == 'G') {
visited[iter.first + 1][iter.second] = true;
tmpedge.push_back({iter.first + 1, iter.second});
}
if(iter.first - 1 >= 0 && visited[iter.first - 1][iter.second] == UNVISITED && grid[iter.first - 1][iter.second] == 'G') {
visited[iter.first - 1][iter.second] = true;
tmpedge.push_back({iter.first - 1, iter.second});
}
if(iter.second + 1 < n && visited[iter.first][iter.second + 1] == UNVISITED && grid[iter.first][iter.second + 1] == 'G') {
visited[iter.first][iter.second + 1] = true;
tmpedge.push_back({iter.first, iter.second + 1});
}
if(iter.second - 1 >= 0 && visited[iter.first][iter.second - 1] == UNVISITED && grid[iter.first][iter.second - 1] == 'G') {
visited[iter.first][iter.second - 1] = true;
tmpedge.push_back({iter.first, iter.second - 1});
}
}
bees_edge.clear();
bees_edge = tmpedge;
}
if(found) {
return true;
} else {
return false;
}
}
int main() {
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];
}
}
int l = 0, r = n * n + 1;
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 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2500 KB |
Output is correct |
7 |
Correct |
185 ms |
6464 KB |
Output is correct |
8 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2484 KB |
Output is correct |
12 |
Correct |
1 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 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Incorrect |
20 ms |
4988 KB |
Output isn't correct |
34 |
Correct |
17 ms |
4956 KB |
Output is correct |
35 |
Correct |
22 ms |
4956 KB |
Output is correct |
36 |
Incorrect |
34 ms |
4956 KB |
Output isn't correct |
37 |
Correct |
24 ms |
5028 KB |
Output is correct |
38 |
Correct |
28 ms |
4956 KB |
Output is correct |
39 |
Incorrect |
37 ms |
4956 KB |
Output isn't correct |
40 |
Correct |
26 ms |
4956 KB |
Output is correct |
41 |
Correct |
38 ms |
5064 KB |
Output is correct |
42 |
Incorrect |
44 ms |
4952 KB |
Output isn't correct |
43 |
Correct |
34 ms |
5084 KB |
Output is correct |
44 |
Correct |
48 ms |
4956 KB |
Output is correct |
45 |
Incorrect |
62 ms |
5136 KB |
Output isn't correct |
46 |
Correct |
38 ms |
4956 KB |
Output is correct |
47 |
Correct |
56 ms |
4956 KB |
Output is correct |
48 |
Incorrect |
71 ms |
5288 KB |
Output isn't correct |
49 |
Correct |
55 ms |
5208 KB |
Output is correct |
50 |
Correct |
76 ms |
5208 KB |
Output is correct |
51 |
Incorrect |
82 ms |
5480 KB |
Output isn't correct |
52 |
Correct |
60 ms |
5480 KB |
Output is correct |
53 |
Correct |
88 ms |
5468 KB |
Output is correct |
54 |
Incorrect |
91 ms |
5464 KB |
Output isn't correct |
55 |
Correct |
67 ms |
5464 KB |
Output is correct |
56 |
Correct |
95 ms |
5712 KB |
Output is correct |
57 |
Incorrect |
109 ms |
5872 KB |
Output isn't correct |
58 |
Correct |
74 ms |
5720 KB |
Output is correct |
59 |
Correct |
111 ms |
5916 KB |
Output is correct |
60 |
Incorrect |
133 ms |
5980 KB |
Output isn't correct |
61 |
Correct |
84 ms |
5980 KB |
Output is correct |
62 |
Correct |
122 ms |
5980 KB |
Output is correct |
63 |
Correct |
256 ms |
6112 KB |
Output is correct |
64 |
Correct |
226 ms |
5980 KB |
Output is correct |
65 |
Correct |
251 ms |
5976 KB |
Output is correct |
66 |
Incorrect |
278 ms |
5980 KB |
Output isn't correct |
67 |
Correct |
280 ms |
6080 KB |
Output is correct |
68 |
Correct |
226 ms |
6112 KB |
Output is correct |
69 |
Correct |
199 ms |
6100 KB |
Output is correct |
70 |
Correct |
205 ms |
6224 KB |
Output is correct |
71 |
Correct |
251 ms |
6112 KB |
Output is correct |
72 |
Correct |
244 ms |
5976 KB |
Output is correct |
73 |
Correct |
153 ms |
7332 KB |
Output is correct |
74 |
Correct |
225 ms |
6480 KB |
Output is correct |
75 |
Correct |
209 ms |
6392 KB |
Output is correct |
76 |
Correct |
224 ms |
6432 KB |
Output is correct |
77 |
Correct |
190 ms |
6380 KB |
Output is correct |
78 |
Correct |
203 ms |
7132 KB |
Output is correct |
79 |
Correct |
190 ms |
6480 KB |
Output is correct |
80 |
Correct |
194 ms |
6236 KB |
Output is correct |
81 |
Correct |
226 ms |
6236 KB |
Output is correct |
82 |
Correct |
200 ms |
6344 KB |
Output is correct |
83 |
Correct |
196 ms |
7116 KB |
Output is correct |
84 |
Correct |
195 ms |
6300 KB |
Output is correct |
85 |
Correct |
220 ms |
6480 KB |
Output is correct |
86 |
Correct |
194 ms |
6296 KB |
Output is correct |
87 |
Correct |
195 ms |
6236 KB |
Output is correct |
88 |
Correct |
195 ms |
7060 KB |
Output is correct |
89 |
Correct |
198 ms |
6236 KB |
Output is correct |
90 |
Correct |
183 ms |
6488 KB |
Output is correct |
91 |
Correct |
191 ms |
6552 KB |
Output is correct |
92 |
Correct |
197 ms |
6756 KB |
Output is correct |