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 int 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<int>>> g(n * m);
vector<vector<pair<int, int>>> steni(n * 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 < 4; 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 * m + j].push_back({i + ci, j + cj, 1});
}
}
}
}
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
steni[i * m + 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
steni[i * m + 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
steni[i * m + 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
steni[i * m + j].push_back(cur);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int mst = inf;
int inde = -1;
for (int qw = 0; qw < steni[i * m + j].size(); qw++) {
auto [si, sj] = steni[i * m + j][qw];
if (abs(si - i) + abs(sj - j) < mst) {
mst = abs(si - i) + abs(sj - j);
inde = qw;
}
}
for (int qw = 0; qw < steni[i * m + j].size(); qw++) {
auto [si, sj] = steni[i * m + j][qw];
if (qw != inde && abs(si - i) + abs(sj - j) > mst + 1) {
g[i * m + j].push_back({si, sj, mst + 1});
}
}
}
}
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 vec : g[vi * m + vj]) {
int ui = vec[0], uj = vec[1], w = vec[2];
if (d + w < dist[ui][uj]) {
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:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (int qw = 0; qw < steni[i * m + j].size(); qw++) {
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
portals.cpp:89:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int qw = 0; qw < steni[i * m + j].size(); qw++) {
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
portals.cpp:114:28: warning: 'finj' may be used uninitialized in this function [-Wmaybe-uninitialized]
114 | cout << dist[fini][finj] << '\n';
| ^
portals.cpp:114:22: warning: 'fini' may be used uninitialized in this function [-Wmaybe-uninitialized]
114 | cout << dist[fini][finj] << '\n';
| ^
portals.cpp:98:20: warning: 'staj' may be used uninitialized in this function [-Wmaybe-uninitialized]
98 | dist[stai][staj] = 0;
| ^
portals.cpp:98:14: warning: 'stai' may be used uninitialized in this function [-Wmaybe-uninitialized]
98 | 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... |