#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, s;
int x3, x2, y3, y2;
vector<vector<char>> grid;
vector<vector<int>> tim; //saves time the bees get there
vector<vector<int>> dist; //saves latest time can leave current spot to get to house on time
vector<pair<int, int>> direc = {
{1, 0},
{-1, 0},
{0, 1},
{0, -1}
};
bool check(int i, int j) {
if (i < 0 || j < 0 || i >= n || j >= n) return false; //out of bounds
if (grid[i][j] == 'M' || grid[i][j] == 'G') return true; //good next field
return false;
}
void bfs1() {
queue<tuple<int, int, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 'H') q.push({0, i, j});
}
}
while (!q.empty()) {
auto [d, i, j] = q.front();
q.pop();
if (tim[i][j] <= d) continue;
tim[i][j] = d;
for (auto [x, y] : direc) {
if (check(i+x, j+y)) q.push({d+1, i+x, j+y});
}
}
}
void bfs2() {
priority_queue<tuple<int, int, int, int>> pq; //dist, steps, i, j
pq.push({1e9, s, x2, y2});
while (!pq.empty()) {
auto [d, step, i, j] = pq.top();
//~ cout << d << " " << i << " " << j << " " << step << endl;
pq.pop();
if (tim[i][j] <= d) {
d = tim[i][j]-1;
step = s;
}
if (step == 0) {
d--;
step = s;
}
if (dist[i][j] >= d) continue;
dist[i][j] = d;
for (auto [x, y] : direc) {
if (check(i+x, j+y)) pq.push({d, step-1, i+x, j+y});
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s;
grid = vector<vector<char>> (n, vector<char> (n));
tim = vector<vector<int>> (n, vector<int> (n, 1e9));
dist = vector<vector<int>> (n, vector<int> (n, -1));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'D') {
x2 = i; y2 = j;
}
if (grid[i][j] == 'M') {
x3 = i; y3 = j;
}
}
}
bfs1(); //calculates how long it takes for fields to be infected with bees
//now need to calc movement somehow
bfs2(); //seg fault occurs in here
cout << dist[x3][y3] << endl;
}
Compilation message
mecho.cpp: In function 'void bfs1()':
mecho.cpp:34:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | auto [d, i, j] = q.front();
| ^
mecho.cpp:38:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
38 | for (auto [x, y] : direc) {
| ^
mecho.cpp: In function 'void bfs2()':
mecho.cpp:48:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
48 | auto [d, step, i, j] = pq.top();
| ^
mecho.cpp:61:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
61 | for (auto [x, y] : direc) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
362 ms |
15148 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 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
456 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
456 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
5 ms |
2652 KB |
Output is correct |
34 |
Correct |
6 ms |
2648 KB |
Output is correct |
35 |
Correct |
33 ms |
2648 KB |
Output is correct |
36 |
Correct |
8 ms |
3160 KB |
Output is correct |
37 |
Correct |
7 ms |
3164 KB |
Output is correct |
38 |
Correct |
45 ms |
3436 KB |
Output is correct |
39 |
Correct |
8 ms |
3932 KB |
Output is correct |
40 |
Correct |
9 ms |
3932 KB |
Output is correct |
41 |
Correct |
56 ms |
4212 KB |
Output is correct |
42 |
Correct |
12 ms |
4696 KB |
Output is correct |
43 |
Correct |
12 ms |
4700 KB |
Output is correct |
44 |
Correct |
70 ms |
5040 KB |
Output is correct |
45 |
Correct |
12 ms |
5720 KB |
Output is correct |
46 |
Correct |
14 ms |
5724 KB |
Output is correct |
47 |
Correct |
86 ms |
5952 KB |
Output is correct |
48 |
Correct |
17 ms |
6748 KB |
Output is correct |
49 |
Correct |
16 ms |
6872 KB |
Output is correct |
50 |
Correct |
108 ms |
6996 KB |
Output is correct |
51 |
Correct |
19 ms |
7772 KB |
Output is correct |
52 |
Correct |
20 ms |
7772 KB |
Output is correct |
53 |
Correct |
122 ms |
8028 KB |
Output is correct |
54 |
Correct |
21 ms |
9052 KB |
Output is correct |
55 |
Correct |
23 ms |
9052 KB |
Output is correct |
56 |
Correct |
137 ms |
9296 KB |
Output is correct |
57 |
Correct |
23 ms |
10332 KB |
Output is correct |
58 |
Correct |
27 ms |
10332 KB |
Output is correct |
59 |
Correct |
169 ms |
10584 KB |
Output is correct |
60 |
Correct |
27 ms |
11612 KB |
Output is correct |
61 |
Correct |
28 ms |
11608 KB |
Output is correct |
62 |
Correct |
189 ms |
11864 KB |
Output is correct |
63 |
Correct |
109 ms |
11836 KB |
Output is correct |
64 |
Correct |
106 ms |
11860 KB |
Output is correct |
65 |
Correct |
117 ms |
11860 KB |
Output is correct |
66 |
Correct |
121 ms |
11860 KB |
Output is correct |
67 |
Correct |
111 ms |
11860 KB |
Output is correct |
68 |
Correct |
66 ms |
11864 KB |
Output is correct |
69 |
Correct |
90 ms |
11868 KB |
Output is correct |
70 |
Correct |
56 ms |
11868 KB |
Output is correct |
71 |
Correct |
62 ms |
11868 KB |
Output is correct |
72 |
Correct |
92 ms |
11868 KB |
Output is correct |
73 |
Correct |
213 ms |
14940 KB |
Output is correct |
74 |
Correct |
370 ms |
18856 KB |
Output is correct |
75 |
Correct |
222 ms |
14928 KB |
Output is correct |
76 |
Correct |
204 ms |
14940 KB |
Output is correct |
77 |
Correct |
255 ms |
14940 KB |
Output is correct |
78 |
Correct |
287 ms |
14684 KB |
Output is correct |
79 |
Correct |
449 ms |
18648 KB |
Output is correct |
80 |
Correct |
270 ms |
15152 KB |
Output is correct |
81 |
Correct |
234 ms |
14684 KB |
Output is correct |
82 |
Correct |
320 ms |
16464 KB |
Output is correct |
83 |
Correct |
244 ms |
14480 KB |
Output is correct |
84 |
Correct |
399 ms |
18064 KB |
Output is correct |
85 |
Correct |
317 ms |
15036 KB |
Output is correct |
86 |
Correct |
270 ms |
14916 KB |
Output is correct |
87 |
Correct |
411 ms |
16448 KB |
Output is correct |
88 |
Correct |
351 ms |
14616 KB |
Output is correct |
89 |
Correct |
385 ms |
17544 KB |
Output is correct |
90 |
Correct |
389 ms |
15640 KB |
Output is correct |
91 |
Correct |
379 ms |
15952 KB |
Output is correct |
92 |
Correct |
402 ms |
15696 KB |
Output is correct |