#include <bits/stdc++.h>
#define se second
#define fi first
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, S; cin >> N >> S;
vector<string> grid(N);
for (int i = 0; i < N; i++) {
cin >> grid[i];
}
int start, end;
vector<int> dist(N * N, INT_MAX);
vector<vector<int>> adj(N * N);
queue<pair<int,int>> hives;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (grid[i][j] == 'H') {
hives.push({i * N + j, 1});
dist[i * N + j] = 0;
}
else if (grid[i][j] == 'M') start = i * N + j;
else if (grid[i][j] == 'D') end = i * N + j;
if (grid[i][j] == 'H' || grid[i][j] == 'T') continue;
if (i && (grid[i - 1][j] == 'D' || grid[i - 1][j] == 'H' || grid[i - 1][j] == 'M' || grid[i - 1][j] == 'G')) adj[i * N - N + j].push_back(i * N + j);
if (j && (grid[i][j-1] == 'D' || grid[i][j-1] == 'H' || grid[i][j-1] == 'M' || grid[i][j-1] == 'G')) adj[i * N + j-1].push_back(i * N + j);
if (i != N - 1 && (grid[i + 1][j] == 'D' || grid[i + 1][j] == 'H' || grid[i + 1][j] == 'M' || grid[i + 1][j] == 'G')) adj[i * N + N + j].push_back(i * N + j);
if (j != N - 1 && (grid[i][j+1] == 'D' || grid[i][j+1] == 'H' || grid[i][j+1] == 'M' || grid[i][j+1] == 'G')) adj[i * N + j+1].push_back(i * N + j);
}
}
while (!hives.empty()) {
auto x = hives.front(); hives.pop();
for (int y : adj[x.fi]) {
if (dist[y] == INT_MAX && grid[y / N][y % N] != 'D'){
hives.push({y, x.se + 1});
dist[y] = x.se;
}
}
}
int maxi = 0;
for (int x : adj[end]) maxi = max(maxi, dist[x] != INT_MAX ? dist[x] : 0);
vector<bool> pro;
int l = 0, r = N * N / 2 + N;
while (l <= r) {
int mid = l + (r - l) / 2;
pro.clear(); pro.resize(N * N, false);
queue<pair<int,int>> q;
q.push({mid * S, start});
pro[start] = true;
bool won = false;
while (!q.empty() && q.front().fi / S < maxi) {
auto u = q.front(); q.pop();
if (u.se == end) {
won = true;
break;
}
if (u.fi / S >= dist[u.se]) continue;
for (int x : adj[u.se]) if (!pro[x]){ pro[x] = true; q.push({u.fi + 1, x});}
}
if (won) l = mid + 1;
else r = mid - 1;
}
cout << l - 1 << '\n';
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:48:22: warning: 'end' may be used uninitialized in this function [-Wmaybe-uninitialized]
48 | for (int x : adj[end]) maxi = max(maxi, dist[x] != INT_MAX ? dist[x] : 0);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Correct |
143 ms |
38784 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
408 KB |
Output isn't correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Incorrect |
1 ms |
356 KB |
Output isn't correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
32 |
Correct |
0 ms |
604 KB |
Output is correct |
33 |
Incorrect |
6 ms |
5724 KB |
Output isn't correct |
34 |
Correct |
6 ms |
5896 KB |
Output is correct |
35 |
Correct |
19 ms |
6760 KB |
Output is correct |
36 |
Incorrect |
11 ms |
7516 KB |
Output isn't correct |
37 |
Correct |
10 ms |
7520 KB |
Output is correct |
38 |
Correct |
24 ms |
8800 KB |
Output is correct |
39 |
Incorrect |
12 ms |
9440 KB |
Output isn't correct |
40 |
Correct |
12 ms |
9312 KB |
Output is correct |
41 |
Correct |
41 ms |
10976 KB |
Output is correct |
42 |
Incorrect |
14 ms |
11360 KB |
Output isn't correct |
43 |
Correct |
12 ms |
11360 KB |
Output is correct |
44 |
Correct |
29 ms |
13404 KB |
Output is correct |
45 |
Incorrect |
15 ms |
13656 KB |
Output isn't correct |
46 |
Correct |
15 ms |
13660 KB |
Output is correct |
47 |
Correct |
37 ms |
15964 KB |
Output is correct |
48 |
Incorrect |
20 ms |
16220 KB |
Output isn't correct |
49 |
Correct |
19 ms |
16220 KB |
Output is correct |
50 |
Correct |
37 ms |
19176 KB |
Output is correct |
51 |
Incorrect |
23 ms |
19132 KB |
Output isn't correct |
52 |
Correct |
23 ms |
19036 KB |
Output is correct |
53 |
Correct |
65 ms |
22324 KB |
Output is correct |
54 |
Incorrect |
27 ms |
22108 KB |
Output isn't correct |
55 |
Correct |
25 ms |
22164 KB |
Output is correct |
56 |
Correct |
65 ms |
25720 KB |
Output is correct |
57 |
Incorrect |
31 ms |
25248 KB |
Output isn't correct |
58 |
Correct |
30 ms |
25180 KB |
Output is correct |
59 |
Correct |
76 ms |
29732 KB |
Output is correct |
60 |
Incorrect |
32 ms |
28680 KB |
Output isn't correct |
61 |
Correct |
30 ms |
28760 KB |
Output is correct |
62 |
Correct |
127 ms |
33744 KB |
Output is correct |
63 |
Correct |
113 ms |
30532 KB |
Output is correct |
64 |
Correct |
253 ms |
30552 KB |
Output is correct |
65 |
Correct |
153 ms |
30716 KB |
Output is correct |
66 |
Correct |
191 ms |
30644 KB |
Output is correct |
67 |
Correct |
159 ms |
30668 KB |
Output is correct |
68 |
Correct |
82 ms |
30584 KB |
Output is correct |
69 |
Correct |
72 ms |
30552 KB |
Output is correct |
70 |
Correct |
88 ms |
30544 KB |
Output is correct |
71 |
Correct |
74 ms |
30748 KB |
Output is correct |
72 |
Correct |
66 ms |
30532 KB |
Output is correct |
73 |
Correct |
98 ms |
38696 KB |
Output is correct |
74 |
Incorrect |
116 ms |
38740 KB |
Output isn't correct |
75 |
Correct |
140 ms |
38740 KB |
Output is correct |
76 |
Incorrect |
123 ms |
38696 KB |
Output isn't correct |
77 |
Correct |
138 ms |
38732 KB |
Output is correct |
78 |
Correct |
115 ms |
38744 KB |
Output is correct |
79 |
Correct |
90 ms |
38740 KB |
Output is correct |
80 |
Correct |
109 ms |
38736 KB |
Output is correct |
81 |
Correct |
114 ms |
38656 KB |
Output is correct |
82 |
Correct |
117 ms |
38788 KB |
Output is correct |
83 |
Correct |
116 ms |
38736 KB |
Output is correct |
84 |
Incorrect |
124 ms |
38456 KB |
Output isn't correct |
85 |
Correct |
124 ms |
38948 KB |
Output is correct |
86 |
Correct |
125 ms |
38692 KB |
Output is correct |
87 |
Correct |
121 ms |
38636 KB |
Output is correct |
88 |
Correct |
121 ms |
38736 KB |
Output is correct |
89 |
Incorrect |
142 ms |
38656 KB |
Output isn't correct |
90 |
Correct |
126 ms |
38768 KB |
Output is correct |
91 |
Correct |
120 ms |
38700 KB |
Output is correct |
92 |
Correct |
117 ms |
38736 KB |
Output is correct |