#include <bits/stdc++.h>
using namespace std;
#define sz(a) (int)a.size()
const int mxN = (int)1e3+10;
using ar = array<int,3>;
int X[] = {1,-1,0,0};
int Y[] = {0,0,1,-1};
string a[mxN];
bool vis[mxN][mxN];
int n, m, stX, stY, enX, enY;
pair<int,int> dir[4][mxN][mxN];
int dis[mxN][mxN], sh[mxN][mxN];
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(dis,63,sizeof(dis));
pq.push({0,sti,stj}); dis[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 dis[ni][nj]>dis[i][j]+1)
dis[ni][nj] = dis[i][j]+1, pq.push({dis[ni][nj],ni,nj});
}
for(int k = 0; k < 4; k++){
auto [ni,nj] = dir[k][i][j];
if(valid(ni,nj) and dis[ni][nj]>dis[i][j]+sh[i][j])
dis[ni][nj] = dis[i][j]+sh[i][j], pq.push({dis[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;
}
}
for(int i = 0; i < n; i++){
int last = 0;
for(int j = 0; j < m; j++){
if(a[i][j]=='#') last = j+1;
else{
dir[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{
dir[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{
dir[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{
dir[3][i][j]={last,j};
sh[i][j]=min(sh[i][j],abs(last-i)+1);
}
}
}
dijkstra(stX,stY); cout << dis[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;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8276 KB |
Output is correct |
2 |
Correct |
4 ms |
8404 KB |
Output is correct |
3 |
Correct |
4 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 |
3 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 |
4 ms |
8404 KB |
Output is correct |
10 |
Correct |
4 ms |
8404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8276 KB |
Output is correct |
2 |
Correct |
3 ms |
8404 KB |
Output is correct |
3 |
Correct |
4 ms |
8404 KB |
Output is correct |
4 |
Correct |
4 ms |
8388 KB |
Output is correct |
5 |
Correct |
4 ms |
8404 KB |
Output is correct |
6 |
Correct |
3 ms |
8404 KB |
Output is correct |
7 |
Correct |
4 ms |
8408 KB |
Output is correct |
8 |
Correct |
4 ms |
8404 KB |
Output is correct |
9 |
Correct |
4 ms |
9172 KB |
Output is correct |
10 |
Correct |
4 ms |
9172 KB |
Output is correct |
11 |
Correct |
4 ms |
9172 KB |
Output is correct |
12 |
Correct |
5 ms |
9200 KB |
Output is correct |
13 |
Correct |
4 ms |
9172 KB |
Output is correct |
14 |
Correct |
5 ms |
8404 KB |
Output is correct |
15 |
Correct |
4 ms |
9172 KB |
Output is correct |
16 |
Correct |
3 ms |
8404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8296 KB |
Output is correct |
2 |
Correct |
3 ms |
8404 KB |
Output is correct |
3 |
Correct |
3 ms |
8404 KB |
Output is correct |
4 |
Correct |
4 ms |
8404 KB |
Output is correct |
5 |
Correct |
10 ms |
13012 KB |
Output is correct |
6 |
Correct |
11 ms |
13012 KB |
Output is correct |
7 |
Correct |
12 ms |
13028 KB |
Output is correct |
8 |
Correct |
8 ms |
13012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8276 KB |
Output is correct |
2 |
Correct |
3 ms |
8404 KB |
Output is correct |
3 |
Correct |
4 ms |
8504 KB |
Output is correct |
4 |
Correct |
3 ms |
8404 KB |
Output is correct |
5 |
Correct |
4 ms |
8404 KB |
Output is correct |
6 |
Correct |
4 ms |
8404 KB |
Output is correct |
7 |
Correct |
3 ms |
8488 KB |
Output is correct |
8 |
Correct |
4 ms |
8404 KB |
Output is correct |
9 |
Correct |
4 ms |
9172 KB |
Output is correct |
10 |
Correct |
4 ms |
9172 KB |
Output is correct |
11 |
Correct |
4 ms |
9188 KB |
Output is correct |
12 |
Correct |
4 ms |
9172 KB |
Output is correct |
13 |
Correct |
4 ms |
9172 KB |
Output is correct |
14 |
Correct |
10 ms |
13012 KB |
Output is correct |
15 |
Correct |
11 ms |
13012 KB |
Output is correct |
16 |
Correct |
12 ms |
13012 KB |
Output is correct |
17 |
Correct |
11 ms |
13012 KB |
Output is correct |
18 |
Correct |
12 ms |
12992 KB |
Output is correct |
19 |
Correct |
14 ms |
13012 KB |
Output is correct |
20 |
Correct |
12 ms |
13012 KB |
Output is correct |
21 |
Correct |
10 ms |
13008 KB |
Output is correct |
22 |
Correct |
11 ms |
13012 KB |
Output is correct |
23 |
Correct |
11 ms |
13012 KB |
Output is correct |
24 |
Correct |
12 ms |
12996 KB |
Output is correct |
25 |
Correct |
4 ms |
8404 KB |
Output is correct |
26 |
Correct |
5 ms |
9224 KB |
Output is correct |
27 |
Correct |
4 ms |
8328 KB |
Output is correct |
28 |
Correct |
8 ms |
12936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8276 KB |
Output is correct |
2 |
Correct |
5 ms |
8404 KB |
Output is correct |
3 |
Correct |
4 ms |
8404 KB |
Output is correct |
4 |
Correct |
4 ms |
8404 KB |
Output is correct |
5 |
Correct |
3 ms |
8404 KB |
Output is correct |
6 |
Correct |
3 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 |
9176 KB |
Output is correct |
10 |
Correct |
5 ms |
9256 KB |
Output is correct |
11 |
Correct |
4 ms |
9172 KB |
Output is correct |
12 |
Correct |
6 ms |
9220 KB |
Output is correct |
13 |
Correct |
4 ms |
9172 KB |
Output is correct |
14 |
Correct |
10 ms |
13020 KB |
Output is correct |
15 |
Correct |
12 ms |
13040 KB |
Output is correct |
16 |
Correct |
12 ms |
13012 KB |
Output is correct |
17 |
Correct |
11 ms |
12960 KB |
Output is correct |
18 |
Correct |
12 ms |
12948 KB |
Output is correct |
19 |
Correct |
13 ms |
13072 KB |
Output is correct |
20 |
Correct |
13 ms |
13076 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 |
13012 KB |
Output is correct |
24 |
Correct |
195 ms |
42968 KB |
Output is correct |
25 |
Correct |
355 ms |
43056 KB |
Output is correct |
26 |
Correct |
262 ms |
42976 KB |
Output is correct |
27 |
Correct |
296 ms |
42884 KB |
Output is correct |
28 |
Correct |
146 ms |
42800 KB |
Output is correct |
29 |
Correct |
165 ms |
42832 KB |
Output is correct |
30 |
Correct |
195 ms |
42764 KB |
Output is correct |
31 |
Correct |
13 ms |
13012 KB |
Output is correct |
32 |
Correct |
259 ms |
43052 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 |
42904 KB |
Output is correct |
36 |
Correct |
4 ms |
8404 KB |
Output is correct |
37 |
Correct |
8 ms |
13008 KB |
Output is correct |
38 |
Correct |
99 ms |
42780 KB |
Output is correct |
39 |
Correct |
133 ms |
34984 KB |
Output is correct |