#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int di[] = {-1, 1, 0, 0};
int dj[] = {0, 0, -1, 1};
const int N = 1000;
int dist[N+5][N+5];
int wall[N+5][N+5];
int dole[N+5][N+5], gore[N+5][N+5], levo[N+5][N+5], desno[N+5][N+5];
int mat[N+5][N+5];
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n, m;
cin >> n >> m;
int si, sj, ti, tj;
for(int i=1; i<=n; i++){
string s;
cin >> s;
for(int j=1; j<=m; j++){
if(s[j-1] != '#') mat[i][j] = 1;
if(s[j-1] == 'S') si = i, sj = j;
else if(s[j-1] == 'C') ti = i, tj = j;
}
}
queue <pair <int, int>> zid;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
bool pored = 0;
for(int d=0; d<4; d++){
int ci = i + di[d];
int cj = j + dj[d];
if(!mat[ci][cj]) pored = 1;
}
if(pored){
wall[i][j] = 1;
zid.push({i, j});
}
else wall[i][j] = N*N+5;
}
}
while(!zid.empty()){
int vi, vj;
tie(vi, vj) = zid.front();
zid.pop();
for(int d=0; d<4; d++){
int ci = vi + di[d];
int cj = vj + dj[d];
if(mat[ci][cj] && wall[ci][cj] > wall[vi][vj] + 1){
wall[ci][cj] = wall[vi][vj] + 1;
zid.push({ci, cj});
}
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(mat[i][j-1]) levo[i][j] = levo[i][j-1];
else levo[i][j] = j;
}
for(int j=m; j>=1; j--){
if(mat[i][j+1]) desno[i][j] = desno[i][j+1];
else desno[i][j] = j;
}
}
for(int j=1; j<=m; j++){
for(int i=1; i<=n; i++){
if(mat[i-1][j]) gore[i][j] = gore[i-1][j];
else gore[i][j] = i;
}
for(int i=n; i>=1; i--){
if(mat[i+1][j]) dole[i][j] = dole[i+1][j];
else dole[i][j] = i;
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(mat[i][j]) dist[i][j] = N*N+5;
}
}
using T = tuple <int, int, int>;
priority_queue <T, vector <T>, greater <T>> q;
dist[si][sj] = 0;
q.push(make_tuple(0, si, sj));
while(!q.empty()){
int ds, vi, vj;
tie(ds, vi, vj) = q.top();
q.pop();
if(ds != dist[vi][vj]) continue;
for(int d=0; d<4; d++){
int ci = vi + di[d];
int cj = vj + dj[d];
if(dist[ci][cj] > dist[vi][vj] + 1){
dist[ci][cj] = dist[vi][vj] + 1;
q.push(make_tuple(dist[ci][cj], ci, cj));
}
}
int k = levo[vi][vj];
if(dist[vi][k] > dist[vi][vj] + wall[vi][vj]){
dist[vi][k] = dist[vi][vj] + wall[vi][vj];
q.push(make_tuple(dist[vi][k], vi, k));
}
k = desno[vi][vj];
if(dist[vi][k] > dist[vi][vj] + wall[vi][vj]){
dist[vi][k] = dist[vi][vj] + wall[vi][vj];
q.push(make_tuple(dist[vi][k], vi, k));
}
k = gore[vi][vj];
if(dist[k][vj] > dist[vi][vj] + wall[vi][vj]){
dist[k][vj] = dist[vi][vj] + wall[vi][vj];
q.push(make_tuple(dist[k][vj], k, vj));
}
k = dole[vi][vj];
if(dist[k][vj] > dist[vi][vj] + wall[vi][vj]){
dist[k][vj] = dist[vi][vj] + wall[vi][vj];
q.push(make_tuple(dist[k][vj], k, vj));
}
}
cout << dist[ti][tj] << "\n";
return 0;
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:126:29: warning: 'tj' may be used uninitialized in this function [-Wmaybe-uninitialized]
126 | cout << dist[ti][tj] << "\n";
| ^~~~
portals.cpp:126:29: warning: 'ti' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
452 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
580 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
584 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
1732 KB |
Output is correct |
10 |
Correct |
2 ms |
1740 KB |
Output is correct |
11 |
Correct |
1 ms |
1740 KB |
Output is correct |
12 |
Correct |
1 ms |
1740 KB |
Output is correct |
13 |
Correct |
1 ms |
1740 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
1740 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
8 ms |
6220 KB |
Output is correct |
6 |
Correct |
9 ms |
6220 KB |
Output is correct |
7 |
Correct |
10 ms |
6220 KB |
Output is correct |
8 |
Correct |
5 ms |
6092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
580 KB |
Output is correct |
3 |
Correct |
1 ms |
580 KB |
Output is correct |
4 |
Correct |
1 ms |
452 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
1664 KB |
Output is correct |
10 |
Correct |
2 ms |
1740 KB |
Output is correct |
11 |
Correct |
2 ms |
1740 KB |
Output is correct |
12 |
Correct |
1 ms |
1740 KB |
Output is correct |
13 |
Correct |
1 ms |
1740 KB |
Output is correct |
14 |
Correct |
8 ms |
6224 KB |
Output is correct |
15 |
Correct |
9 ms |
6220 KB |
Output is correct |
16 |
Correct |
10 ms |
6220 KB |
Output is correct |
17 |
Correct |
9 ms |
6096 KB |
Output is correct |
18 |
Correct |
11 ms |
6136 KB |
Output is correct |
19 |
Correct |
11 ms |
5956 KB |
Output is correct |
20 |
Correct |
14 ms |
5956 KB |
Output is correct |
21 |
Correct |
8 ms |
6216 KB |
Output is correct |
22 |
Correct |
9 ms |
6168 KB |
Output is correct |
23 |
Correct |
12 ms |
6220 KB |
Output is correct |
24 |
Correct |
10 ms |
5932 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
1740 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
5 ms |
6092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
580 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
580 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
1740 KB |
Output is correct |
10 |
Correct |
2 ms |
1740 KB |
Output is correct |
11 |
Correct |
1 ms |
1728 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1740 KB |
Output is correct |
14 |
Correct |
8 ms |
6092 KB |
Output is correct |
15 |
Correct |
10 ms |
6220 KB |
Output is correct |
16 |
Correct |
9 ms |
6220 KB |
Output is correct |
17 |
Correct |
9 ms |
6176 KB |
Output is correct |
18 |
Correct |
11 ms |
6096 KB |
Output is correct |
19 |
Correct |
12 ms |
5964 KB |
Output is correct |
20 |
Correct |
11 ms |
5836 KB |
Output is correct |
21 |
Correct |
8 ms |
6128 KB |
Output is correct |
22 |
Correct |
10 ms |
6172 KB |
Output is correct |
23 |
Correct |
9 ms |
6212 KB |
Output is correct |
24 |
Correct |
207 ms |
34788 KB |
Output is correct |
25 |
Correct |
359 ms |
29364 KB |
Output is correct |
26 |
Correct |
269 ms |
28952 KB |
Output is correct |
27 |
Correct |
243 ms |
28996 KB |
Output is correct |
28 |
Correct |
163 ms |
36592 KB |
Output is correct |
29 |
Correct |
174 ms |
36284 KB |
Output is correct |
30 |
Correct |
205 ms |
36292 KB |
Output is correct |
31 |
Correct |
10 ms |
5836 KB |
Output is correct |
32 |
Correct |
244 ms |
29128 KB |
Output is correct |
33 |
Correct |
0 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
1740 KB |
Output is correct |
35 |
Correct |
205 ms |
30020 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
5 ms |
6092 KB |
Output is correct |
38 |
Correct |
89 ms |
34280 KB |
Output is correct |
39 |
Correct |
145 ms |
37096 KB |
Output is correct |