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);
ll n, m;
cin >> n >> m;
ll stai, staj, fini, finj;
vector<vector<char>> a(n, vector<char>(m));
for (ll i = 0; i < n; i++) {
for (ll 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<vector<ll>>>> g(n, vector<vector<vector<ll>>>(m));
vector<vector<vector<pair<ll, ll>>>> steni(n, vector<vector<pair<ll, ll>>>(m));
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < m; j++) {
if (a[i][j] == '#')
continue;
for (ll c = 0; c < 4; c++) {
ll 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, 1});
}
}
}
}
for (ll i = 0; i < n; i++) {
pair<ll, ll> cur = {i, 0};
for (ll j = 0; j < m; j++) {
if (a[i][j] == '#')
cur = {i, j + 1};
else
steni[i][j].push_back(cur);
}
cur = {i, m - 1};
for (ll j = m - 1; j >= 0; j--) {
if (a[i][j] == '#')
cur = {i, j - 1};
else
steni[i][j].push_back(cur);
}
}
for (ll j = 0; j < m; j++) {
pair<ll, ll> cur = {0, j};
for (ll i = 0; i < n; i++) {
if (a[i][j] == '#')
cur = {i + 1, j};
else
steni[i][j].push_back(cur);
}
cur = {n - 1, j};
for (ll i = n - 1; i >= 0; i--) {
if (a[i][j] == '#')
cur = {i - 1, j};
else
steni[i][j].push_back(cur);
}
}
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < m; j++) {
ll mst = inf;
ll inde = -1;
for (ll qw = 0; qw < steni[i][j].size(); qw++) {
auto [si, sj] = steni[i][j][qw];
if (abs(si - i) + abs(sj - j) < mst) {
mst = abs(si - i) + abs(sj - j);
inde = qw;
}
}
for (ll qw = 0; qw < steni[i][j].size(); qw++) {
auto [si, sj] = steni[i][j][qw];
if (qw != inde && abs(si - i) + abs(sj - j) > mst + 1) {
g[i][j].push_back({si, sj, mst + 1});
}
}
}
}
vector<vector<pair<ll, ll>>> par(n, vector<pair<ll, ll>>(m, {-1, -1}));
vector<vector<ll>> dist(n, vector<ll>(m, inf));
dist[stai][staj] = 0;
set<vector<ll>> st = {{0, stai, staj}};
while (!st.empty()) {
auto t = st.begin();
auto qw = *t;
ll d = qw[0], vi = qw[1], vj = qw[2];
st.erase(t);
for (auto vec : g[vi][vj]) {
ll ui = vec[0], uj = vec[1], w = vec[2];
if (d + w < dist[ui][uj]) {
par[ui][uj] = {vi, vj};
st.erase({dist[ui][uj], ui, uj});
dist[ui][uj] = d + w;
st.insert({d + w, ui, uj});
}
}
}
cout << dist[fini][finj] << '\n';
}
Compilation message (stderr)
portals.cpp: In function 'int main()':
portals.cpp:82:32: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (ll qw = 0; qw < steni[i][j].size(); qw++) {
| ~~~^~~~~~~~~~~~~~~~~~~~
portals.cpp:89:32: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (ll qw = 0; qw < steni[i][j].size(); qw++) {
| ~~~^~~~~~~~~~~~~~~~~~~~
portals.cpp:116:28: warning: 'finj' may be used uninitialized in this function [-Wmaybe-uninitialized]
116 | cout << dist[fini][finj] << '\n';
| ^
portals.cpp:116:22: warning: 'fini' may be used uninitialized in this function [-Wmaybe-uninitialized]
116 | cout << dist[fini][finj] << '\n';
| ^
portals.cpp:99:20: warning: 'staj' may be used uninitialized in this function [-Wmaybe-uninitialized]
99 | dist[stai][staj] = 0;
| ^
portals.cpp:99:14: warning: 'stai' may be used uninitialized in this function [-Wmaybe-uninitialized]
99 | 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... |