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 <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <iomanip>
using namespace std;
const int maxr = 1005, maxrc = maxr * maxr;
int r, c, start = -1, finish = -1;
inline int id(const int &x, const int &y) { return x * c + y; }
int g[maxrc][4], wall[maxrc], nxt[maxrc][4], dist[maxrc];
string s[maxr];
priority_queue<pair<int, int>> pq;
inline void check(const int &u, const int &d)
{
if (d < dist[u]) dist[u] = d, pq.push({ -d, u });
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
for (int i = 0; i < maxrc; i++) for (int j = 0; j < 4; j++) g[i][j] = nxt[i][j] = -1;
for (int i = 0; i < maxrc; i++) wall[i] = -1, dist[i] = 1e9 + 5;
cin >> r >> c;
s[0] = s[r + 1] = string(c + 2, '#');
for (int i = 1; i <= r; i++) cin >> s[i], s[i].insert(s[i].begin(), '#'), s[i].push_back('#');
r += 2, c += 2;
queue<int> q;
for (int i = 0; i < r; i++) for (int j = 0; j < c; j++)
{
if (s[i][j] == 'S') start = id(i, j);
if (s[i][j] == 'C') finish = id(i, j);
if (s[i][j] == '#') wall[id(i, j)] = 0, q.push(id(i, j));
if (i && s[i - 1][j] != '#') g[id(i, j)][0] = id(i - 1, j);
if (i < r - 1 && s[i + 1][j] != '#') g[id(i, j)][1] = id(i + 1, j);
if (j && s[i][j - 1] != '#') g[id(i, j)][2] = id(i, j - 1);
if (j < c - 1 && s[i][j + 1] != '#') g[id(i, j)][3] = id(i, j + 1);
if (i && s[i - 1][j] == '#') nxt[id(i, j)][0] = id(i, j);
if (i < r - 1 && s[i + 1][j] == '#') nxt[id(i, j)][1] = id(i, j);
if (j && s[i][j - 1] == '#') nxt[id(i, j)][2] = id(i, j);
if (j < c - 1 && s[i][j + 1] == '#') nxt[id(i, j)][3] = id(i, j);
}
while (!q.empty())
{
int i = q.front();
q.pop();
for (int j = 0; j < 4; j++) if (g[i][j] != -1 && wall[g[i][j]] == -1)
wall[g[i][j]] = wall[i] + 1, q.push(g[i][j]);
}
for (int i = 1; i < r; i++) for (int j = 0; j < c; j++) if (nxt[id(i, j)][0] == -1) nxt[id(i, j)][0] = nxt[id(i - 1, j)][0];
for (int i = r-2; i >= 0; i--) for (int j = 0; j < c; j++) if (nxt[id(i, j)][1] == -1) nxt[id(i, j)][1] = nxt[id(i + 1, j)][1];
for (int i = 0; i < r; i++) for (int j = 1; j < c; j++) if (nxt[id(i, j)][2] == -1) nxt[id(i, j)][2] = nxt[id(i, j - 1)][2];
for (int i = 0; i < r; i++) for (int j = c-2; j >= 0; j--) if (nxt[id(i, j)][3] == -1) nxt[id(i, j)][3] = nxt[id(i, j + 1)][3];
check(start, 0);
while (!pq.empty())
{
int u = pq.top().second, d = -pq.top().first;
pq.pop();
if (dist[u] != d) continue;
for (int i = 0; i < 4; i++) if (g[u][i] != -1) check(g[u][i], d + 1);
for (int i = 0; i < 4; i++) check(nxt[u][i], d + wall[u]);
}
cout << dist[finish] << "\n";
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... |