#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 (bees_visited[startx][starty] && eating_time > 0)
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:40: warning: 'starty' may be used uninitialized in this function [-Wmaybe-uninitialized]
75 | if (bees_visited[startx][starty] && eating_time > 0)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
mecho.cpp:75:40: 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 |
Incorrect |
3 ms |
6476 KB |
Output isn't correct |
2 |
Incorrect |
4 ms |
6600 KB |
Output isn't correct |
3 |
Incorrect |
3 ms |
6476 KB |
Output isn't correct |
4 |
Incorrect |
4 ms |
6604 KB |
Output isn't correct |
5 |
Correct |
4 ms |
6604 KB |
Output is correct |
6 |
Incorrect |
4 ms |
6604 KB |
Output isn't correct |
7 |
Incorrect |
454 ms |
9952 KB |
Output isn't correct |
8 |
Correct |
4 ms |
6604 KB |
Output is correct |
9 |
Incorrect |
5 ms |
6604 KB |
Output isn't correct |
10 |
Incorrect |
5 ms |
6604 KB |
Output isn't correct |
11 |
Incorrect |
4 ms |
6604 KB |
Output isn't correct |
12 |
Correct |
6 ms |
6732 KB |
Output is correct |
13 |
Correct |
5 ms |
6732 KB |
Output is correct |
14 |
Correct |
9 ms |
6776 KB |
Output is correct |
15 |
Incorrect |
5 ms |
6604 KB |
Output isn't correct |
16 |
Incorrect |
5 ms |
6604 KB |
Output isn't correct |
17 |
Incorrect |
5 ms |
6668 KB |
Output isn't correct |
18 |
Incorrect |
5 ms |
6604 KB |
Output isn't correct |
19 |
Incorrect |
7 ms |
6604 KB |
Output isn't correct |
20 |
Incorrect |
7 ms |
6676 KB |
Output isn't correct |
21 |
Incorrect |
6 ms |
6708 KB |
Output isn't correct |
22 |
Incorrect |
5 ms |
6604 KB |
Output isn't correct |
23 |
Incorrect |
5 ms |
6732 KB |
Output isn't correct |
24 |
Incorrect |
5 ms |
6732 KB |
Output isn't correct |
25 |
Incorrect |
6 ms |
6780 KB |
Output isn't correct |
26 |
Incorrect |
6 ms |
6776 KB |
Output isn't correct |
27 |
Incorrect |
7 ms |
6800 KB |
Output isn't correct |
28 |
Incorrect |
6 ms |
6732 KB |
Output isn't correct |
29 |
Incorrect |
7 ms |
6808 KB |
Output isn't correct |
30 |
Incorrect |
7 ms |
6732 KB |
Output isn't correct |
31 |
Incorrect |
7 ms |
6732 KB |
Output isn't correct |
32 |
Incorrect |
7 ms |
6732 KB |
Output isn't correct |
33 |
Incorrect |
59 ms |
7832 KB |
Output isn't correct |
34 |
Incorrect |
42 ms |
7756 KB |
Output isn't correct |
35 |
Incorrect |
58 ms |
7756 KB |
Output isn't correct |
36 |
Incorrect |
50 ms |
8012 KB |
Output isn't correct |
37 |
Incorrect |
55 ms |
8024 KB |
Output isn't correct |
38 |
Incorrect |
78 ms |
8028 KB |
Output isn't correct |
39 |
Incorrect |
68 ms |
8184 KB |
Output isn't correct |
40 |
Incorrect |
64 ms |
8192 KB |
Output isn't correct |
41 |
Incorrect |
107 ms |
8140 KB |
Output isn't correct |
42 |
Incorrect |
83 ms |
8524 KB |
Output isn't correct |
43 |
Incorrect |
83 ms |
8544 KB |
Output isn't correct |
44 |
Incorrect |
119 ms |
8524 KB |
Output isn't correct |
45 |
Incorrect |
91 ms |
8780 KB |
Output isn't correct |
46 |
Incorrect |
134 ms |
8808 KB |
Output isn't correct |
47 |
Incorrect |
162 ms |
8812 KB |
Output isn't correct |
48 |
Incorrect |
114 ms |
9108 KB |
Output isn't correct |
49 |
Incorrect |
110 ms |
9036 KB |
Output isn't correct |
50 |
Incorrect |
173 ms |
9036 KB |
Output isn't correct |
51 |
Incorrect |
130 ms |
9164 KB |
Output isn't correct |
52 |
Incorrect |
133 ms |
9236 KB |
Output isn't correct |
53 |
Incorrect |
220 ms |
9308 KB |
Output isn't correct |
54 |
Incorrect |
149 ms |
9420 KB |
Output isn't correct |
55 |
Incorrect |
145 ms |
9424 KB |
Output isn't correct |
56 |
Incorrect |
295 ms |
9540 KB |
Output isn't correct |
57 |
Incorrect |
190 ms |
9624 KB |
Output isn't correct |
58 |
Incorrect |
186 ms |
9636 KB |
Output isn't correct |
59 |
Incorrect |
312 ms |
9640 KB |
Output isn't correct |
60 |
Incorrect |
207 ms |
9828 KB |
Output isn't correct |
61 |
Incorrect |
197 ms |
9784 KB |
Output isn't correct |
62 |
Incorrect |
313 ms |
9836 KB |
Output isn't correct |
63 |
Correct |
445 ms |
9844 KB |
Output is correct |
64 |
Incorrect |
436 ms |
9924 KB |
Output isn't correct |
65 |
Incorrect |
428 ms |
9796 KB |
Output isn't correct |
66 |
Incorrect |
501 ms |
9856 KB |
Output isn't correct |
67 |
Correct |
417 ms |
9796 KB |
Output is correct |
68 |
Correct |
519 ms |
9868 KB |
Output is correct |
69 |
Incorrect |
505 ms |
9868 KB |
Output isn't correct |
70 |
Incorrect |
480 ms |
9872 KB |
Output isn't correct |
71 |
Incorrect |
490 ms |
9872 KB |
Output isn't correct |
72 |
Incorrect |
490 ms |
9876 KB |
Output isn't correct |
73 |
Incorrect |
459 ms |
10144 KB |
Output isn't correct |
74 |
Incorrect |
464 ms |
10136 KB |
Output isn't correct |
75 |
Incorrect |
445 ms |
10140 KB |
Output isn't correct |
76 |
Incorrect |
479 ms |
10144 KB |
Output isn't correct |
77 |
Incorrect |
464 ms |
10140 KB |
Output isn't correct |
78 |
Correct |
455 ms |
10180 KB |
Output is correct |
79 |
Incorrect |
470 ms |
10104 KB |
Output isn't correct |
80 |
Incorrect |
474 ms |
10108 KB |
Output isn't correct |
81 |
Incorrect |
464 ms |
10100 KB |
Output isn't correct |
82 |
Incorrect |
476 ms |
10084 KB |
Output isn't correct |
83 |
Correct |
465 ms |
10064 KB |
Output is correct |
84 |
Incorrect |
476 ms |
10052 KB |
Output isn't correct |
85 |
Incorrect |
469 ms |
10180 KB |
Output isn't correct |
86 |
Incorrect |
463 ms |
10040 KB |
Output isn't correct |
87 |
Incorrect |
459 ms |
10064 KB |
Output isn't correct |
88 |
Correct |
489 ms |
10008 KB |
Output is correct |
89 |
Incorrect |
476 ms |
10044 KB |
Output isn't correct |
90 |
Incorrect |
463 ms |
10048 KB |
Output isn't correct |
91 |
Incorrect |
470 ms |
10004 KB |
Output isn't correct |
92 |
Incorrect |
468 ms |
9924 KB |
Output isn't correct |