// If this is correct, God forgive my soul for trying to code the legit solution.
#include <bits/stdc++.h>
int dl[] = {1, 0, -1, 0};
int dc[] = {0, 1, 0,-1};
struct Matrix {
int R, C, N;
std::vector<std::string> matr;
std::vector<int> dist;
explicit Matrix(int _R, int _C, int _N): R(_R), C(_C), N(_N) {
matr.resize(_R);
dist.resize(R * C, R * C + 1);
}
bool in_matrix(int l, int c) {
return 0 <= l && l < R && 0 <= c && c < C;
}
int coord_to_index(int l, int c) {
return l * C + c;
}
std::vector<std::tuple<int, int, int>> create_adjacency_list(int l, int c) {
std::vector<std::tuple<int, int, int>> adj;
for (int i = 0; i < 4; i++) {
int ln = l + dl[i];
int cn = c + dc[i];
if (in_matrix(ln, cn) && matr[ln][cn] == '.')
adj.push_back({ln, cn, 0});
}
for (int ln = l - N; ln <= l + N; ln++)
for (int cn = c - N; cn <= c + N; cn++) {
if ((std::abs(l - ln) != N || std::abs(c - cn) != N) && in_matrix(ln, cn) &&
(ln != l || cn != c)) {
adj.push_back({ln, cn, 1});
}
}
return adj;
}
int run_bfs(int Sl, int Sc, int Dl, int Dc) {
std::deque<std::pair<int, int>> q;
dist[coord_to_index(Sl, Sc)] = 0;
q.push_back({Sl, Sc});
while (!q.empty()) {
int l = q.front().first;
int c = q.front().second;
int node = coord_to_index(l, c);
if (l == Dl && c == Dc)
return dist[node];
q.pop_front();
auto adj = create_adjacency_list(l, c);
for (auto it: adj) {
auto [ln, cn, cost] = it;
int node_to = coord_to_index(ln, cn);
std::cerr << l << " " << c << " -> " << ln << " " << cn << ": " << cost << "\n";
if (dist[node] + cost < dist[node_to]) {
dist[node_to] = dist[node] + cost;
if (cost == 0)
q.push_front({ln, cn});
else
q.push_back({ln, cn});
}
}
}
return dist[coord_to_index(Dl, Dc)];
}
};
int main() {
std::cin.tie(NULL);
std::iostream::sync_with_stdio(false);
int R, C, N;
std::cin >> R >> C >> N;
int Sl, Sc, Dl, Dc;
std::cin >> Sl >> Sc >> Dl >> Dc;
Sl--; Sc--; Dl--; Dc--;
Matrix M(R, C, N);
for (int i = 0; i < R; i++)
std::cin >> M.matr[i];
std::cout << M.run_bfs(Sl, Sc, Dl, Dc);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
36 ms |
348 KB |
Output is correct |
4 |
Correct |
52 ms |
500 KB |
Output is correct |
5 |
Correct |
25 ms |
468 KB |
Output is correct |
6 |
Correct |
19 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
472 KB |
Output is correct |
8 |
Correct |
17 ms |
496 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
6 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
448 KB |
Output is correct |
16 |
Correct |
10 ms |
348 KB |
Output is correct |
17 |
Correct |
25 ms |
348 KB |
Output is correct |
18 |
Correct |
33 ms |
348 KB |
Output is correct |
19 |
Correct |
1848 ms |
4684 KB |
Output is correct |
20 |
Correct |
743 ms |
2184 KB |
Output is correct |
21 |
Correct |
856 ms |
2640 KB |
Output is correct |
22 |
Execution timed out |
2067 ms |
5460 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
472 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
36 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
17 ms |
484 KB |
Output is correct |
9 |
Correct |
51 ms |
484 KB |
Output is correct |
10 |
Correct |
25 ms |
480 KB |
Output is correct |
11 |
Correct |
19 ms |
348 KB |
Output is correct |
12 |
Execution timed out |
2033 ms |
3924 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
7 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
51 ms |
492 KB |
Output is correct |
8 |
Correct |
25 ms |
348 KB |
Output is correct |
9 |
Execution timed out |
2062 ms |
3924 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
472 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
36 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
17 ms |
484 KB |
Output is correct |
9 |
Correct |
51 ms |
484 KB |
Output is correct |
10 |
Correct |
25 ms |
480 KB |
Output is correct |
11 |
Correct |
19 ms |
348 KB |
Output is correct |
12 |
Execution timed out |
2033 ms |
3924 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
6 ms |
472 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
36 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
17 ms |
484 KB |
Output is correct |
9 |
Correct |
51 ms |
484 KB |
Output is correct |
10 |
Correct |
25 ms |
480 KB |
Output is correct |
11 |
Correct |
19 ms |
348 KB |
Output is correct |
12 |
Execution timed out |
2033 ms |
3924 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
36 ms |
348 KB |
Output is correct |
4 |
Correct |
52 ms |
500 KB |
Output is correct |
5 |
Correct |
25 ms |
468 KB |
Output is correct |
6 |
Correct |
19 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
472 KB |
Output is correct |
8 |
Correct |
17 ms |
496 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
6 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
448 KB |
Output is correct |
16 |
Correct |
10 ms |
348 KB |
Output is correct |
17 |
Correct |
25 ms |
348 KB |
Output is correct |
18 |
Correct |
33 ms |
348 KB |
Output is correct |
19 |
Correct |
1848 ms |
4684 KB |
Output is correct |
20 |
Correct |
743 ms |
2184 KB |
Output is correct |
21 |
Correct |
856 ms |
2640 KB |
Output is correct |
22 |
Execution timed out |
2067 ms |
5460 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
36 ms |
348 KB |
Output is correct |
4 |
Correct |
52 ms |
500 KB |
Output is correct |
5 |
Correct |
25 ms |
468 KB |
Output is correct |
6 |
Correct |
19 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
472 KB |
Output is correct |
8 |
Correct |
17 ms |
496 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
6 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
448 KB |
Output is correct |
16 |
Correct |
10 ms |
348 KB |
Output is correct |
17 |
Correct |
25 ms |
348 KB |
Output is correct |
18 |
Correct |
33 ms |
348 KB |
Output is correct |
19 |
Correct |
1848 ms |
4684 KB |
Output is correct |
20 |
Correct |
743 ms |
2184 KB |
Output is correct |
21 |
Correct |
856 ms |
2640 KB |
Output is correct |
22 |
Execution timed out |
2067 ms |
5460 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
36 ms |
348 KB |
Output is correct |
4 |
Correct |
52 ms |
500 KB |
Output is correct |
5 |
Correct |
25 ms |
468 KB |
Output is correct |
6 |
Correct |
19 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
472 KB |
Output is correct |
8 |
Correct |
17 ms |
496 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
6 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
448 KB |
Output is correct |
16 |
Correct |
10 ms |
348 KB |
Output is correct |
17 |
Correct |
25 ms |
348 KB |
Output is correct |
18 |
Correct |
33 ms |
348 KB |
Output is correct |
19 |
Correct |
1848 ms |
4684 KB |
Output is correct |
20 |
Correct |
743 ms |
2184 KB |
Output is correct |
21 |
Correct |
856 ms |
2640 KB |
Output is correct |
22 |
Execution timed out |
2067 ms |
5460 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |