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;
typedef long long ll;
#define fi first
#define se second
typedef long double ld;
const ll inf = 1e9;
const ld eps = 1e-8;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
int stai, staj, fini, finj;
vector<vector<char>> a(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
if (a[i][j] == 'S') {
stai = i;
staj = j;
} else if (a[i][j] == 'C') {
fini = i;
finj = j;
}
}
}
vector<vector<vector<pair<int, int>>>> g(n, vector<vector<pair<int, int>>>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == '#')
continue;
for (int c = 0; c < 5; c++) {
int ci = (c % 2 == 0) * (c - 1),
cj = (c % 2 == 1) * (c - 2);
if (i + ci >= 0 && i + ci < n && j + cj >= 0 && j + cj < m && a[i + ci][j + cj] != '#') {
g[i][j].push_back({i + ci, j + cj});
}
}
}
}
for (int i = 0; i < n; i++) {
pair<int, int> cur = {i, 0};
for (int j = 0; j < m; j++) {
if (a[i][j] == '#')
cur = {i, j + 1};
else
g[i][j].push_back(cur);
}
cur = {i, m - 1};
for (int j = m - 1; j >= 0; j--) {
if (a[i][j] == '#')
cur = {i, j - 1};
else
g[i][j].push_back(cur);
}
}
for (int j = 0; j < m; j++) {
pair<int, int> cur = {0, j};
for (int i = 0; i < n; i++) {
if (a[i][j] == '#')
cur = {i + 1, j};
else
g[i][j].push_back(cur);
}
cur = {n - 1, j};
for (int i = n - 1; i >= 0; i--) {
if (a[i][j] == '#')
cur = {i - 1, j};
else
g[i][j].push_back(cur);
}
}
vector<vector<int>> dist(n, vector<int>(m, inf));
dist[stai][staj] = 0;
set<vector<int>> st = {{0, stai, staj}};
while (!st.empty()) {
auto t = st.begin();
auto qw = *t;
int d = qw[0], vi = qw[1], vj = qw[2];
st.erase(t);
for (auto [ui, uj] : g[vi][vj]) {
if (d + 1 < dist[ui][uj]) {
st.erase({dist[ui][uj], ui, uj});
dist[ui][uj] = d + 1;
st.insert({d + 1, ui, uj});
}
}
}
cout << dist[fini][finj] << '\n';
}
Compilation message (stderr)
portals.cpp: In function 'int main()':
portals.cpp:93:28: warning: 'finj' may be used uninitialized in this function [-Wmaybe-uninitialized]
93 | cout << dist[fini][finj] << '\n';
| ^
portals.cpp:93:22: warning: 'fini' may be used uninitialized in this function [-Wmaybe-uninitialized]
93 | cout << dist[fini][finj] << '\n';
| ^
portals.cpp:78:20: warning: 'staj' may be used uninitialized in this function [-Wmaybe-uninitialized]
78 | dist[stai][staj] = 0;
| ^
portals.cpp:78:14: warning: 'stai' may be used uninitialized in this function [-Wmaybe-uninitialized]
78 | dist[stai][staj] = 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... |