This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//no template practicing
#include<bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 0, -1, +1};
const int dy[4] = {-1, 1, 0, 0};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int R, C, N, rs, cs, rt, ct;
cin >> R >> C >> N >> rs >> cs >> rt >> ct;
--rs, --cs, --rt, --ct;
vector<vector<char>> a(R, vector<char>(C));
vector<vector<int>> d(R, vector<int>(C, -1));
for(int i = 0; i < R; ++i){
for(int j = 0; j < C; ++j){
cin >> a[i][j];
}
}
queue<pair<int, int>> white_queue;
queue<tuple<int, int, int, int>> black_queue;
white_queue.push({rs, cs});
d[rs][cs] = 0;
auto inside = [&](int r, int c){
return -1 < r && r < R && -1 < c && c < C;
};
while(!white_queue.empty() || !black_queue.empty()){
while(!white_queue.empty()){
int r, c;
tie(r, c) = white_queue.front(); white_queue.pop();
for(int i = 0; i < 4; ++i){
int nr = r + dx[i], nc = c + dy[i];
if(inside(nr, nc) && d[nr][nc] == -1){
if(a[nr][nc] == '.'){
d[nr][nc] = d[r][c];
white_queue.push({nr, nc});
}
else{
d[nr][nc] = d[r][c] + 1;
black_queue.push({nr, nc, N - 1, N - 1});
}
}
}
}
while(!black_queue.empty()){
int r, c, dist_r, dist_c;
tie(r, c, dist_r, dist_c) = black_queue.front(); black_queue.pop();
if(dist_r == 0 || dist_c == 0){
white_queue.push({r, c});
}
for(int i = 0; i < 4; ++i){
int nr = r + dx[i], nc = c + dy[i];
int n_dist_r = dist_r - abs(dx[i]), n_dist_c = dist_c - abs(dy[i]);
if(inside(nr, nc) && d[nr][nc] == -1 && n_dist_r >= 0 && n_dist_c >= 0){
d[nr][nc] = d[r][c];
black_queue.push({nr, nc, n_dist_r, n_dist_c});
}
}
}
if(d[rt][ct] != -1){
cout << d[rt][ct] << '\n';
return 0;
}
}
assert(false);
return 0;
}
# | 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... |