This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <array>
#include <climits>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int R, C, K, Sr, Sc, Gr, Gc;
cin >> R >> C >> K >> Sr >> Sc >> Gr >> Gc;
K = 1 - K; Sr--; Sc--; Gr--; Gc--;
vector<string> A(R);
for(string& a : A) {
cin >> a;
for(char& c : a) c = c == '#';
}
// {ã“ã“ã«è¾¿ã‚Šç€ãã¾ã§ã«å¿…è¦ãªå¡—ã‚Šã¤ã¶ã—回数, 最後ã®å¡—ã‚Šã¤ã¶ã—ã«ã‚ˆã£ã¦è¡Œã‘る縦ã®ç§»å‹•é‡, 最後ã®å¡—ã‚Šã¤ã¶ã—ã«ã‚ˆã£ã¦è¡Œã‘る横ã®ç§»å‹•é‡} ã‚’æŒã¤
// å„レイヤー 3 フェーズ㮠BFS
// 1. 4 æ–¹å‘㫠白ãªã‚‰ç§»å‹• / é»’ãªã‚‰ (K - 1, K - 1) ã§å¡—ã‚Šã¤ã¶ã—追åŠ
// 2. 横方å‘ã«ç§»å‹•
// 3. 縦方å‘ã«ç§»å‹•
vector cost(R, vector(C, array{INT_MAX, 0, 0}));
vector<pair<int, int>> q1, q2;
auto push = [&](vector<pair<int, int>>& q, int r, int c, int x, int y, int z) -> void {
if(r < 0 || r >= R || c < 0 || c >= C) return;
if(cost[r][c][0] <= x) return;
cost[r][c] = {x, y, z};
q.emplace_back(r, c);
};
auto push2 = [&](int r, int c, int x) -> void {
if(r < 0 || r >= R || c < 0 || c >= C) return;
if(A[r][c]) push(q2, r, c, x + 1, K, K);
else push(q1, r, c, x, 0, 0);
};
push(q1, Sr, Sc, 0, 0, 0);
while(q1.size()) {
// 横方å‘ã«ç§»å‹•
for(int i = 0; i < q1.size(); i++) {
const auto [r, c] = q1[i];
const auto [x, y, z] = cost[r][c];
if(z == 0) continue;
push(q1, r, c - 1, x, y, z + 1);
push(q1, r, c + 1, x, y, z + 1);
}
// 縦方å‘ã«ç§»å‹•
for(int i = 0; i < q1.size(); i++) {
const auto [r, c] = q1[i];
const auto [x, y, z] = cost[r][c];
if(y == 0) continue;
push(q1, r - 1, c, x, y + 1, z);
push(q1, r + 1, c, x, y + 1, z);
}
// BFS
for(int i = 0; i < q1.size(); i++) {
const auto [r, c] = q1[i];
const auto [x, y, z] = cost[r][c];
push2(r - 1, c, x);
push2(r + 1, c, x);
push2(r, c - 1, x);
push2(r, c + 1, x);
}
swap(q1, q2);
q2.clear();
}
cout << cost[Gr][Gc][0] << endl;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:43:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i = 0; i < q1.size(); i++) {
| ~~^~~~~~~~~~~
Main.cpp:51:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i = 0; i < q1.size(); i++) {
| ~~^~~~~~~~~~~
Main.cpp:59:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i = 0; i < q1.size(); i++) {
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |