#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast,unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define sz(x) (int)x.size()
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define pb push_back
const ll mod = 1e9+7, maxn = 1e3+2, inf = ll(1e9)+5;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int dist_wall[maxn][maxn];
pp ad[maxn][maxn][4];
pp st, en;
bool wall[maxn][maxn];
int n, m;
void clc_bfs_wall(){
deque<pp> q;
rep(i,0,n+2) rep(j,0,m+2) {
dist_wall[i][j] = inf;
if(wall[i][j]) dist_wall[i][j] = 0, q.pb({i, j});
}
while(sz(q)){
auto[i, j] = q.front(); q.pop_front();
rep(d,0,4){
int x = dx[d] + i, y = dy[d] + j;
if(x > 0 && x <= n && y > 0 && y <= m && !wall[x][y] && dist_wall[x][y] == inf)
dist_wall[x][y] = dist_wall[i][j] + 1, q.pb({x, y});
}
}
}
vector<pair<int, pp>> adj(int i, int j){
vector<pair<int, pp>> res;
rep(d,0,4){
int x = dx[d] + i, y = dy[d] + j;
if(x > 0 && x <= n && y > 0 && y <= m && !wall[x][y]) res.pb({1, {x, y}});
}
rep(d,0,4) if(ad[i][j][d].ff + 1) res.pb({dist_wall[i][j], ad[i][j][d]});
return res;
}
void clc_portal_edge(){
rep(i,0,n+2) rep(j,0,m+2) rep(d,0,4) ad[i][j][d] = {-1, -1};
rep(i,0,n+2) rep(j,0,m+2) {
if(wall[i][j]) rep(d,0,4){
int x = i + dx[d], y = j + dy[d];
int a = x, b = y;
while(x > 0 && x <= n && y > 0 && y <= m && !wall[x][y]) ad[x][y][d] = {a, b}, x += dx[d], y += dy[d];
}
}
}
int dijkstra(){
vector<vector<pp>> vc(n*m);
vc[0].pb(st);
vector<vector<int>> dist(n + 2, vector<int>(m + 2, inf)); dist[st.ff][st.ss] = 0;
rep(i,0,sz(vc)){
for(pp c: vc[i]){
if(dist[c.ff][c.ss] - i) continue;
if(c == en) return i;
for(auto[w, p]: adj(c.ff, c.ss)) if(dist[p.ff][p.ss] > i + w)
vc[dist[p.ff][p.ss] = i + w].pb(p);
}
}
return -1;
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> m;
rep(i,0,n+2) rep(j,0,m+2) wall[i][j] = true;
rep(i,1,n+1) rep(j,1,m+1){
char c; cin >> c;
if(c == '#') continue;
if(c == 'S') st = {i, j};
else if(c == 'C') en = {i, j};
wall[i][j] = false;
}
clc_bfs_wall();
clc_portal_edge();
cout << dijkstra() << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
904 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
0 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
12 ms |
4852 KB |
Output is correct |
6 |
Correct |
10 ms |
4820 KB |
Output is correct |
7 |
Correct |
9 ms |
4692 KB |
Output is correct |
8 |
Correct |
11 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
976 KB |
Output is correct |
11 |
Correct |
2 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
13 ms |
4800 KB |
Output is correct |
15 |
Correct |
11 ms |
4748 KB |
Output is correct |
16 |
Correct |
9 ms |
4692 KB |
Output is correct |
17 |
Correct |
11 ms |
4820 KB |
Output is correct |
18 |
Correct |
10 ms |
4720 KB |
Output is correct |
19 |
Correct |
4 ms |
4564 KB |
Output is correct |
20 |
Correct |
7 ms |
4820 KB |
Output is correct |
21 |
Correct |
11 ms |
4820 KB |
Output is correct |
22 |
Correct |
11 ms |
4700 KB |
Output is correct |
23 |
Correct |
10 ms |
4820 KB |
Output is correct |
24 |
Correct |
10 ms |
4948 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
10 ms |
5292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
11 ms |
4812 KB |
Output is correct |
15 |
Correct |
12 ms |
4756 KB |
Output is correct |
16 |
Correct |
9 ms |
4692 KB |
Output is correct |
17 |
Correct |
10 ms |
4692 KB |
Output is correct |
18 |
Correct |
10 ms |
4820 KB |
Output is correct |
19 |
Correct |
4 ms |
4564 KB |
Output is correct |
20 |
Correct |
7 ms |
4820 KB |
Output is correct |
21 |
Correct |
11 ms |
4844 KB |
Output is correct |
22 |
Correct |
11 ms |
4812 KB |
Output is correct |
23 |
Correct |
10 ms |
4820 KB |
Output is correct |
24 |
Correct |
238 ms |
72056 KB |
Output is correct |
25 |
Correct |
362 ms |
72912 KB |
Output is correct |
26 |
Correct |
89 ms |
66064 KB |
Output is correct |
27 |
Correct |
178 ms |
70276 KB |
Output is correct |
28 |
Correct |
268 ms |
72704 KB |
Output is correct |
29 |
Correct |
258 ms |
71820 KB |
Output is correct |
30 |
Correct |
224 ms |
70412 KB |
Output is correct |
31 |
Correct |
11 ms |
4948 KB |
Output is correct |
32 |
Correct |
296 ms |
75120 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
980 KB |
Output is correct |
35 |
Correct |
175 ms |
70052 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
10 ms |
5332 KB |
Output is correct |
38 |
Correct |
240 ms |
85848 KB |
Output is correct |
39 |
Correct |
143 ms |
68468 KB |
Output is correct |