이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//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... |