#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) 0
#endif
#define pb push_back
#define i2 array<int, 2>
#define i3 array<int, 3>
#define ll long long
const int N = 1e3 + 8, dr[] = {1, -1, 0, 0}, dc[] = {0, 0, 1, -1};
int R, C, sr, sc, cr, cc, wall[N][N], dist[N][N];
bool mark[N][N];
i2 W[N][N][4];
char a[N][N];
vector<i3> adj[N][N];
bool ok(int r, int c) {
return (r <= R && c <= C && r >= 0 && c >= 0);
}
bool ko(int r, int c) {
return ok(r, c) && a[r][c] != '#';
}
void bfs() {
queue<i2> q;
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
if (a[i][j] == '#') {
wall[i][j] = 0;
q.push({i, j});
}
}
}
while (q.size()) {
auto [r, c] = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nr = r + dr[i];
int nc = c + dc[i];
if (ok(nr, nc) && wall[nr][nc] == -1) {
wall[nr][nc] = wall[r][c] + 1;
q.push({nr, nc});
}
}
}
/* for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
wall[i][j]--;
}
} */
}
void work() {
i2 last;
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
if (a[i][j] == '#') {
last = {i, j + 1};
}
W[i][j][3] = last;
}
}
for (int i = 0; i <= R; i++) {
for (int j = C; j >= 0; j--) {
if (a[i][j] == '#') {
last = {i, j - 1};;
}
W[i][j][1] = last;
}
}
for (int j = 0; j <= C; j++) {
for (int i = 0; i <= R; i++) {
if (a[i][j] == '#') {
last = {i + 1, j};
}
W[i][j][0] = last;
}
}
for (int j = 0; j <= C; j++) {
for (int i = R; i >= 0; i--) {
if (a[i][j] == '#') {
last = {i - 1, j};
}
W[i][j][2] = last;
}
}
for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
if (a[i][j] != '#') {
for (int x = 0; x < 4; x++) {
if (ko(i + dr[x], j + dc[x])) {
adj[i][j].pb({i + dr[x], j + dc[x], 1});
}
}
for (int x = 0; x < 4; x++) {
adj[i][j].pb({W[i][j][x][0], W[i][j][x][1], wall[i][j]});
}
}
}
}
}
void dijkstra() {
set<i3> s;
s.insert({0, sr, sc});
dist[sr][sc] = 0;
while (s.size()) {
auto [w, r, c] = *s.begin();
s.erase(s.begin());
if (mark[r][c]) {
continue;
}
mark[r][c] = 1;
for (auto [nr, nc, w] : adj[r][c]) {
if (dist[r][c] + w < dist[nr][nc]) {
dist[nr][nc] = dist[r][c] + w;
s.insert({dist[nr][nc], nr, nc});
}
}
}
}
void solve() {
for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) {a[i][j] = '#'; wall[i][j] = -1; dist[i][j] = N * N;}
cin >> R >> C;
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
cin >> a[i][j];
if (a[i][j] == 'S') {
sr = i;
sc = j;
}
if (a[i][j] == 'C') {
cr = i;
cc = j;
}
}
}
R++; C++;
bfs();
work();
dijkstra();
/* for (int i = 0; i <= R; i++) {
for (int j = 0; j <= C; j++) {
cerr << dist[i][j] << ' ';
}
cerr << '\n';
} */
cout << dist[cr][cc];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
35440 KB |
Output is correct |
2 |
Correct |
8 ms |
35164 KB |
Output is correct |
3 |
Correct |
8 ms |
35164 KB |
Output is correct |
4 |
Correct |
9 ms |
35164 KB |
Output is correct |
5 |
Correct |
8 ms |
35164 KB |
Output is correct |
6 |
Correct |
9 ms |
35416 KB |
Output is correct |
7 |
Correct |
8 ms |
35164 KB |
Output is correct |
8 |
Correct |
8 ms |
35164 KB |
Output is correct |
9 |
Correct |
8 ms |
35164 KB |
Output is correct |
10 |
Correct |
8 ms |
35164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
35164 KB |
Output is correct |
2 |
Correct |
8 ms |
35164 KB |
Output is correct |
3 |
Correct |
8 ms |
35164 KB |
Output is correct |
4 |
Correct |
8 ms |
35172 KB |
Output is correct |
5 |
Correct |
8 ms |
35312 KB |
Output is correct |
6 |
Correct |
8 ms |
35288 KB |
Output is correct |
7 |
Correct |
8 ms |
35164 KB |
Output is correct |
8 |
Correct |
8 ms |
35164 KB |
Output is correct |
9 |
Correct |
9 ms |
35420 KB |
Output is correct |
10 |
Correct |
9 ms |
35420 KB |
Output is correct |
11 |
Correct |
8 ms |
35416 KB |
Output is correct |
12 |
Correct |
8 ms |
35420 KB |
Output is correct |
13 |
Correct |
8 ms |
35420 KB |
Output is correct |
14 |
Correct |
7 ms |
35164 KB |
Output is correct |
15 |
Correct |
8 ms |
35420 KB |
Output is correct |
16 |
Correct |
8 ms |
35160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
35164 KB |
Output is correct |
2 |
Correct |
8 ms |
35164 KB |
Output is correct |
3 |
Correct |
8 ms |
35236 KB |
Output is correct |
4 |
Correct |
8 ms |
35308 KB |
Output is correct |
5 |
Correct |
17 ms |
43996 KB |
Output is correct |
6 |
Correct |
18 ms |
44124 KB |
Output is correct |
7 |
Correct |
22 ms |
44528 KB |
Output is correct |
8 |
Correct |
15 ms |
44380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
35164 KB |
Output is correct |
2 |
Correct |
9 ms |
35164 KB |
Output is correct |
3 |
Correct |
8 ms |
35184 KB |
Output is correct |
4 |
Correct |
9 ms |
35160 KB |
Output is correct |
5 |
Correct |
8 ms |
35244 KB |
Output is correct |
6 |
Correct |
8 ms |
35160 KB |
Output is correct |
7 |
Correct |
8 ms |
35164 KB |
Output is correct |
8 |
Correct |
7 ms |
35164 KB |
Output is correct |
9 |
Correct |
9 ms |
35420 KB |
Output is correct |
10 |
Correct |
10 ms |
35420 KB |
Output is correct |
11 |
Correct |
8 ms |
35420 KB |
Output is correct |
12 |
Correct |
8 ms |
35500 KB |
Output is correct |
13 |
Correct |
9 ms |
35420 KB |
Output is correct |
14 |
Correct |
17 ms |
43952 KB |
Output is correct |
15 |
Correct |
19 ms |
44124 KB |
Output is correct |
16 |
Correct |
19 ms |
44376 KB |
Output is correct |
17 |
Correct |
19 ms |
44380 KB |
Output is correct |
18 |
Correct |
21 ms |
44892 KB |
Output is correct |
19 |
Correct |
24 ms |
45916 KB |
Output is correct |
20 |
Correct |
22 ms |
45912 KB |
Output is correct |
21 |
Correct |
17 ms |
44124 KB |
Output is correct |
22 |
Correct |
17 ms |
44124 KB |
Output is correct |
23 |
Correct |
19 ms |
44380 KB |
Output is correct |
24 |
Correct |
22 ms |
45916 KB |
Output is correct |
25 |
Correct |
8 ms |
35160 KB |
Output is correct |
26 |
Correct |
9 ms |
35416 KB |
Output is correct |
27 |
Correct |
8 ms |
35164 KB |
Output is correct |
28 |
Correct |
15 ms |
44592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
35164 KB |
Output is correct |
2 |
Correct |
8 ms |
35164 KB |
Output is correct |
3 |
Correct |
8 ms |
35164 KB |
Output is correct |
4 |
Correct |
8 ms |
35164 KB |
Output is correct |
5 |
Correct |
8 ms |
35164 KB |
Output is correct |
6 |
Correct |
8 ms |
35164 KB |
Output is correct |
7 |
Correct |
8 ms |
35160 KB |
Output is correct |
8 |
Correct |
8 ms |
35164 KB |
Output is correct |
9 |
Correct |
8 ms |
35420 KB |
Output is correct |
10 |
Correct |
9 ms |
35420 KB |
Output is correct |
11 |
Correct |
8 ms |
35420 KB |
Output is correct |
12 |
Correct |
9 ms |
35420 KB |
Output is correct |
13 |
Correct |
8 ms |
35416 KB |
Output is correct |
14 |
Correct |
17 ms |
44124 KB |
Output is correct |
15 |
Correct |
18 ms |
44120 KB |
Output is correct |
16 |
Correct |
19 ms |
44380 KB |
Output is correct |
17 |
Correct |
19 ms |
44376 KB |
Output is correct |
18 |
Correct |
21 ms |
45132 KB |
Output is correct |
19 |
Correct |
24 ms |
45916 KB |
Output is correct |
20 |
Correct |
22 ms |
45912 KB |
Output is correct |
21 |
Correct |
18 ms |
44120 KB |
Output is correct |
22 |
Correct |
17 ms |
44124 KB |
Output is correct |
23 |
Correct |
19 ms |
44376 KB |
Output is correct |
24 |
Correct |
343 ms |
141120 KB |
Output is correct |
25 |
Correct |
582 ms |
176464 KB |
Output is correct |
26 |
Correct |
469 ms |
175700 KB |
Output is correct |
27 |
Correct |
468 ms |
175860 KB |
Output is correct |
28 |
Correct |
259 ms |
128684 KB |
Output is correct |
29 |
Correct |
279 ms |
130116 KB |
Output is correct |
30 |
Correct |
305 ms |
131536 KB |
Output is correct |
31 |
Correct |
21 ms |
45776 KB |
Output is correct |
32 |
Correct |
457 ms |
175596 KB |
Output is correct |
33 |
Correct |
8 ms |
35164 KB |
Output is correct |
34 |
Correct |
8 ms |
35420 KB |
Output is correct |
35 |
Correct |
360 ms |
148292 KB |
Output is correct |
36 |
Correct |
8 ms |
35164 KB |
Output is correct |
37 |
Correct |
15 ms |
44380 KB |
Output is correct |
38 |
Correct |
212 ms |
139004 KB |
Output is correct |
39 |
Correct |
243 ms |
120784 KB |
Output is correct |