#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){
if (x >= 0 && x < n && y >= 0 && y < n && field[x][y] != 'T' && field[x][y] != 'H')
return true;
return false;
}
void update_field(queue <pair <int, int>> &q, int max_steps, bool (&visited)[MAX_N][MAX_N], bool (&visited2)[MAX_N][MAX_N], int (&dis)[MAX_N][MAX_N], bool is_mecho){
while (!q.empty() && dis[q.front().first][q.front().second] != -1){
int x = q.front().first, y = q.front().second;
q.pop();
if (is_mecho && visited[x][y] && visited2[x][y])
continue;
for (int i = 0; i < 4; i++){
if (valid_sq(x + dx[i], y + dy[i]) && !visited[x+dx[i]][y+dy[i]] && !visited2[x+dx[i]][y+dy[i]]){
q.push({x+dx[i], y+dy[i]});
visited[x+dx[i]][y+dy[i]] = true;
if (dis[x][y] + 1 < max_steps)
dis[x+dx[i]][y+dy[i]] = dis[x][y] + 1;
else
dis[x+dx[i]][y+dy[i]] = -1;
}
}
}
}
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));
//simulate
for (auto i : hives){
bees_q.push({i.first, i.second});
bees_visited[i.first][i.second] = true;
}
if (eating_time > 0)
update_field(bees_q, eating_time, bees_visited, bees_visited, bees_dis, false);
mecho_q.push({startx, starty});
for (int i = eating_time; i <= n*2-2 && !bees_q.empty() && !mecho_q.empty(); i++){
while (!bees_q.empty() && bees_dis[bees_q.front().first][bees_q.front().second] == -1){
bees_dis[bees_q.front().first][bees_q.front().second] = 0;
bees_q.push({bees_q.front().first, bees_q.front().second});
bees_q.pop();
}while (!mecho_q.empty() && mecho_dis[mecho_q.front().first][mecho_q.front().second] == -1){
mecho_dis[mecho_q.front().first][mecho_q.front().second] = 0;
mecho_q.push({mecho_q.front().first, mecho_q.front().second});
mecho_q.pop();
}
update_field(mecho_q, s, mecho_visited, bees_visited, mecho_dis, true);
if (mecho_visited[home_x][home_y])
break;
update_field(bees_q, 1, bees_visited, bees_visited, bees_dis, false);
}
if (mecho_visited[home_x][home_y])
l = eating_time + 1;
else
r = eating_time - 1;
//empty_queues
while (!bees_q.empty())
bees_q.pop();
while (!mecho_q.empty())
mecho_q.pop();
}
cout << r << '\n';
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:87:41: warning: 'home_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
87 | if (mecho_visited[home_x][home_y])
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
mecho.cpp:87: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 |
4 ms |
6476 KB |
Output isn't correct |
3 |
Correct |
4 ms |
6476 KB |
Output is correct |
4 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
5 |
Correct |
4 ms |
6476 KB |
Output is correct |
6 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
7 |
Incorrect |
312 ms |
7556 KB |
Output isn't correct |
8 |
Correct |
4 ms |
6476 KB |
Output is correct |
9 |
Correct |
5 ms |
6476 KB |
Output is correct |
10 |
Correct |
4 ms |
6476 KB |
Output is correct |
11 |
Correct |
4 ms |
6476 KB |
Output is correct |
12 |
Incorrect |
5 ms |
6476 KB |
Output isn't correct |
13 |
Incorrect |
6 ms |
6476 KB |
Output isn't correct |
14 |
Correct |
6 ms |
6476 KB |
Output is correct |
15 |
Incorrect |
5 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 |
5 ms |
6476 KB |
Output isn't correct |
19 |
Incorrect |
7 ms |
6576 KB |
Output isn't correct |
20 |
Incorrect |
5 ms |
6476 KB |
Output isn't correct |
21 |
Incorrect |
5 ms |
6476 KB |
Output isn't correct |
22 |
Incorrect |
5 ms |
6476 KB |
Output isn't correct |
23 |
Incorrect |
5 ms |
6524 KB |
Output isn't correct |
24 |
Incorrect |
5 ms |
6476 KB |
Output isn't correct |
25 |
Incorrect |
5 ms |
6580 KB |
Output isn't correct |
26 |
Incorrect |
5 ms |
6476 KB |
Output isn't correct |
27 |
Incorrect |
6 ms |
6476 KB |
Output isn't correct |
28 |
Incorrect |
6 ms |
6476 KB |
Output isn't correct |
29 |
Incorrect |
5 ms |
6476 KB |
Output isn't correct |
30 |
Incorrect |
6 ms |
6476 KB |
Output isn't correct |
31 |
Incorrect |
6 ms |
6476 KB |
Output isn't correct |
32 |
Incorrect |
5 ms |
6476 KB |
Output isn't correct |
33 |
Incorrect |
12 ms |
6732 KB |
Output isn't correct |
34 |
Incorrect |
12 ms |
6732 KB |
Output isn't correct |
35 |
Correct |
41 ms |
6732 KB |
Output is correct |
36 |
Incorrect |
13 ms |
6776 KB |
Output isn't correct |
37 |
Incorrect |
14 ms |
6772 KB |
Output isn't correct |
38 |
Correct |
55 ms |
6744 KB |
Output is correct |
39 |
Incorrect |
21 ms |
6732 KB |
Output isn't correct |
40 |
Incorrect |
21 ms |
6732 KB |
Output isn't correct |
41 |
Correct |
70 ms |
6732 KB |
Output is correct |
42 |
Incorrect |
17 ms |
6988 KB |
Output isn't correct |
43 |
Incorrect |
19 ms |
6988 KB |
Output isn't correct |
44 |
Correct |
83 ms |
7016 KB |
Output is correct |
45 |
Incorrect |
19 ms |
6988 KB |
Output isn't correct |
46 |
Incorrect |
22 ms |
6988 KB |
Output isn't correct |
47 |
Correct |
98 ms |
6988 KB |
Output is correct |
48 |
Incorrect |
22 ms |
7052 KB |
Output isn't correct |
49 |
Incorrect |
28 ms |
7148 KB |
Output isn't correct |
50 |
Correct |
116 ms |
7236 KB |
Output is correct |
51 |
Incorrect |
24 ms |
7212 KB |
Output isn't correct |
52 |
Incorrect |
28 ms |
7116 KB |
Output isn't correct |
53 |
Correct |
141 ms |
7192 KB |
Output is correct |
54 |
Incorrect |
28 ms |
7252 KB |
Output isn't correct |
55 |
Incorrect |
31 ms |
7244 KB |
Output isn't correct |
56 |
Correct |
159 ms |
7236 KB |
Output is correct |
57 |
Incorrect |
34 ms |
7332 KB |
Output isn't correct |
58 |
Incorrect |
34 ms |
7244 KB |
Output isn't correct |
59 |
Correct |
183 ms |
7300 KB |
Output is correct |
60 |
Incorrect |
34 ms |
7244 KB |
Output isn't correct |
61 |
Incorrect |
39 ms |
7244 KB |
Output isn't correct |
62 |
Correct |
213 ms |
7344 KB |
Output is correct |
63 |
Incorrect |
331 ms |
7344 KB |
Output isn't correct |
64 |
Correct |
335 ms |
7244 KB |
Output is correct |
65 |
Correct |
315 ms |
7364 KB |
Output is correct |
66 |
Correct |
350 ms |
7244 KB |
Output is correct |
67 |
Correct |
334 ms |
7244 KB |
Output is correct |
68 |
Correct |
339 ms |
7372 KB |
Output is correct |
69 |
Correct |
295 ms |
7360 KB |
Output is correct |
70 |
Correct |
322 ms |
7372 KB |
Output is correct |
71 |
Correct |
362 ms |
7360 KB |
Output is correct |
72 |
Incorrect |
380 ms |
7372 KB |
Output isn't correct |
73 |
Incorrect |
272 ms |
7632 KB |
Output isn't correct |
74 |
Correct |
329 ms |
7748 KB |
Output is correct |
75 |
Correct |
357 ms |
7556 KB |
Output is correct |
76 |
Correct |
353 ms |
7628 KB |
Output is correct |
77 |
Correct |
317 ms |
7732 KB |
Output is correct |
78 |
Correct |
334 ms |
7484 KB |
Output is correct |
79 |
Correct |
337 ms |
7588 KB |
Output is correct |
80 |
Correct |
332 ms |
7600 KB |
Output is correct |
81 |
Correct |
332 ms |
7620 KB |
Output is correct |
82 |
Correct |
319 ms |
7500 KB |
Output is correct |
83 |
Correct |
348 ms |
7540 KB |
Output is correct |
84 |
Correct |
330 ms |
7564 KB |
Output is correct |
85 |
Correct |
334 ms |
7572 KB |
Output is correct |
86 |
Correct |
339 ms |
7552 KB |
Output is correct |
87 |
Correct |
319 ms |
7532 KB |
Output is correct |
88 |
Correct |
345 ms |
7504 KB |
Output is correct |
89 |
Correct |
322 ms |
7520 KB |
Output is correct |
90 |
Correct |
318 ms |
7500 KB |
Output is correct |
91 |
Correct |
308 ms |
7472 KB |
Output is correct |
92 |
Correct |
346 ms |
7500 KB |
Output is correct |