#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 800;
vector <string> field(MAX_N);
bool bees_visited[MAX_N][MAX_N], mecho_visited[MAX_N][MAX_N];
int mecho_dis[MAX_N][MAX_N], bees_dis[MAX_N][MAX_N];
queue <pair <int, int>> mecho_q, bees_q;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int n, s;
bool valid_sq(int x, int y, bool is_mecho){
if (x >= 0 && x < n && y >= 0 && y < n){
if (is_mecho && (field[x][y] == 'G' || field[x][y] == 'D' || field[x][y] == 'M'))
return true;
else if (!is_mecho && (field[x][y] == 'G' || field[x][y] == 'M'))
return true;
return false;
}
return false;
}
int main()
{
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
cin >> n >> s;
for (int i = 0; i < n; i++)
cin >> field[i];
vector <pair <int, int>> hives;
int mechox, mechoy, home_x, home_y;
//find starting points position
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (field[i][j] == 'M'){
mechox = i;
mechoy = j;
}else if (field[i][j] == 'H'){
hives.push_back({i, j});
}else if (field[i][j] == 'D'){
home_x = i;
home_y = j;
}
}
}
int l = 0, r = n*n;
while (l <= r){
int eating_time = (l + r) / 2;
memset(bees_visited, false, sizeof(bees_visited));
memset(mecho_visited, false, sizeof(mecho_visited));
memset(bees_dis, 0, sizeof(bees_dis));
memset(mecho_dis, 0, sizeof(mecho_dis));
//move bees
for (auto i : hives){
bees_q.push({i.first, i.second});
bees_visited[i.first][i.second] = true;
}
while (!bees_q.empty()){
int x = bees_q.front().first, y = bees_q.front().second;
bees_q.pop();
for (int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if (valid_sq(nx, ny, false) && !bees_visited[nx][ny]){
bees_dis[nx][ny] = bees_dis[x][y] + 1;
bees_q.push({nx, ny});
bees_visited[nx][ny] = true;
}
}
}
mecho_q.push({mechox, mechoy});
mecho_visited[mechox][mechoy] = true;
if (bees_dis[mechox][mechoy] - eating_time < eating_time)
mecho_q.pop();
while (!mecho_q.empty()){
int x = mecho_q.front().first, y = mecho_q.front().second;
mecho_q.pop();
for (int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if (valid_sq(nx, ny, true) && !mecho_visited[nx][ny] && (mecho_dis[x][y] + 1) / s < bees_dis[nx][ny]){
mecho_visited[nx][ny] = true;
mecho_q.push({nx, ny});
mecho_dis[nx][ny] = mecho_dis[x][y] + 1;
}
}
}
bool mecho_reached = false;
for (int i = 0; i < 4; i++){
int nx = home_x+ dx[i], ny = home_y + dy[i];
if (valid_sq(nx, ny, true)){
if (bees_dis[nx][ny] - eating_time > (mecho_dis[nx][ny] / s) && mecho_visited[nx][ny]){
mecho_reached = true;
break;
}
}
}
if (mecho_reached)
l = eating_time + 1;
else
r = eating_time - 1;
}
cout << l - 1 << '\n';
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:70:36: warning: 'mechoy' may be used uninitialized in this function [-Wmaybe-uninitialized]
70 | if (bees_dis[mechox][mechoy] - eating_time < eating_time)
| ~~~~~~~~~~~~~~~~~~~~~~~^
mecho.cpp:70:36: warning: 'mechox' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:86:37: warning: 'home_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
86 | int nx = home_x+ dx[i], ny = home_y + dy[i];
| ^~
mecho.cpp:86:17: warning: 'home_x' may be used uninitialized in this function [-Wmaybe-uninitialized]
86 | int nx = home_x+ dx[i], ny = home_y + dy[i];
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6476 KB |
Output is correct |
2 |
Correct |
4 ms |
6476 KB |
Output is correct |
3 |
Correct |
4 ms |
6476 KB |
Output is correct |
4 |
Correct |
4 ms |
6476 KB |
Output is correct |
5 |
Incorrect |
5 ms |
6544 KB |
Output isn't correct |
6 |
Correct |
4 ms |
6476 KB |
Output is correct |
7 |
Correct |
511 ms |
7572 KB |
Output is correct |
8 |
Correct |
4 ms |
6476 KB |
Output is correct |
9 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
10 |
Correct |
4 ms |
6476 KB |
Output is correct |
11 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
12 |
Correct |
6 ms |
6476 KB |
Output is correct |
13 |
Correct |
6 ms |
6476 KB |
Output is correct |
14 |
Correct |
7 ms |
6476 KB |
Output is correct |
15 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
16 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
17 |
Incorrect |
5 ms |
6476 KB |
Output isn't correct |
18 |
Incorrect |
7 ms |
6476 KB |
Output isn't correct |
19 |
Incorrect |
6 ms |
6476 KB |
Output isn't correct |
20 |
Incorrect |
5 ms |
6584 KB |
Output isn't correct |
21 |
Incorrect |
6 ms |
6476 KB |
Output isn't correct |
22 |
Incorrect |
7 ms |
6468 KB |
Output isn't correct |
23 |
Incorrect |
6 ms |
6476 KB |
Output isn't correct |
24 |
Incorrect |
6 ms |
6476 KB |
Output isn't correct |
25 |
Incorrect |
6 ms |
6476 KB |
Output isn't correct |
26 |
Incorrect |
7 ms |
6480 KB |
Output isn't correct |
27 |
Incorrect |
7 ms |
6604 KB |
Output isn't correct |
28 |
Incorrect |
8 ms |
6592 KB |
Output isn't correct |
29 |
Incorrect |
7 ms |
6476 KB |
Output isn't correct |
30 |
Incorrect |
7 ms |
6476 KB |
Output isn't correct |
31 |
Incorrect |
9 ms |
6604 KB |
Output isn't correct |
32 |
Incorrect |
8 ms |
6476 KB |
Output isn't correct |
33 |
Incorrect |
57 ms |
6860 KB |
Output isn't correct |
34 |
Incorrect |
54 ms |
6860 KB |
Output isn't correct |
35 |
Incorrect |
71 ms |
6860 KB |
Output isn't correct |
36 |
Incorrect |
59 ms |
6860 KB |
Output isn't correct |
37 |
Incorrect |
71 ms |
6980 KB |
Output isn't correct |
38 |
Incorrect |
91 ms |
6920 KB |
Output isn't correct |
39 |
Incorrect |
72 ms |
6860 KB |
Output isn't correct |
40 |
Incorrect |
77 ms |
6904 KB |
Output isn't correct |
41 |
Incorrect |
130 ms |
6860 KB |
Output isn't correct |
42 |
Incorrect |
106 ms |
7116 KB |
Output isn't correct |
43 |
Incorrect |
119 ms |
7168 KB |
Output isn't correct |
44 |
Incorrect |
147 ms |
7116 KB |
Output isn't correct |
45 |
Incorrect |
113 ms |
7128 KB |
Output isn't correct |
46 |
Incorrect |
130 ms |
7176 KB |
Output isn't correct |
47 |
Incorrect |
180 ms |
7236 KB |
Output isn't correct |
48 |
Incorrect |
139 ms |
7256 KB |
Output isn't correct |
49 |
Incorrect |
150 ms |
7260 KB |
Output isn't correct |
50 |
Incorrect |
196 ms |
7276 KB |
Output isn't correct |
51 |
Incorrect |
166 ms |
7308 KB |
Output isn't correct |
52 |
Incorrect |
167 ms |
7244 KB |
Output isn't correct |
53 |
Incorrect |
243 ms |
7316 KB |
Output isn't correct |
54 |
Incorrect |
181 ms |
7368 KB |
Output isn't correct |
55 |
Incorrect |
204 ms |
7304 KB |
Output isn't correct |
56 |
Incorrect |
296 ms |
7272 KB |
Output isn't correct |
57 |
Incorrect |
229 ms |
7488 KB |
Output isn't correct |
58 |
Incorrect |
272 ms |
7372 KB |
Output isn't correct |
59 |
Incorrect |
359 ms |
7412 KB |
Output isn't correct |
60 |
Incorrect |
257 ms |
7460 KB |
Output isn't correct |
61 |
Incorrect |
298 ms |
7480 KB |
Output isn't correct |
62 |
Incorrect |
412 ms |
7536 KB |
Output isn't correct |
63 |
Incorrect |
571 ms |
7360 KB |
Output isn't correct |
64 |
Incorrect |
433 ms |
7492 KB |
Output isn't correct |
65 |
Incorrect |
438 ms |
7488 KB |
Output isn't correct |
66 |
Incorrect |
485 ms |
7492 KB |
Output isn't correct |
67 |
Correct |
527 ms |
7464 KB |
Output is correct |
68 |
Incorrect |
450 ms |
7500 KB |
Output isn't correct |
69 |
Incorrect |
467 ms |
7492 KB |
Output isn't correct |
70 |
Incorrect |
448 ms |
7500 KB |
Output isn't correct |
71 |
Incorrect |
444 ms |
7500 KB |
Output isn't correct |
72 |
Incorrect |
458 ms |
7500 KB |
Output isn't correct |
73 |
Correct |
396 ms |
7876 KB |
Output is correct |
74 |
Correct |
501 ms |
7880 KB |
Output is correct |
75 |
Correct |
498 ms |
7756 KB |
Output is correct |
76 |
Correct |
512 ms |
7872 KB |
Output is correct |
77 |
Correct |
450 ms |
7748 KB |
Output is correct |
78 |
Correct |
473 ms |
7628 KB |
Output is correct |
79 |
Correct |
559 ms |
7628 KB |
Output is correct |
80 |
Correct |
468 ms |
7724 KB |
Output is correct |
81 |
Correct |
488 ms |
7724 KB |
Output is correct |
82 |
Correct |
498 ms |
7724 KB |
Output is correct |
83 |
Correct |
490 ms |
7732 KB |
Output is correct |
84 |
Correct |
571 ms |
7748 KB |
Output is correct |
85 |
Correct |
503 ms |
7628 KB |
Output is correct |
86 |
Correct |
481 ms |
7628 KB |
Output is correct |
87 |
Correct |
492 ms |
7748 KB |
Output is correct |
88 |
Correct |
479 ms |
7612 KB |
Output is correct |
89 |
Correct |
552 ms |
7628 KB |
Output is correct |
90 |
Correct |
473 ms |
7628 KB |
Output is correct |
91 |
Correct |
523 ms |
7748 KB |
Output is correct |
92 |
Correct |
512 ms |
7628 KB |
Output is correct |