#include <bits/stdc++.h>
using namespace std;
int n, m;
char c[1010][1010];
pair <int, int> nxt[1010][1010][4], s, t;
int mx[4] = {-1, 0, 1, 0};
int my[4] = {0, -1, 0, 1};
int d[1010][1010];
int dist_to_wall[1010][1010];
void bfs() {
queue <pair <int, int> > q;
for (int i = 0; i <= n + 1; ++i)
for (int j = 0; j <= m + 1; ++j)
if (c[i][j] == '#') {
dist_to_wall[i][j] = 0;
q.push({i, j});
}
else dist_to_wall[i][j] = 1e9;
while (!q.empty()) {
int u = q.front().first;
int v = q.front().second;
q.pop();
for (int i = 0; i < 4; ++i) {
int x = u + mx[i], y = v + my[i];
if (x < 0 || x > n + 1 || y < 0 || y > m + 1 || dist_to_wall[x][y] != 1e9) continue;
dist_to_wall[x][y] = dist_to_wall[u][v] + 1;
q.push({x, y});
}
}
}
void dijkstra() {
priority_queue <pair <int, pair <int, int> > > pq;
for (int i = 0; i <= n + 1; ++i)
for (int j = 0; j <= m + 1; ++j)
d[i][j] = 1e9;
d[s.first][s.second] = 0;
pq.push({0, s});
while (!pq.empty()) {
int u = pq.top().second.first;
int v = pq.top().second.second;
int dist = - pq.top().first;
pq.pop();
if (dist != d[u][v]) continue;
for (int i = 0; i < 4; ++i) {
int x = u + mx[i];
int y = v + my[i];
if (c[x][y] != '#' && d[x][y] > d[u][v] + 1) {
d[x][y] = d[u][v] + 1;
pq.push({- d[x][y], {x, y}});
}
x = nxt[u][v][i].first, y = nxt[u][v][i].second;
if (d[u][v] + dist_to_wall[u][v] < d[x][y]) {
d[x][y] = d[u][v] + dist_to_wall[u][v];
pq.push({- d[x][y], {x, y}});
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i <= n + 1; ++i)
for (int j = 0; j <= m + 1; ++j)
c[i][j] = '#';
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
scanf(" %c", &c[i][j]);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
if (c[i][j] == 'S') s = {i, j};
if (c[i][j] == 'C') t = {i, j};
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
if (c[i - 1][j] == '#') nxt[i][j][2] = {i, j};
else nxt[i][j][2] = nxt[i - 1][j][2];
if (c[i][j - 1] == '#') nxt[i][j][3] = {i, j};
else nxt[i][j][3] = nxt[i][j - 1][3];
}
for (int i = n; i >= 1; --i)
for (int j = m; j >= 1; --j) {
if (c[i + 1][j] == '#') nxt[i][j][0] = {i, j};
else nxt[i][j][0] = nxt[i + 1][j][0];
if (c[i][j + 1] == '#') nxt[i][j][1] = {i, j};
else nxt[i][j][1] = nxt[i][j + 1][1];
}
bfs();
dijkstra();
cout << d[t.first][t.second];
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
portals.cpp:71:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf(" %c", &c[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
432 KB |
Output is correct |
3 |
Correct |
1 ms |
432 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
436 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
428 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
944 KB |
Output is correct |
11 |
Correct |
1 ms |
944 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
432 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
8 ms |
4380 KB |
Output is correct |
6 |
Correct |
12 ms |
4308 KB |
Output is correct |
7 |
Correct |
10 ms |
4308 KB |
Output is correct |
8 |
Correct |
5 ms |
4384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
1 ms |
436 KB |
Output is correct |
7 |
Correct |
1 ms |
436 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
948 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
948 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
8 ms |
4308 KB |
Output is correct |
15 |
Correct |
9 ms |
4384 KB |
Output is correct |
16 |
Correct |
9 ms |
4424 KB |
Output is correct |
17 |
Correct |
9 ms |
4256 KB |
Output is correct |
18 |
Correct |
10 ms |
4308 KB |
Output is correct |
19 |
Correct |
11 ms |
4180 KB |
Output is correct |
20 |
Correct |
11 ms |
4188 KB |
Output is correct |
21 |
Correct |
9 ms |
4284 KB |
Output is correct |
22 |
Correct |
9 ms |
4296 KB |
Output is correct |
23 |
Correct |
11 ms |
4360 KB |
Output is correct |
24 |
Correct |
10 ms |
4156 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
944 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
6 ms |
4308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
432 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
952 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
944 KB |
Output is correct |
14 |
Correct |
7 ms |
4308 KB |
Output is correct |
15 |
Correct |
9 ms |
4288 KB |
Output is correct |
16 |
Correct |
9 ms |
4308 KB |
Output is correct |
17 |
Correct |
9 ms |
4252 KB |
Output is correct |
18 |
Correct |
11 ms |
4308 KB |
Output is correct |
19 |
Correct |
11 ms |
4224 KB |
Output is correct |
20 |
Correct |
11 ms |
4252 KB |
Output is correct |
21 |
Correct |
9 ms |
4308 KB |
Output is correct |
22 |
Correct |
9 ms |
4400 KB |
Output is correct |
23 |
Correct |
9 ms |
4320 KB |
Output is correct |
24 |
Correct |
213 ms |
44604 KB |
Output is correct |
25 |
Correct |
354 ms |
42136 KB |
Output is correct |
26 |
Correct |
292 ms |
42068 KB |
Output is correct |
27 |
Correct |
281 ms |
41968 KB |
Output is correct |
28 |
Correct |
176 ms |
45508 KB |
Output is correct |
29 |
Correct |
203 ms |
46552 KB |
Output is correct |
30 |
Correct |
219 ms |
46472 KB |
Output is correct |
31 |
Correct |
8 ms |
4160 KB |
Output is correct |
32 |
Correct |
255 ms |
41812 KB |
Output is correct |
33 |
Correct |
1 ms |
304 KB |
Output is correct |
34 |
Correct |
1 ms |
980 KB |
Output is correct |
35 |
Correct |
202 ms |
43900 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
6 ms |
4416 KB |
Output is correct |
38 |
Correct |
141 ms |
44732 KB |
Output is correct |
39 |
Correct |
166 ms |
45788 KB |
Output is correct |