#include <bits/stdc++.h>
using namespace std;
#define pii pair <int , int>
#define F first
#define S second
#define num(i, j) (i * (m + 1) + j)
#define IsAdjToWall(i, j) (!(A[i - 1][j] && A[i + 1][j] && A[i][j - 1] && A[i][j + 1]))
const int N = 1e3 + 5, inf = N*N;
int A[N][N], dis[N][N], sid[N][N], nxt[N][N][4], n, m, S, E;
pii dir[] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
void BFS() {
for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) sid[i][j] = inf;
queue <int> q;
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++)
if (A[i][j] && IsAdjToWall(i, j)) sid[i][j] = 0, q.push(num(i, j));
while (!q.empty()) {
int x = q.front() / (m + 1), y = q.front() % (m + 1); q.pop();
for (int i = 0; i < 4; i++) {
int _x = x + dir[i].F, _y = y + dir[i].S;
if (A[_x][_y] && sid[x][y] + 1 < sid[_x][_y])
sid[_x][_y] = sid[x][y] + 1, q.push(num(_x, _y));
}
}
}
void Dijkstra() {
for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) dis[i][j] = inf;
priority_queue <pii, vector<pii>, greater<pii>> pq;
dis[S / (m + 1)][S % (m + 1)] = 0;
pq.push({0, S});
while (!pq.empty()) {
int x = pq.top().S / (m + 1), y = pq.top().S % (m + 1); pq.pop();
for (int i = 0; i < 4; i++) {
int _x = x + dir[i].F, _y = y + dir[i].S;
if (A[_x][_y] && dis[x][y] + 1 < dis[_x][_y])
dis[_x][_y] = dis[x][y] + 1, pq.push({dis[_x][_y], num(_x, _y)});
_x = nxt[x][y][i] / (m + 1), _y = nxt[x][y][i] % (m + 1);
if (A[_x][_y] && dis[x][y] + sid[x][y] + 1 < dis[_x][_y])
dis[_x][_y] = dis[x][y] + sid[x][y] + 1, pq.push({dis[_x][_y], num(_x, _y)});
}
}
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) {
char c; cin >> c;
if (c != '#') A[i][j] = 1;
if (c == 'S') S = num(i, j);
if (c == 'C') E = num(i, j);
}
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (A[i][j]) {
nxt[i][j][0] = (A[i - 1][j] ? nxt[i - 1][j][0] : num(i, j));
nxt[i][j][2] = (A[i][j - 1] ? nxt[i][j - 1][2] : num(i, j));
}
for (int i = n; i >= 1; i--) for (int j = m; j >= 1; j--) if (A[i][j]) {
nxt[i][j][1] = (A[i + 1][j] ? nxt[i + 1][j][1] : num(i, j));
nxt[i][j][3] = (A[i][j + 1] ? nxt[i][j + 1][3] : num(i, j));
}
BFS(), Dijkstra();
cout << dis[E / (m + 1)][E % (m + 1)] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
1152 KB |
Output is correct |
10 |
Correct |
7 ms |
1152 KB |
Output is correct |
11 |
Correct |
6 ms |
1152 KB |
Output is correct |
12 |
Correct |
5 ms |
1152 KB |
Output is correct |
13 |
Correct |
6 ms |
1152 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
7 ms |
1152 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
12 ms |
4352 KB |
Output is correct |
6 |
Correct |
13 ms |
4224 KB |
Output is correct |
7 |
Correct |
14 ms |
4352 KB |
Output is correct |
8 |
Correct |
10 ms |
4352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
1152 KB |
Output is correct |
10 |
Correct |
6 ms |
1280 KB |
Output is correct |
11 |
Correct |
5 ms |
1152 KB |
Output is correct |
12 |
Correct |
5 ms |
1152 KB |
Output is correct |
13 |
Correct |
6 ms |
1280 KB |
Output is correct |
14 |
Correct |
12 ms |
4224 KB |
Output is correct |
15 |
Correct |
13 ms |
4224 KB |
Output is correct |
16 |
Correct |
14 ms |
4352 KB |
Output is correct |
17 |
Correct |
12 ms |
4224 KB |
Output is correct |
18 |
Correct |
14 ms |
4224 KB |
Output is correct |
19 |
Correct |
13 ms |
4224 KB |
Output is correct |
20 |
Correct |
15 ms |
4224 KB |
Output is correct |
21 |
Correct |
11 ms |
4224 KB |
Output is correct |
22 |
Correct |
14 ms |
4224 KB |
Output is correct |
23 |
Correct |
13 ms |
4224 KB |
Output is correct |
24 |
Correct |
14 ms |
4224 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
1152 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
9 ms |
4352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
1152 KB |
Output is correct |
10 |
Correct |
5 ms |
1152 KB |
Output is correct |
11 |
Correct |
5 ms |
1152 KB |
Output is correct |
12 |
Correct |
6 ms |
1280 KB |
Output is correct |
13 |
Correct |
5 ms |
1152 KB |
Output is correct |
14 |
Correct |
12 ms |
4352 KB |
Output is correct |
15 |
Correct |
12 ms |
4352 KB |
Output is correct |
16 |
Correct |
14 ms |
4352 KB |
Output is correct |
17 |
Correct |
12 ms |
4352 KB |
Output is correct |
18 |
Correct |
14 ms |
4224 KB |
Output is correct |
19 |
Correct |
14 ms |
4224 KB |
Output is correct |
20 |
Correct |
13 ms |
4224 KB |
Output is correct |
21 |
Correct |
11 ms |
4224 KB |
Output is correct |
22 |
Correct |
12 ms |
4352 KB |
Output is correct |
23 |
Correct |
13 ms |
4224 KB |
Output is correct |
24 |
Correct |
196 ms |
29060 KB |
Output is correct |
25 |
Correct |
309 ms |
29208 KB |
Output is correct |
26 |
Correct |
251 ms |
29176 KB |
Output is correct |
27 |
Correct |
242 ms |
29048 KB |
Output is correct |
28 |
Correct |
156 ms |
29004 KB |
Output is correct |
29 |
Correct |
169 ms |
29064 KB |
Output is correct |
30 |
Correct |
195 ms |
28936 KB |
Output is correct |
31 |
Correct |
12 ms |
4224 KB |
Output is correct |
32 |
Correct |
234 ms |
29048 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
1152 KB |
Output is correct |
35 |
Correct |
181 ms |
27100 KB |
Output is correct |
36 |
Correct |
5 ms |
512 KB |
Output is correct |
37 |
Correct |
9 ms |
4352 KB |
Output is correct |
38 |
Correct |
96 ms |
28996 KB |
Output is correct |
39 |
Correct |
119 ms |
23148 KB |
Output is correct |