#include <bits/stdc++.h>
using namespace std;
#define sz(a) (int)a.size()
const int N = (int)1e3+10;
using ar = array<int,3>;
int X[] = {1,-1,0,0};
int Y[] = {0,0,1,-1};
string a[N];
bool vis[N][N];
int n, m, stX, stY, enX, enY;
pair<int,int> dr[4][N][N];
int D[N][N], sh[N][N];
priority_queue<ar,vector<ar>,greater<ar>> pq;
bool valid(int i, int j){
return i>=0 and j>=0 and i<n and j<m and a[i][j]!='#';
}
void dijkstra(int sti, int stj){
memset(D,63,sizeof(D));
pq.push({0,sti,stj}); D[sti][stj] = 0;
while(!pq.empty()){
auto [w,i,j] = pq.top(); pq.pop();
if(vis[i][j]) continue; vis[i][j]=1;
for(int k = 0; k < 4; k++){
int ni = i+X[k], nj = j+Y[k];
if(valid(ni,nj) and D[ni][nj]>D[i][j]+1)
D[ni][nj] = D[i][j]+1, pq.push({D[ni][nj],ni,nj});
}
for(int k = 0; k < 4; k++){
auto [ni,nj] = dr[k][i][j];
if(valid(ni,nj) and D[ni][nj]>D[i][j]+sh[i][j])
D[ni][nj] = D[i][j]+sh[i][j], pq.push({D[ni][nj],ni,nj});
}
}
}
int32_t main(){
cin >> n >> m; memset(sh,63,sizeof(sh));
for(int i = 0; i < n; i++){
cin >> a[i];
for(int j = 0; j < m; j++){
if(a[i][j]=='C') enX=i,enY=j;
else if(a[i][j]=='S') stX=i,stY=j;
}
int last = 0;
for(int j = 0; j < m; j++){
if(a[i][j]=='#') last = j+1;
else{
dr[0][i][j]={i,last};
sh[i][j]=min(sh[i][j],abs(last-j)+1);
}
}
last = m-1;
for(int j = m-1; j >= 0; j--){
if(a[i][j]=='#') last = j-1;
else{
dr[1][i][j]={i,last};
sh[i][j]=min(sh[i][j],abs(last-j)+1);
}
}
}
for(int j = 0; j < m; j++){
int last = 0;
for(int i = 0; i < n; i++){
if(a[i][j]=='#') last = i+1;
else{
dr[2][i][j]={last,j};
sh[i][j]=min(sh[i][j],abs(last-i)+1);
}
}
last = n-1;
for(int i = n-1; i >= 0; i--){
if(a[i][j]=='#') last = i-1;
else{
dr[3][i][j]={last,j};
sh[i][j]=min(sh[i][j],abs(last-i)+1);
}
}
}
dijkstra(stX,stY); cout << D[enX][enY];
}
Compilation message
portals.cpp: In function 'void dijkstra(int, int)':
portals.cpp:25:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
25 | if(vis[i][j]) continue; vis[i][j]=1;
| ^~
portals.cpp:25:27: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
25 | if(vis[i][j]) continue; vis[i][j]=1;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8276 KB |
Output is correct |
2 |
Correct |
5 ms |
8404 KB |
Output is correct |
3 |
Correct |
5 ms |
8404 KB |
Output is correct |
4 |
Correct |
4 ms |
8404 KB |
Output is correct |
5 |
Correct |
4 ms |
8404 KB |
Output is correct |
6 |
Correct |
4 ms |
8484 KB |
Output is correct |
7 |
Correct |
5 ms |
8388 KB |
Output is correct |
8 |
Correct |
5 ms |
8404 KB |
Output is correct |
9 |
Correct |
4 ms |
8308 KB |
Output is correct |
10 |
Correct |
4 ms |
8276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8276 KB |
Output is correct |
2 |
Correct |
4 ms |
8404 KB |
Output is correct |
3 |
Correct |
5 ms |
8424 KB |
Output is correct |
4 |
Correct |
5 ms |
8404 KB |
Output is correct |
5 |
Correct |
6 ms |
8404 KB |
Output is correct |
6 |
Correct |
5 ms |
8404 KB |
Output is correct |
7 |
Correct |
4 ms |
8404 KB |
Output is correct |
8 |
Correct |
5 ms |
8404 KB |
Output is correct |
9 |
Correct |
5 ms |
9172 KB |
Output is correct |
10 |
Correct |
6 ms |
9172 KB |
Output is correct |
11 |
Correct |
6 ms |
9252 KB |
Output is correct |
12 |
Correct |
5 ms |
9220 KB |
Output is correct |
13 |
Correct |
6 ms |
9172 KB |
Output is correct |
14 |
Correct |
5 ms |
8404 KB |
Output is correct |
15 |
Correct |
5 ms |
9172 KB |
Output is correct |
16 |
Correct |
4 ms |
8404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8276 KB |
Output is correct |
2 |
Correct |
4 ms |
8388 KB |
Output is correct |
3 |
Correct |
4 ms |
8404 KB |
Output is correct |
4 |
Correct |
5 ms |
8404 KB |
Output is correct |
5 |
Correct |
12 ms |
12928 KB |
Output is correct |
6 |
Correct |
13 ms |
13036 KB |
Output is correct |
7 |
Correct |
15 ms |
13012 KB |
Output is correct |
8 |
Correct |
12 ms |
13032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8276 KB |
Output is correct |
2 |
Correct |
5 ms |
8464 KB |
Output is correct |
3 |
Correct |
4 ms |
8404 KB |
Output is correct |
4 |
Correct |
4 ms |
8404 KB |
Output is correct |
5 |
Correct |
6 ms |
8504 KB |
Output is correct |
6 |
Correct |
5 ms |
8404 KB |
Output is correct |
7 |
Correct |
4 ms |
8404 KB |
Output is correct |
8 |
Correct |
5 ms |
8404 KB |
Output is correct |
9 |
Correct |
5 ms |
9172 KB |
Output is correct |
10 |
Correct |
5 ms |
9172 KB |
Output is correct |
11 |
Correct |
5 ms |
9172 KB |
Output is correct |
12 |
Correct |
4 ms |
9172 KB |
Output is correct |
13 |
Correct |
6 ms |
9164 KB |
Output is correct |
14 |
Correct |
12 ms |
13012 KB |
Output is correct |
15 |
Correct |
13 ms |
13012 KB |
Output is correct |
16 |
Correct |
13 ms |
13012 KB |
Output is correct |
17 |
Correct |
13 ms |
13012 KB |
Output is correct |
18 |
Correct |
17 ms |
13072 KB |
Output is correct |
19 |
Correct |
15 ms |
12972 KB |
Output is correct |
20 |
Correct |
14 ms |
13012 KB |
Output is correct |
21 |
Correct |
11 ms |
13012 KB |
Output is correct |
22 |
Correct |
16 ms |
13040 KB |
Output is correct |
23 |
Correct |
12 ms |
13012 KB |
Output is correct |
24 |
Correct |
18 ms |
13012 KB |
Output is correct |
25 |
Correct |
3 ms |
8404 KB |
Output is correct |
26 |
Correct |
4 ms |
9172 KB |
Output is correct |
27 |
Correct |
5 ms |
8404 KB |
Output is correct |
28 |
Correct |
9 ms |
12956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8276 KB |
Output is correct |
2 |
Correct |
4 ms |
8404 KB |
Output is correct |
3 |
Correct |
4 ms |
8384 KB |
Output is correct |
4 |
Correct |
4 ms |
8404 KB |
Output is correct |
5 |
Correct |
4 ms |
8404 KB |
Output is correct |
6 |
Correct |
5 ms |
8404 KB |
Output is correct |
7 |
Correct |
4 ms |
8404 KB |
Output is correct |
8 |
Correct |
4 ms |
8404 KB |
Output is correct |
9 |
Correct |
5 ms |
9172 KB |
Output is correct |
10 |
Correct |
5 ms |
9172 KB |
Output is correct |
11 |
Correct |
4 ms |
9172 KB |
Output is correct |
12 |
Correct |
4 ms |
9172 KB |
Output is correct |
13 |
Correct |
5 ms |
9172 KB |
Output is correct |
14 |
Correct |
11 ms |
12936 KB |
Output is correct |
15 |
Correct |
13 ms |
13012 KB |
Output is correct |
16 |
Correct |
13 ms |
13012 KB |
Output is correct |
17 |
Correct |
12 ms |
13012 KB |
Output is correct |
18 |
Correct |
13 ms |
13072 KB |
Output is correct |
19 |
Correct |
14 ms |
13012 KB |
Output is correct |
20 |
Correct |
15 ms |
13044 KB |
Output is correct |
21 |
Correct |
10 ms |
13012 KB |
Output is correct |
22 |
Correct |
10 ms |
13012 KB |
Output is correct |
23 |
Correct |
11 ms |
12924 KB |
Output is correct |
24 |
Correct |
215 ms |
42972 KB |
Output is correct |
25 |
Correct |
338 ms |
43092 KB |
Output is correct |
26 |
Correct |
297 ms |
42968 KB |
Output is correct |
27 |
Correct |
260 ms |
43004 KB |
Output is correct |
28 |
Correct |
152 ms |
42776 KB |
Output is correct |
29 |
Correct |
167 ms |
42908 KB |
Output is correct |
30 |
Correct |
206 ms |
42868 KB |
Output is correct |
31 |
Correct |
13 ms |
13012 KB |
Output is correct |
32 |
Correct |
273 ms |
42864 KB |
Output is correct |
33 |
Correct |
4 ms |
8404 KB |
Output is correct |
34 |
Correct |
4 ms |
9172 KB |
Output is correct |
35 |
Correct |
197 ms |
42816 KB |
Output is correct |
36 |
Correct |
4 ms |
8404 KB |
Output is correct |
37 |
Correct |
8 ms |
13012 KB |
Output is correct |
38 |
Correct |
115 ms |
42704 KB |
Output is correct |
39 |
Correct |
134 ms |
35004 KB |
Output is correct |