Submission #943229

# Submission time Handle Problem Language Result Execution time Memory
943229 2024-03-11T09:29:43 Z siewjh Portals (BOI14_portals) C++17
100 / 100
547 ms 175120 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXR = 1005;
const int MAXN = 1000005;
const int inf = 1e9;
char grid[MAXR][MAXR];
int dtw[MAXR][MAXR], dist[MAXN];
vector<pair<int, int>> adj[MAXN];
int rows, cols, rmove[] = {1, -1, 0, 0}, cmove[] = {0, 0, 1, -1};
int gind(int r, int c){
	return (r - 1) * cols + (c - 1);
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> rows >> cols;
	int st, en;
	for (int i = 0; i <= rows + 1; i++)
		for (int j = 0; j <= cols + 1; j++)
			grid[i][j] = '#';
	for (int i = 1; i <= rows; i++)
		for (int j = 1; j <= cols; j++){
			char ch; cin >> ch;
			if (ch == '#') grid[i][j] = '#';
			else{
				grid[i][j] = '.';
				if (ch == 'S') st = gind(i, j);
				else if (ch == 'C') en = gind(i, j);
			}
		}
	queue<pair<int, int>> q;
	memset(dtw, -1, sizeof(dtw));
	for (int r = 1; r <= rows; r++)
		for (int c = 1; c <= cols; c++){
			if (grid[r][c] == '#') continue;
			for (int k = 0; k < 4; k++){
				int nr = r + rmove[k], nc = c + cmove[k];
				if (grid[nr][nc] == '#'){
					dtw[r][c] = 1;
					q.push({r, c});
					break;
				}
			}
		}
	while (!q.empty()){
		int r, c; tie(r, c) = q.front(); q.pop();
		for (int k = 0; k < 4; k++){
			int nr = r + rmove[k], nc = c + cmove[k];
			if (grid[nr][nc] != '#' && dtw[nr][nc] == -1){
				dtw[nr][nc] = dtw[r][c] + 1;
				q.push({nr, nc});
			}
		}
	}
	for (int r = 1; r <= rows; r++)
		for (int c = 1; c <= cols; c++){
			if (grid[r][c] == '#') continue;
			for (int k = 0; k < 4; k++){
				int nr = r + rmove[k], nc = c + cmove[k];
				if (grid[nr][nc] == '.'){
					adj[gind(r, c)].push_back({gind(nr, nc), 1});
					adj[gind(nr, nc)].push_back({gind(r, c), 1});
				}
			}
		}
	for (int r = 1; r <= rows; r++){
		int pw = 0;
		for (int c = 1; c <= cols; c++){
			if (grid[r][c] == '#') pw = c;
			else adj[gind(r, c)].push_back({gind(r, pw + 1), dtw[r][c]});
		}
		pw = cols + 1;
		for (int c = cols; c >= 1; c--){
			if (grid[r][c] == '#') pw = c;
			else adj[gind(r, c)].push_back({gind(r, pw - 1), dtw[r][c]});
		}
	}
	for (int c = 1; c <= cols; c++){
		int pw = 0;
		for (int r = 1; r <= rows; r++){
			if (grid[r][c] == '#') pw = r;
			else adj[gind(r, c)].push_back({gind(pw + 1, c), dtw[r][c]});
		}
		pw = rows + 1;
		for (int r = rows; r >= 1; r--){
			if (grid[r][c] == '#') pw = r;
			else adj[gind(r, c)].push_back({gind(pw - 1, c), dtw[r][c]});
		}
	}
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
	pq.push({0, st});
	for (int i = 0; i < MAXN; i++) dist[i] = inf;
	dist[st] = 0;
	while (!pq.empty()){
		int x, d; tie(d, x) = pq.top(); pq.pop();
		if (dist[x] != d) continue;
		if (x == en){
			cout << d;
			return 0;
		}
		for (auto [nn, nd] : adj[x]){
			if (dist[nn] > d + nd){
				dist[nn] = d + nd;
				pq.push({dist[nn], nn});
			}
		}
	}
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:93:11: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
   93 |  dist[st] = 0;
      |  ~~~~~~~~~^~~
portals.cpp:97:3: warning: 'en' may be used uninitialized in this function [-Wmaybe-uninitialized]
   97 |   if (x == en){
      |   ^~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 31576 KB Output is correct
2 Correct 8 ms 31580 KB Output is correct
3 Correct 7 ms 31580 KB Output is correct
4 Correct 8 ms 31580 KB Output is correct
5 Correct 7 ms 31580 KB Output is correct
6 Correct 7 ms 31832 KB Output is correct
7 Correct 7 ms 31580 KB Output is correct
8 Correct 7 ms 31836 KB Output is correct
9 Correct 7 ms 31580 KB Output is correct
10 Correct 7 ms 31580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 31580 KB Output is correct
2 Correct 8 ms 31580 KB Output is correct
3 Correct 7 ms 31580 KB Output is correct
4 Correct 8 ms 31576 KB Output is correct
5 Correct 7 ms 31580 KB Output is correct
6 Correct 7 ms 31580 KB Output is correct
7 Correct 7 ms 31580 KB Output is correct
8 Correct 7 ms 31836 KB Output is correct
9 Correct 8 ms 32088 KB Output is correct
10 Correct 9 ms 32092 KB Output is correct
11 Correct 7 ms 31836 KB Output is correct
12 Correct 7 ms 31836 KB Output is correct
13 Correct 8 ms 32092 KB Output is correct
14 Correct 7 ms 31580 KB Output is correct
15 Correct 8 ms 32092 KB Output is correct
16 Correct 8 ms 31580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 31580 KB Output is correct
2 Correct 7 ms 31580 KB Output is correct
3 Correct 7 ms 31580 KB Output is correct
4 Correct 7 ms 31580 KB Output is correct
5 Correct 16 ms 34308 KB Output is correct
6 Correct 18 ms 34396 KB Output is correct
7 Correct 18 ms 34908 KB Output is correct
8 Correct 16 ms 34140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 31580 KB Output is correct
2 Correct 8 ms 31580 KB Output is correct
3 Correct 7 ms 31580 KB Output is correct
4 Correct 7 ms 31580 KB Output is correct
5 Correct 8 ms 31580 KB Output is correct
6 Correct 8 ms 31588 KB Output is correct
7 Correct 7 ms 31580 KB Output is correct
8 Correct 8 ms 31580 KB Output is correct
9 Correct 8 ms 32236 KB Output is correct
10 Correct 8 ms 32092 KB Output is correct
11 Correct 9 ms 31836 KB Output is correct
12 Correct 7 ms 31836 KB Output is correct
13 Correct 7 ms 32092 KB Output is correct
14 Correct 17 ms 34140 KB Output is correct
15 Correct 18 ms 34396 KB Output is correct
16 Correct 18 ms 34908 KB Output is correct
17 Correct 18 ms 35420 KB Output is correct
18 Correct 23 ms 36736 KB Output is correct
19 Correct 18 ms 37464 KB Output is correct
20 Correct 20 ms 37468 KB Output is correct
21 Correct 17 ms 34140 KB Output is correct
22 Correct 16 ms 34396 KB Output is correct
23 Correct 18 ms 34648 KB Output is correct
24 Correct 21 ms 37468 KB Output is correct
25 Correct 7 ms 31580 KB Output is correct
26 Correct 8 ms 32112 KB Output is correct
27 Correct 6 ms 31588 KB Output is correct
28 Correct 13 ms 34232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 31580 KB Output is correct
2 Correct 7 ms 31580 KB Output is correct
3 Correct 7 ms 31580 KB Output is correct
4 Correct 7 ms 31580 KB Output is correct
5 Correct 7 ms 31580 KB Output is correct
6 Correct 7 ms 31580 KB Output is correct
7 Correct 7 ms 31836 KB Output is correct
8 Correct 8 ms 31832 KB Output is correct
9 Correct 9 ms 32340 KB Output is correct
10 Correct 8 ms 32092 KB Output is correct
11 Correct 7 ms 31836 KB Output is correct
12 Correct 7 ms 31876 KB Output is correct
13 Correct 8 ms 32092 KB Output is correct
14 Correct 20 ms 34144 KB Output is correct
15 Correct 21 ms 34400 KB Output is correct
16 Correct 18 ms 35172 KB Output is correct
17 Correct 17 ms 35416 KB Output is correct
18 Correct 22 ms 36440 KB Output is correct
19 Correct 18 ms 37604 KB Output is correct
20 Correct 20 ms 37480 KB Output is correct
21 Correct 17 ms 34180 KB Output is correct
22 Correct 17 ms 34152 KB Output is correct
23 Correct 18 ms 34652 KB Output is correct
24 Correct 443 ms 128028 KB Output is correct
25 Correct 547 ms 175120 KB Output is correct
26 Correct 313 ms 174888 KB Output is correct
27 Correct 417 ms 174928 KB Output is correct
28 Correct 335 ms 90092 KB Output is correct
29 Correct 368 ms 91740 KB Output is correct
30 Correct 354 ms 95572 KB Output is correct
31 Correct 21 ms 37468 KB Output is correct
32 Correct 492 ms 174656 KB Output is correct
33 Correct 7 ms 31580 KB Output is correct
34 Correct 7 ms 32092 KB Output is correct
35 Correct 308 ms 123876 KB Output is correct
36 Correct 7 ms 31576 KB Output is correct
37 Correct 13 ms 34140 KB Output is correct
38 Correct 225 ms 86296 KB Output is correct
39 Correct 193 ms 73020 KB Output is correct