Submission #789543

# Submission time Handle Problem Language Result Execution time Memory
789543 2023-07-21T13:25:05 Z bane Portals (BOI14_portals) C++17
100 / 100
248 ms 46476 KB
#include<bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

const int dx[4] = {1,-1,0,0};
const int dy[4] = {0,0,1,-1};

int dist[1005][1005], nearest_wall[1005][1005];
char grid[1005][1005];
pair<int,int>Portals[1005][1005][4];

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);

	int n, m;
	cin >> n >> m;
	n += 2, m += 2;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			if (i == 0 || j == 0 || j == m - 1 || i == n - 1){
				grid[i][j] = '#';
				continue;
			}
			cin >> grid[i][j];
		}
	}
	
	pair<int,int>start = {-1,-1};
	pair<int,int>cake = {-1,-1};
	
	queue<pair<int,int>>q;	
	for (int i = 0; i <= 1000; i++){
		for (int j = 0; j <= 1000; j++){
			nearest_wall[i][j] = (int)1e9;
		}
	}
	
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			if (grid[i][j] == 'S'){
				start = {i,j};
			}
			if (grid[i][j] == 'C'){
				cake = {i,j};
			}
			if (grid[i][j]== '#'){
				nearest_wall[i][j] = 0;
				q.push({i,j});
			}
		}
	}

	while(!q.empty()){
		auto [x,y] = q.front();
		q.pop();
		for (int dir = 0; dir < 4; dir++){
			int nx = x + dx[dir];
			int ny = y + dy[dir];
			if (nx < n && nx >= 0 && ny < m && ny >= 0){
				if (nearest_wall[nx][ny] > nearest_wall[x][y] + 1){
					q.push({nx,ny});
					nearest_wall[nx][ny] = nearest_wall[x][y] + 1;
				}
			}
		}
	}
	for (int j = 0; j < m; j++){
		//go down - > go up with man
		int highest = 0;
		for (int i = 0; i < n; i++){
			if (grid[i][j] == '#'){
				highest = i;
			}
			Portals[i][j][0] = {highest + 1,j};
		}
		highest = n - 1;
		for (int i = n - 1; i>=0; i--){
			if (grid[i][j] == '#'){
				highest = i;
			}
			Portals[i][j][1] = {highest - 1,j};
		}
	}

	for (int i = 0; i < n; i++){
		//go right - > go left with man
		int Lowest = 0;
		for (int j = 0; j < m; j++){
			if (grid[i][j] == '#'){
				Lowest = j;
			}
			Portals[i][j][3] = {i, Lowest + 1};
		}
		Lowest = m - 1;
		for (int j = m - 1; j>=0; j--){
			if (grid[i][j] == '#'){
				Lowest = j;
			}
			Portals[i][j][2] = {i,Lowest - 1};
		}
	}

	priority_queue<pair<int,pair<int,int>>>dijkstra;
	dijkstra.push({0,{start.first,start.second}});

	for (int i = 0; i <= 1000; i++){
		for (int j = 0; j <= 1000; j++){
			dist[i][j] = (int)1e9;
		}
	}
	dist[start.first][start.second] = 0;

	while(!dijkstra.empty()){
		int d = -dijkstra.top().first;
		auto [x,y] = dijkstra.top().second;
		dijkstra.pop();
		if (d != dist[x][y]){
			continue;
		}

		for (int dir = 0; dir < 4; dir++){
			int nx = x + dx[dir];
			int ny = y + dy[dir];
			if (nx < n && nx >= 0 && ny < m && ny >= 0){
				if (grid[nx][ny] != '#'){
					if (dist[nx][ny] > d + 1){
						dijkstra.push({-d-1, {nx,ny}});
						dist[nx][ny] = d + 1;
					}
				}
			}

			//teleport
			nx = Portals[x][y][dir].first;
			ny = Portals[x][y][dir].second;

			if (dist[nx][ny] > nearest_wall[x][y] + d){
				if (grid[nx][ny] != '#'){
					dist[nx][ny] = d + nearest_wall[x][y];
					dijkstra.push({-dist[nx][ny], {nx,ny}});
				}
			}
		}
	}

	cout << dist[cake.first][cake.second] << '\n';

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8148 KB Output is correct
2 Correct 5 ms 8148 KB Output is correct
3 Correct 4 ms 8260 KB Output is correct
4 Correct 4 ms 8136 KB Output is correct
5 Correct 4 ms 8260 KB Output is correct
6 Correct 4 ms 8276 KB Output is correct
7 Correct 4 ms 8276 KB Output is correct
8 Correct 4 ms 8276 KB Output is correct
9 Correct 4 ms 8148 KB Output is correct
10 Correct 4 ms 8136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8256 KB Output is correct
4 Correct 5 ms 8148 KB Output is correct
5 Correct 4 ms 8240 KB Output is correct
6 Correct 4 ms 8276 KB Output is correct
7 Correct 4 ms 8276 KB Output is correct
8 Correct 4 ms 8276 KB Output is correct
9 Correct 4 ms 8532 KB Output is correct
10 Correct 4 ms 8532 KB Output is correct
11 Correct 4 ms 8516 KB Output is correct
12 Correct 4 ms 8532 KB Output is correct
13 Correct 4 ms 8532 KB Output is correct
14 Correct 4 ms 8148 KB Output is correct
15 Correct 4 ms 8516 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8136 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 9 ms 10700 KB Output is correct
6 Correct 9 ms 10708 KB Output is correct
7 Correct 10 ms 10708 KB Output is correct
8 Correct 6 ms 10708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8280 KB Output is correct
2 Correct 4 ms 8204 KB Output is correct
3 Correct 4 ms 8276 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8264 KB Output is correct
6 Correct 4 ms 8176 KB Output is correct
7 Correct 4 ms 8268 KB Output is correct
8 Correct 3 ms 8276 KB Output is correct
9 Correct 4 ms 8532 KB Output is correct
10 Correct 4 ms 8532 KB Output is correct
11 Correct 4 ms 8516 KB Output is correct
12 Correct 4 ms 8532 KB Output is correct
13 Correct 4 ms 8532 KB Output is correct
14 Correct 8 ms 10708 KB Output is correct
15 Correct 9 ms 10656 KB Output is correct
16 Correct 10 ms 10708 KB Output is correct
17 Correct 9 ms 10708 KB Output is correct
18 Correct 11 ms 10712 KB Output is correct
19 Correct 11 ms 10452 KB Output is correct
20 Correct 13 ms 10580 KB Output is correct
21 Correct 8 ms 10708 KB Output is correct
22 Correct 10 ms 10732 KB Output is correct
23 Correct 9 ms 10708 KB Output is correct
24 Correct 10 ms 10452 KB Output is correct
25 Correct 3 ms 8148 KB Output is correct
26 Correct 4 ms 8516 KB Output is correct
27 Correct 4 ms 8148 KB Output is correct
28 Correct 6 ms 10708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8132 KB Output is correct
2 Correct 3 ms 8276 KB Output is correct
3 Correct 3 ms 8148 KB Output is correct
4 Correct 3 ms 8148 KB Output is correct
5 Correct 3 ms 8276 KB Output is correct
6 Correct 4 ms 8276 KB Output is correct
7 Correct 4 ms 8276 KB Output is correct
8 Correct 3 ms 8276 KB Output is correct
9 Correct 5 ms 8532 KB Output is correct
10 Correct 5 ms 8532 KB Output is correct
11 Correct 4 ms 8532 KB Output is correct
12 Correct 4 ms 8520 KB Output is correct
13 Correct 4 ms 8516 KB Output is correct
14 Correct 8 ms 10740 KB Output is correct
15 Correct 9 ms 10708 KB Output is correct
16 Correct 9 ms 10708 KB Output is correct
17 Correct 9 ms 10708 KB Output is correct
18 Correct 11 ms 10708 KB Output is correct
19 Correct 10 ms 10524 KB Output is correct
20 Correct 11 ms 10592 KB Output is correct
21 Correct 8 ms 10704 KB Output is correct
22 Correct 8 ms 10708 KB Output is correct
23 Correct 9 ms 10752 KB Output is correct
24 Correct 170 ms 45372 KB Output is correct
25 Correct 248 ms 42064 KB Output is correct
26 Correct 230 ms 41892 KB Output is correct
27 Correct 211 ms 41932 KB Output is correct
28 Correct 118 ms 45444 KB Output is correct
29 Correct 135 ms 46420 KB Output is correct
30 Correct 156 ms 46476 KB Output is correct
31 Correct 10 ms 10452 KB Output is correct
32 Correct 198 ms 41768 KB Output is correct
33 Correct 4 ms 8148 KB Output is correct
34 Correct 4 ms 8532 KB Output is correct
35 Correct 163 ms 43728 KB Output is correct
36 Correct 3 ms 8148 KB Output is correct
37 Correct 6 ms 10708 KB Output is correct
38 Correct 91 ms 44744 KB Output is correct
39 Correct 119 ms 45832 KB Output is correct