#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){
if (x >= 0 && x < n && y >= 0 && y < n && field[x][y] != 'T' && field[x][y] != 'H')
return true;
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 = 2*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});
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) && !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});
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) && !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;
}
}
}
if (mecho_visited[home_x][home_y])
l = eating_time + 1;
else
r = eating_time - 1;
//empty both queues
while (!bees_q.empty())
bees_q.pop();
while (!mecho_q.empty())
mecho_q.pop();
}
cout << l - 1 << '\n';
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:79:41: warning: 'home_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
79 | if (mecho_visited[home_x][home_y])
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
mecho.cpp:79:41: warning: 'home_x' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6476 KB |
Output is correct |
2 |
Incorrect |
3 ms |
6604 KB |
Output isn't correct |
3 |
Correct |
3 ms |
6604 KB |
Output is correct |
4 |
Incorrect |
4 ms |
6604 KB |
Output isn't correct |
5 |
Correct |
3 ms |
6604 KB |
Output is correct |
6 |
Incorrect |
4 ms |
6604 KB |
Output isn't correct |
7 |
Incorrect |
346 ms |
9944 KB |
Output isn't correct |
8 |
Correct |
4 ms |
6604 KB |
Output is correct |
9 |
Correct |
4 ms |
6604 KB |
Output is correct |
10 |
Correct |
4 ms |
6604 KB |
Output is correct |
11 |
Correct |
4 ms |
6632 KB |
Output is correct |
12 |
Incorrect |
7 ms |
6732 KB |
Output isn't correct |
13 |
Incorrect |
5 ms |
6740 KB |
Output isn't correct |
14 |
Correct |
5 ms |
6732 KB |
Output is correct |
15 |
Incorrect |
4 ms |
6604 KB |
Output isn't correct |
16 |
Incorrect |
4 ms |
6652 KB |
Output isn't correct |
17 |
Incorrect |
5 ms |
6604 KB |
Output isn't correct |
18 |
Incorrect |
4 ms |
6604 KB |
Output isn't correct |
19 |
Incorrect |
5 ms |
6604 KB |
Output isn't correct |
20 |
Incorrect |
4 ms |
6604 KB |
Output isn't correct |
21 |
Incorrect |
5 ms |
6604 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 |
8 ms |
6708 KB |
Output isn't correct |
26 |
Incorrect |
5 ms |
6732 KB |
Output isn't correct |
27 |
Incorrect |
6 ms |
6732 KB |
Output isn't correct |
28 |
Incorrect |
5 ms |
6732 KB |
Output isn't correct |
29 |
Incorrect |
5 ms |
6812 KB |
Output isn't correct |
30 |
Incorrect |
5 ms |
6732 KB |
Output isn't correct |
31 |
Incorrect |
6 ms |
6732 KB |
Output isn't correct |
32 |
Incorrect |
6 ms |
6732 KB |
Output isn't correct |
33 |
Incorrect |
35 ms |
7756 KB |
Output isn't correct |
34 |
Incorrect |
51 ms |
7756 KB |
Output isn't correct |
35 |
Correct |
71 ms |
7756 KB |
Output is correct |
36 |
Incorrect |
48 ms |
8132 KB |
Output isn't correct |
37 |
Incorrect |
52 ms |
8020 KB |
Output isn't correct |
38 |
Correct |
74 ms |
8008 KB |
Output is correct |
39 |
Incorrect |
60 ms |
8196 KB |
Output isn't correct |
40 |
Incorrect |
63 ms |
8140 KB |
Output isn't correct |
41 |
Correct |
96 ms |
8140 KB |
Output is correct |
42 |
Incorrect |
72 ms |
8608 KB |
Output isn't correct |
43 |
Incorrect |
96 ms |
8524 KB |
Output isn't correct |
44 |
Correct |
110 ms |
8608 KB |
Output is correct |
45 |
Incorrect |
82 ms |
8724 KB |
Output isn't correct |
46 |
Incorrect |
100 ms |
8824 KB |
Output isn't correct |
47 |
Correct |
192 ms |
8780 KB |
Output is correct |
48 |
Incorrect |
102 ms |
9024 KB |
Output isn't correct |
49 |
Incorrect |
117 ms |
9008 KB |
Output isn't correct |
50 |
Correct |
162 ms |
9036 KB |
Output is correct |
51 |
Incorrect |
134 ms |
9220 KB |
Output isn't correct |
52 |
Incorrect |
141 ms |
9348 KB |
Output isn't correct |
53 |
Correct |
195 ms |
9232 KB |
Output is correct |
54 |
Incorrect |
131 ms |
9420 KB |
Output isn't correct |
55 |
Incorrect |
161 ms |
9540 KB |
Output isn't correct |
56 |
Correct |
216 ms |
9444 KB |
Output is correct |
57 |
Incorrect |
159 ms |
9628 KB |
Output isn't correct |
58 |
Incorrect |
181 ms |
9580 KB |
Output isn't correct |
59 |
Correct |
293 ms |
9644 KB |
Output is correct |
60 |
Incorrect |
170 ms |
9832 KB |
Output isn't correct |
61 |
Incorrect |
206 ms |
9804 KB |
Output isn't correct |
62 |
Correct |
338 ms |
9924 KB |
Output is correct |
63 |
Correct |
358 ms |
9840 KB |
Output is correct |
64 |
Correct |
417 ms |
9896 KB |
Output is correct |
65 |
Correct |
482 ms |
9836 KB |
Output is correct |
66 |
Correct |
356 ms |
9836 KB |
Output is correct |
67 |
Correct |
380 ms |
9844 KB |
Output is correct |
68 |
Correct |
317 ms |
9868 KB |
Output is correct |
69 |
Correct |
309 ms |
9868 KB |
Output is correct |
70 |
Correct |
288 ms |
9884 KB |
Output is correct |
71 |
Correct |
319 ms |
9868 KB |
Output is correct |
72 |
Incorrect |
268 ms |
9872 KB |
Output isn't correct |
73 |
Incorrect |
268 ms |
10132 KB |
Output isn't correct |
74 |
Correct |
316 ms |
10136 KB |
Output is correct |
75 |
Correct |
286 ms |
10132 KB |
Output is correct |
76 |
Correct |
329 ms |
10224 KB |
Output is correct |
77 |
Correct |
288 ms |
10136 KB |
Output is correct |
78 |
Correct |
309 ms |
10212 KB |
Output is correct |
79 |
Correct |
296 ms |
10204 KB |
Output is correct |
80 |
Correct |
343 ms |
10104 KB |
Output is correct |
81 |
Correct |
327 ms |
10100 KB |
Output is correct |
82 |
Correct |
304 ms |
10100 KB |
Output is correct |
83 |
Correct |
356 ms |
10112 KB |
Output is correct |
84 |
Correct |
314 ms |
10060 KB |
Output is correct |
85 |
Correct |
367 ms |
10064 KB |
Output is correct |
86 |
Correct |
337 ms |
10060 KB |
Output is correct |
87 |
Correct |
367 ms |
10100 KB |
Output is correct |
88 |
Correct |
340 ms |
10052 KB |
Output is correct |
89 |
Correct |
373 ms |
9920 KB |
Output is correct |
90 |
Correct |
361 ms |
10004 KB |
Output is correct |
91 |
Correct |
331 ms |
10004 KB |
Output is correct |
92 |
Correct |
323 ms |
10012 KB |
Output is correct |