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 n, m;
char c[1010][1010];
pair <int, int> nxt[1010][1010][4], s, t;
int mx[4] = {-1, 0, 1, 0};
int my[4] = {0, -1, 0, 1};
int d[1010][1010];
int dist_to_wall[1010][1010];
void bfs() {
queue <pair <int, int> > q;
for (int i = 0; i <= n + 1; ++i)
for (int j = 0; j <= m + 1; ++j)
if (c[i][j] == '#') {
dist_to_wall[i][j] = 0;
q.push({i, j});
}
else dist_to_wall[i][j] = 1e9;
while (!q.empty()) {
int u = q.front().first;
int v = q.front().second;
q.pop();
for (int i = 0; i < 4; ++i) {
int x = u + mx[i], y = v + my[i];
if (x < 0 || x > n + 1 || y < 0 || y > m + 1 || dist_to_wall[x][y] != 1e9) continue;
dist_to_wall[x][y] = dist_to_wall[u][v] + 1;
q.push({x, y});
}
}
}
void dijkstra() {
priority_queue <pair <int, pair <int, int> > > pq;
for (int i = 0; i <= n + 1; ++i)
for (int j = 0; j <= m + 1; ++j)
d[i][j] = 1e9;
d[s.first][s.second] = 0;
pq.push({0, s});
while (!pq.empty()) {
int u = pq.top().second.first;
int v = pq.top().second.second;
int dist = - pq.top().first;
pq.pop();
if (dist != d[u][v]) continue;
for (int i = 0; i < 4; ++i) {
int x = u + mx[i];
int y = v + my[i];
if (c[x][y] != '#' && d[x][y] > d[u][v] + 1) {
d[x][y] = d[u][v] + 1;
pq.push({- d[x][y], {x, y}});
}
x = nxt[u][v][i].first, y = nxt[u][v][i].second;
if (d[u][v] + dist_to_wall[u][v] < d[x][y]) {
d[x][y] = d[u][v] + dist_to_wall[u][v];
pq.push({- d[x][y], {x, y}});
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i <= n + 1; ++i)
for (int j = 0; j <= m + 1; ++j)
c[i][j] = '#';
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
scanf(" %c", &c[i][j]);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
if (c[i][j] == 'S') s = {i, j};
if (c[i][j] == 'C') t = {i, j};
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
if (c[i - 1][j] == '#') nxt[i][j][2] = {i, j};
else nxt[i][j][2] = nxt[i - 1][j][2];
if (c[i][j - 1] == '#') nxt[i][j][3] = {i, j};
else nxt[i][j][3] = nxt[i][j - 1][3];
}
for (int i = n; i >= 1; --i)
for (int j = m; j >= 1; --j) {
if (c[i + 1][j] == '#') nxt[i][j][0] = {i, j};
else nxt[i][j][0] = nxt[i + 1][j][0];
if (c[i][j + 1] == '#') nxt[i][j][1] = {i, j};
else nxt[i][j][1] = nxt[i][j + 1][1];
}
bfs();
dijkstra();
cout << d[t.first][t.second];
}
Compilation message (stderr)
portals.cpp: In function 'int main()':
portals.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
portals.cpp:71:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf(" %c", &c[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~
# | 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... |