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 <bits/stdc++.h>
using namespace std;
int r, c, dp[1010][1010], stx, sty, enx, eny, cx, cy, cdi;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
char arr[1010][1010];
pair<int, int> dir[4][1010][1010], cur;
queue<tuple<int, int, int>> q;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> r >> c;
for(int i=0; i<1010; i++) for(int j=0; j<1010; j++) arr[i][j] = '#';
for(int i=1; i<=r; i++) {
for(int j=1; j<=c; j++) {
cin >> arr[i][j];
if(arr[i][j] == 'S') {
stx = i;
sty = j;
}
if(arr[i][j] == 'C') {
enx = i;
eny = j;
}
dp[i][j] = 1e9;
}
}
for(int j=0; j<=c+1; j++) {
for(int i=0; i<=r+1; i++) {
if(arr[i][j] == '#') cur = {i+1, j};
if(arr[i+1][j] == '#') dir[0][i][j] = cur;
}
}
for(int i=0; i<=r+1; i++) {
for(int j=0; j<=c+1; j++) {
if(arr[i][j] == '#') cur = {i, j+1};
if(arr[i][j+1] == '#') dir[1][i][j] = cur;
}
}
for(int j=0; j<=c+1; j++) {
for(int i=r+1; i>=0; i--) {
if(arr[i][j] == '#') cur = {i-1, j};
if(arr[i-1][j] == '#') dir[2][i][j] = cur;
}
}
for(int i=0; i<=r+1; i++) {
for(int j=c+1; j>=0; j--) {
if(arr[i][j] == '#') cur = {i, j-1};
if(arr[i][j-1] == '#') dir[3][i][j] = cur;
}
}
q.push({stx, sty, 0});
while(q.size()) {
cx = get<0>(q.front());
cy = get<1>(q.front());
cdi = get<2>(q.front());
q.pop();
if(cx < 1 || cx > r || cy < 1 || cy > c || dp[cx][cy] <= cdi || arr[cx][cy] == '#') continue;
dp[cx][cy] = cdi;
for(int i=0; i<4; i++) {
if(arr[cx+dx[i]][cy+dy[i]] == '#') {
for(int j=0; j<4; j++) {
if(j == i) continue;
q.push({dir[i][cx][cy].first, dir[i][cx][cy].second, cdi+1});
}
}
q.push({cx+dx[i], cy+dy[i], cdi+1});
}
}
cout << dp[enx][eny];
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... |