#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], eating_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 startx, starty, home_x, home_y;
//find Mecho's position
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (field[i][j] == 'M'){
startx = i;
starty = 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]){
if (eating_dis[x][y] < eating_time){
eating_dis[nx][ny] = eating_dis[x][y] + 1;
}else{
bees_dis[nx][ny] = bees_dis[x][y] + 1;
eating_dis[nx][ny] = INT_MAX;
}
bees_q.push({nx, ny});
bees_visited[nx][ny] = true;
}
}
}
mecho_q.push({startx, starty});
mecho_visited[startx][starty] = true;
if (max(eating_dis[startx][starty], bees_dis[startx][starty]) < eating_time && bees_visited[startx][starty])
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] > (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:75:115: warning: 'starty' may be used uninitialized in this function [-Wmaybe-uninitialized]
75 | if (max(eating_dis[startx][starty], bees_dis[startx][starty]) < eating_time && bees_visited[startx][starty])
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
mecho.cpp:75:115: warning: 'startx' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:91:37: warning: 'home_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
91 | int nx = home_x+ dx[i], ny = home_y + dy[i];
| ^~
mecho.cpp:91:17: warning: 'home_x' may be used uninitialized in this function [-Wmaybe-uninitialized]
91 | int nx = home_x+ dx[i], ny = home_y + dy[i];
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6476 KB |
Output is correct |
2 |
Correct |
3 ms |
6540 KB |
Output is correct |
3 |
Correct |
3 ms |
6476 KB |
Output is correct |
4 |
Correct |
3 ms |
6600 KB |
Output is correct |
5 |
Correct |
4 ms |
6604 KB |
Output is correct |
6 |
Correct |
4 ms |
6604 KB |
Output is correct |
7 |
Correct |
547 ms |
10072 KB |
Output is correct |
8 |
Correct |
4 ms |
6604 KB |
Output is correct |
9 |
Correct |
5 ms |
6604 KB |
Output is correct |
10 |
Correct |
4 ms |
6604 KB |
Output is correct |
11 |
Correct |
6 ms |
6604 KB |
Output is correct |
12 |
Correct |
6 ms |
6732 KB |
Output is correct |
13 |
Incorrect |
7 ms |
6740 KB |
Output isn't correct |
14 |
Correct |
10 ms |
6732 KB |
Output is correct |
15 |
Correct |
5 ms |
6604 KB |
Output is correct |
16 |
Correct |
4 ms |
6604 KB |
Output is correct |
17 |
Correct |
5 ms |
6604 KB |
Output is correct |
18 |
Correct |
5 ms |
6604 KB |
Output is correct |
19 |
Correct |
5 ms |
6604 KB |
Output is correct |
20 |
Correct |
7 ms |
6604 KB |
Output is correct |
21 |
Correct |
6 ms |
6604 KB |
Output is correct |
22 |
Correct |
5 ms |
6704 KB |
Output is correct |
23 |
Correct |
6 ms |
6732 KB |
Output is correct |
24 |
Correct |
7 ms |
6732 KB |
Output is correct |
25 |
Correct |
6 ms |
6732 KB |
Output is correct |
26 |
Correct |
6 ms |
6776 KB |
Output is correct |
27 |
Correct |
9 ms |
6732 KB |
Output is correct |
28 |
Correct |
10 ms |
6732 KB |
Output is correct |
29 |
Correct |
7 ms |
6732 KB |
Output is correct |
30 |
Correct |
8 ms |
6732 KB |
Output is correct |
31 |
Correct |
7 ms |
6732 KB |
Output is correct |
32 |
Correct |
9 ms |
6732 KB |
Output is correct |
33 |
Correct |
44 ms |
7796 KB |
Output is correct |
34 |
Correct |
41 ms |
7740 KB |
Output is correct |
35 |
Correct |
82 ms |
7836 KB |
Output is correct |
36 |
Correct |
54 ms |
8012 KB |
Output is correct |
37 |
Correct |
56 ms |
8012 KB |
Output is correct |
38 |
Correct |
102 ms |
8020 KB |
Output is correct |
39 |
Correct |
87 ms |
8144 KB |
Output is correct |
40 |
Correct |
75 ms |
8116 KB |
Output is correct |
41 |
Correct |
126 ms |
8140 KB |
Output is correct |
42 |
Correct |
83 ms |
8604 KB |
Output is correct |
43 |
Correct |
78 ms |
8604 KB |
Output is correct |
44 |
Correct |
165 ms |
8620 KB |
Output is correct |
45 |
Correct |
97 ms |
8780 KB |
Output is correct |
46 |
Correct |
90 ms |
8812 KB |
Output is correct |
47 |
Correct |
203 ms |
8816 KB |
Output is correct |
48 |
Correct |
109 ms |
8908 KB |
Output is correct |
49 |
Correct |
109 ms |
9016 KB |
Output is correct |
50 |
Correct |
228 ms |
8908 KB |
Output is correct |
51 |
Correct |
138 ms |
9284 KB |
Output is correct |
52 |
Correct |
128 ms |
9220 KB |
Output is correct |
53 |
Correct |
285 ms |
9232 KB |
Output is correct |
54 |
Correct |
155 ms |
9412 KB |
Output is correct |
55 |
Correct |
162 ms |
9432 KB |
Output is correct |
56 |
Correct |
354 ms |
9448 KB |
Output is correct |
57 |
Correct |
170 ms |
9544 KB |
Output is correct |
58 |
Correct |
170 ms |
9672 KB |
Output is correct |
59 |
Correct |
378 ms |
9644 KB |
Output is correct |
60 |
Correct |
195 ms |
9836 KB |
Output is correct |
61 |
Correct |
186 ms |
9924 KB |
Output is correct |
62 |
Correct |
422 ms |
9804 KB |
Output is correct |
63 |
Correct |
504 ms |
9972 KB |
Output is correct |
64 |
Correct |
623 ms |
9840 KB |
Output is correct |
65 |
Correct |
659 ms |
9848 KB |
Output is correct |
66 |
Correct |
556 ms |
9836 KB |
Output is correct |
67 |
Correct |
559 ms |
9924 KB |
Output is correct |
68 |
Correct |
483 ms |
9872 KB |
Output is correct |
69 |
Correct |
520 ms |
9920 KB |
Output is correct |
70 |
Correct |
455 ms |
9888 KB |
Output is correct |
71 |
Correct |
487 ms |
9988 KB |
Output is correct |
72 |
Incorrect |
466 ms |
9924 KB |
Output isn't correct |
73 |
Incorrect |
440 ms |
10052 KB |
Output isn't correct |
74 |
Correct |
468 ms |
10232 KB |
Output is correct |
75 |
Correct |
532 ms |
10060 KB |
Output is correct |
76 |
Correct |
482 ms |
10180 KB |
Output is correct |
77 |
Correct |
474 ms |
10180 KB |
Output is correct |
78 |
Correct |
506 ms |
10052 KB |
Output is correct |
79 |
Correct |
478 ms |
10100 KB |
Output is correct |
80 |
Correct |
468 ms |
10060 KB |
Output is correct |
81 |
Correct |
503 ms |
9992 KB |
Output is correct |
82 |
Correct |
521 ms |
10064 KB |
Output is correct |
83 |
Correct |
642 ms |
9984 KB |
Output is correct |
84 |
Correct |
532 ms |
10064 KB |
Output is correct |
85 |
Correct |
545 ms |
10064 KB |
Output is correct |
86 |
Correct |
491 ms |
10052 KB |
Output is correct |
87 |
Correct |
506 ms |
10156 KB |
Output is correct |
88 |
Correct |
513 ms |
10008 KB |
Output is correct |
89 |
Correct |
514 ms |
10000 KB |
Output is correct |
90 |
Correct |
504 ms |
9892 KB |
Output is correct |
91 |
Correct |
497 ms |
9932 KB |
Output is correct |
92 |
Correct |
540 ms |
10016 KB |
Output is correct |