Submission #743005

# Submission time Handle Problem Language Result Execution time Memory
743005 2023-05-17T07:21:08 Z Dan4Life Portals (BOI14_portals) C++17
100 / 100
377 ms 43028 KB
#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++){
			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});
			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});
		}
	}
}

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 3 ms 8404 KB Output is correct
3 Correct 4 ms 8404 KB Output is correct
4 Correct 3 ms 8404 KB Output is correct
5 Correct 3 ms 8404 KB Output is correct
6 Correct 3 ms 8400 KB Output is correct
7 Correct 4 ms 8404 KB Output is correct
8 Correct 4 ms 8404 KB Output is correct
9 Correct 4 ms 8404 KB Output is correct
10 Correct 3 ms 8276 KB Output is correct
# Verdict Execution time Memory 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 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 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 4 ms 9172 KB Output is correct
11 Correct 4 ms 9172 KB Output is correct
12 Correct 4 ms 9240 KB Output is correct
13 Correct 4 ms 9172 KB Output is correct
14 Correct 4 ms 8404 KB Output is correct
15 Correct 5 ms 9172 KB Output is correct
16 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 4 ms 8404 KB Output is correct
4 Correct 4 ms 8404 KB Output is correct
5 Correct 12 ms 13028 KB Output is correct
6 Correct 12 ms 12932 KB Output is correct
7 Correct 12 ms 13012 KB Output is correct
8 Correct 8 ms 13012 KB Output is correct
# Verdict Execution time Memory 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 4 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 4 ms 9172 KB Output is correct
10 Correct 4 ms 9208 KB Output is correct
11 Correct 5 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 10 ms 13012 KB Output is correct
15 Correct 12 ms 13012 KB Output is correct
16 Correct 14 ms 13012 KB Output is correct
17 Correct 11 ms 12916 KB Output is correct
18 Correct 15 ms 13012 KB Output is correct
19 Correct 14 ms 13076 KB Output is correct
20 Correct 13 ms 13012 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 13 ms 13032 KB Output is correct
25 Correct 4 ms 8404 KB Output is correct
26 Correct 4 ms 9172 KB Output is correct
27 Correct 4 ms 8404 KB Output is correct
28 Correct 8 ms 12980 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 8404 KB Output is correct
4 Correct 4 ms 8404 KB Output is correct
5 Correct 3 ms 8428 KB Output is correct
6 Correct 4 ms 8404 KB Output is correct
7 Correct 3 ms 8404 KB Output is correct
8 Correct 3 ms 8404 KB Output is correct
9 Correct 4 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 9224 KB Output is correct
13 Correct 4 ms 9172 KB Output is correct
14 Correct 9 ms 13012 KB Output is correct
15 Correct 10 ms 13012 KB Output is correct
16 Correct 15 ms 13012 KB Output is correct
17 Correct 11 ms 13000 KB Output is correct
18 Correct 12 ms 13032 KB Output is correct
19 Correct 19 ms 13076 KB Output is correct
20 Correct 18 ms 13196 KB Output is correct
21 Correct 11 ms 12920 KB Output is correct
22 Correct 11 ms 13028 KB Output is correct
23 Correct 12 ms 13016 KB Output is correct
24 Correct 212 ms 42960 KB Output is correct
25 Correct 377 ms 43028 KB Output is correct
26 Correct 266 ms 42968 KB Output is correct
27 Correct 259 ms 43004 KB Output is correct
28 Correct 154 ms 42824 KB Output is correct
29 Correct 170 ms 42828 KB Output is correct
30 Correct 205 ms 42844 KB Output is correct
31 Correct 13 ms 13012 KB Output is correct
32 Correct 258 ms 42764 KB Output is correct
33 Correct 4 ms 8404 KB Output is correct
34 Correct 4 ms 9172 KB Output is correct
35 Correct 198 ms 42856 KB Output is correct
36 Correct 4 ms 8276 KB Output is correct
37 Correct 8 ms 12920 KB Output is correct
38 Correct 108 ms 42832 KB Output is correct
39 Correct 139 ms 35004 KB Output is correct