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;
char s[1004][1004];
int d[1004][1004];
int dis[1004][1004];
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
typedef pair <int, int> pii;
const int inf = 1e9;
pii cell[4][1004][1004];
int n, m;
bool inside(int x, int y) {
return (0 <= x && x < n) && (0 <= y && y < m);
}
struct node {
int x, y;
int dist;
node () {}
node (int x, int y, int dist) : x(x), y(y), dist(dist) {}
bool operator < (node p) const {
return dist > p.dist;
}
};
int main(int argc, char const *argv[])
{
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++) {
scanf("%s", s[i]);
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
d[i][j] = inf;
}
}
queue <pii> Q;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(s[i][j] == '#') continue;
bool good = false;
for(int x = 0; x < 4; x++) {
int p = i + dx[x];
int q = j + dy[x];
if(!inside(p, q) || s[p][q] == '#') {
good = true;
break;
}
}
if(good) {
Q.push(pii(i, j));
d[i][j] = 0;
}
}
}
while(!Q.empty()) {
int x = Q.front().first;
int y = Q.front().second;
Q.pop();
for(int i = 0; i < 4; i++) {
int p = x + dx[i];
int q = y + dy[i];
if(d[p][q] > d[x][y] + 1 && s[p][q] != '#') {
d[p][q] = d[x][y] + 1;
Q.push(pii(p, q));
}
}
}
for(int x = 0; x < n; x++) {
for(int y = 0; y < m; y++) {
if(s[x][y] == '#') continue;
if(inside(x-1, y) && s[x-1][y] != '#') {
cell[0][x][y] = cell[0][x-1][y];
} else cell[0][x][y] = pii(x, y);
if(inside(x, y-1) && s[x][y-1] != '#') {
cell[1][x][y] = cell[1][x][y-1];
} else cell[1][x][y] = pii(x, y);
}
}
for(int x = n-1; x >= 0; x--) {
for(int y = m-1; y >= 0; y--) {
if(s[x][y] == '#') continue;
if(inside(x+1, y) && s[x+1][y] != '#') {
cell[2][x][y] = cell[2][x+1][y];
} else cell[2][x][y] = pii(x, y);
if(inside(x, y+1) && s[x][y+1] != '#') {
cell[3][x][y] = cell[3][x][y+1];
} else cell[3][x][y] = pii(x, y);
}
}
priority_queue <node> PQ;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(s[i][j] == 'S') {
dis[i][j] = 0;
PQ.push(node(i, j, 0));
} else {
dis[i][j] = inf;
}
}
}
while(!PQ.empty()) {
node h = PQ.top();
PQ.pop();
for(int i = 0; i < 4; i++) {
int x = h.x + dx[i];
int y = h.y + dy[i];
if(inside(x, y) && s[x][y] != '#') {
if(dis[x][y] > 1 + dis[h.x][h.y]) {
dis[x][y] = 1 + dis[h.x][h.y];
PQ.push(node(x, y, dis[x][y]));
}
}
int p = cell[i][h.x][h.y].first;
int q = cell[i][h.x][h.y].second;
if(dis[p][q] > dis[h.x][h.y] + d[h.x][h.y] + 1) {
dis[p][q] = dis[h.x][h.y] + d[h.x][h.y] + 1;
PQ.push(node(p, q, dis[p][q]));
}
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(s[i][j] == 'C') {
printf("%d\n", dis[i][j]);
}
}
}
return 0;
}
Compilation message (stderr)
portals.cpp: In function 'int main(int, const char**)':
portals.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
portals.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", s[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... |