#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
pair<int, int> L[maxn][maxn];
pair<int, int> R[maxn][maxn];
pair<int, int> U[maxn][maxn];
pair<int, int> D[maxn][maxn];
vector<pair<int, int>> graf[maxn][maxn];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(11);
cerr << fixed << setprecision(6);
int n, m;
cin >> n >> m;
vector<vector<int>> a(n + 2, vector<int>(m + 2));
int sx, sy, ex, ey;
for (int i = 1; i <= n; ++i) {
string in;
cin >> in;
for (int j = 1; j <= m; ++j) {
char c = in[j - 1];
a[i][j] = (c == '#' ? 0 : 1);
if (c == 'S') sx = i, sy = j;
if (c == 'C') ex = i, ey = j;
}
}
vector<vector<int>> wall(n + 2, vector<int>(m + 2));
queue<pair<int, int>> q;
for (int i = 0; i <= n + 1; ++i) {
for (int j = 0; j <= m + 1; ++j) {
wall[i][j] = (a[i][j] == 0 ? 0 : -1);
if (a[i][j] == 0) q.push({i, j});
}
}
auto IsCell = [&](int x, int y) {
return 1 <= x && x <= n && 1 <= y && y <= m;
};
while (q.size()) {
int x = q.front().first, y = q.front().second;
q.pop();
for (int dir = 0; dir < 4; dir++) {
int nx = x + dx[dir], ny = y + dy[dir];
if (IsCell(nx, ny) && wall[nx][ny] == -1) {
wall[nx][ny] = wall[x][y] + 1;
q.push({nx, ny});
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (j == 1 || a[i][j - 1] == 0) L[i][j] = {i, j};
else L[i][j] = L[i][j - 1];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = m; j >= 1; --j) {
if (j == m || a[i][j + 1] == 0) R[i][j] = {i, j};
else R[i][j] = R[i][j + 1];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (i == 1 || a[i - 1][j] == 0) U[i][j] = {i, j};
else U[i][j] = U[i - 1][j];
}
}
for (int i = n; i >= 1; --i) {
for (int j = 1; j <= m; ++j) {
if (i == n || a[i + 1][j] == 0) D[i][j] = {i, j};
else D[i][j] = D[i + 1][j];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
graf[i][j].push_back(L[i][j]);
graf[i][j].push_back(R[i][j]);
graf[i][j].push_back(U[i][j]);
graf[i][j].push_back(D[i][j]);
}
}
set<tuple<int, int, int>> que;
vector<vector<int>> dist(n + 1, vector<int>(m + 1, 1e9));
dist[sx][sy] = 0;
que.insert({0, sx, sy});
while (que.size()) {
auto t = *que.begin();
int d = get<0>(t);
int x = get<1>(t);
int y = get<2>(t);
que.erase(que.begin());
if (d > dist[x][y]) continue;
for (int dir = 0; dir < 4; dir++) {
int nx = x + dx[dir], ny = y + dy[dir];
if (IsCell(nx, ny) && a[nx][ny] == 1 && dist[nx][ny] > dist[x][y] + 1) {
dist[nx][ny] = dist[x][y] + 1;
que.insert({dist[nx][ny], nx, ny});
}
}
for (auto c : graf[x][y]) {
int nx = c.first;
int ny = c.second;
if (dist[nx][ny] > dist[x][y] + wall[x][y]) {
dist[nx][ny] = dist[x][y] + wall[x][y];
que.insert({dist[nx][ny], nx, ny});
}
}
}
int ans = dist[ex][ey];
cout << (ans == 1e9 ? -1 : ans) << "\n";
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:133:23: warning: 'ey' may be used uninitialized in this function [-Wmaybe-uninitialized]
133 | int ans = dist[ex][ey];
| ^
portals.cpp:133:19: warning: 'ex' may be used uninitialized in this function [-Wmaybe-uninitialized]
133 | int ans = dist[ex][ey];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
24012 KB |
Output is correct |
2 |
Correct |
13 ms |
24212 KB |
Output is correct |
3 |
Correct |
12 ms |
24136 KB |
Output is correct |
4 |
Correct |
13 ms |
24136 KB |
Output is correct |
5 |
Correct |
13 ms |
24156 KB |
Output is correct |
6 |
Correct |
14 ms |
24176 KB |
Output is correct |
7 |
Correct |
16 ms |
24196 KB |
Output is correct |
8 |
Correct |
13 ms |
24140 KB |
Output is correct |
9 |
Correct |
14 ms |
24012 KB |
Output is correct |
10 |
Correct |
15 ms |
24084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
24012 KB |
Output is correct |
2 |
Correct |
12 ms |
24140 KB |
Output is correct |
3 |
Correct |
13 ms |
24216 KB |
Output is correct |
4 |
Correct |
12 ms |
24092 KB |
Output is correct |
5 |
Correct |
13 ms |
24140 KB |
Output is correct |
6 |
Correct |
13 ms |
24140 KB |
Output is correct |
7 |
Correct |
13 ms |
24204 KB |
Output is correct |
8 |
Correct |
16 ms |
24188 KB |
Output is correct |
9 |
Correct |
14 ms |
25112 KB |
Output is correct |
10 |
Correct |
15 ms |
25096 KB |
Output is correct |
11 |
Correct |
17 ms |
25040 KB |
Output is correct |
12 |
Correct |
14 ms |
25052 KB |
Output is correct |
13 |
Correct |
14 ms |
25064 KB |
Output is correct |
14 |
Correct |
13 ms |
24016 KB |
Output is correct |
15 |
Correct |
14 ms |
25040 KB |
Output is correct |
16 |
Correct |
13 ms |
24056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
24044 KB |
Output is correct |
2 |
Correct |
13 ms |
24140 KB |
Output is correct |
3 |
Correct |
13 ms |
24100 KB |
Output is correct |
4 |
Correct |
13 ms |
24220 KB |
Output is correct |
5 |
Correct |
25 ms |
30836 KB |
Output is correct |
6 |
Correct |
26 ms |
30848 KB |
Output is correct |
7 |
Correct |
28 ms |
30928 KB |
Output is correct |
8 |
Correct |
23 ms |
30892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
24012 KB |
Output is correct |
2 |
Correct |
16 ms |
24140 KB |
Output is correct |
3 |
Correct |
14 ms |
24140 KB |
Output is correct |
4 |
Correct |
13 ms |
24136 KB |
Output is correct |
5 |
Correct |
13 ms |
24268 KB |
Output is correct |
6 |
Correct |
13 ms |
24164 KB |
Output is correct |
7 |
Correct |
13 ms |
24172 KB |
Output is correct |
8 |
Correct |
13 ms |
24152 KB |
Output is correct |
9 |
Correct |
18 ms |
24996 KB |
Output is correct |
10 |
Correct |
15 ms |
25032 KB |
Output is correct |
11 |
Correct |
14 ms |
25068 KB |
Output is correct |
12 |
Correct |
15 ms |
25040 KB |
Output is correct |
13 |
Correct |
14 ms |
25104 KB |
Output is correct |
14 |
Correct |
25 ms |
30840 KB |
Output is correct |
15 |
Correct |
25 ms |
30908 KB |
Output is correct |
16 |
Correct |
26 ms |
30908 KB |
Output is correct |
17 |
Correct |
29 ms |
30932 KB |
Output is correct |
18 |
Correct |
32 ms |
30976 KB |
Output is correct |
19 |
Correct |
39 ms |
30932 KB |
Output is correct |
20 |
Correct |
28 ms |
30928 KB |
Output is correct |
21 |
Correct |
26 ms |
30840 KB |
Output is correct |
22 |
Correct |
25 ms |
30800 KB |
Output is correct |
23 |
Correct |
27 ms |
30936 KB |
Output is correct |
24 |
Correct |
27 ms |
30916 KB |
Output is correct |
25 |
Correct |
13 ms |
24016 KB |
Output is correct |
26 |
Correct |
15 ms |
25040 KB |
Output is correct |
27 |
Correct |
15 ms |
24064 KB |
Output is correct |
28 |
Correct |
25 ms |
30840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
24012 KB |
Output is correct |
2 |
Correct |
13 ms |
24140 KB |
Output is correct |
3 |
Correct |
13 ms |
24140 KB |
Output is correct |
4 |
Correct |
14 ms |
24012 KB |
Output is correct |
5 |
Correct |
14 ms |
24168 KB |
Output is correct |
6 |
Correct |
12 ms |
24140 KB |
Output is correct |
7 |
Correct |
13 ms |
24140 KB |
Output is correct |
8 |
Correct |
13 ms |
24168 KB |
Output is correct |
9 |
Correct |
14 ms |
25040 KB |
Output is correct |
10 |
Correct |
14 ms |
25040 KB |
Output is correct |
11 |
Correct |
18 ms |
24964 KB |
Output is correct |
12 |
Correct |
15 ms |
25040 KB |
Output is correct |
13 |
Correct |
19 ms |
25040 KB |
Output is correct |
14 |
Correct |
26 ms |
30896 KB |
Output is correct |
15 |
Correct |
26 ms |
30920 KB |
Output is correct |
16 |
Correct |
26 ms |
30928 KB |
Output is correct |
17 |
Correct |
26 ms |
30928 KB |
Output is correct |
18 |
Correct |
32 ms |
30936 KB |
Output is correct |
19 |
Correct |
30 ms |
30952 KB |
Output is correct |
20 |
Correct |
31 ms |
31064 KB |
Output is correct |
21 |
Correct |
32 ms |
30836 KB |
Output is correct |
22 |
Correct |
26 ms |
30904 KB |
Output is correct |
23 |
Correct |
25 ms |
30808 KB |
Output is correct |
24 |
Correct |
353 ms |
115772 KB |
Output is correct |
25 |
Correct |
583 ms |
116208 KB |
Output is correct |
26 |
Correct |
412 ms |
115764 KB |
Output is correct |
27 |
Correct |
422 ms |
115760 KB |
Output is correct |
28 |
Correct |
320 ms |
115504 KB |
Output is correct |
29 |
Correct |
322 ms |
115484 KB |
Output is correct |
30 |
Correct |
360 ms |
115548 KB |
Output is correct |
31 |
Correct |
25 ms |
30928 KB |
Output is correct |
32 |
Correct |
414 ms |
115388 KB |
Output is correct |
33 |
Correct |
13 ms |
24016 KB |
Output is correct |
34 |
Correct |
14 ms |
25044 KB |
Output is correct |
35 |
Correct |
345 ms |
115772 KB |
Output is correct |
36 |
Correct |
13 ms |
24016 KB |
Output is correct |
37 |
Correct |
22 ms |
30836 KB |
Output is correct |
38 |
Correct |
230 ms |
115452 KB |
Output is correct |
39 |
Correct |
288 ms |
115520 KB |
Output is correct |