Submission #943265

# Submission time Handle Problem Language Result Execution time Memory
943265 2024-03-11T09:45:55 Z hmm789 Portals (BOI14_portals) C++14
100 / 100
798 ms 240556 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define INF 1000000000000000000

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int r, c, sx, sy, ex, ey;
	cin >> r >> c;
	string a[r+2], str;
	for(int i = 1; i <= r; i++) {
		cin >> str;
		a[i] = '#' + str + '#';
	}
	str = "";
	for(int i = 0; i < c+2; i++) str += '#';
	a[0] = str; a[r+1] = str;
	r += 2; c += 2;
	int dist[r][c];
	memset(dist, -1, sizeof(dist));
	queue<pair<int, int>> q;
	int dx[4] = {-1, 0, 0, 1};
	int dy[4] = {0, -1, 1, 0};
	for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) {
		if(a[i][j] == 'S') sx = i, sy = j;
		if(a[i][j] == 'C') ex = i, ey = j;
		if(a[i][j] == '#') {
			q.push({i, j});
			dist[i][j] = 0;
		}
	}
	while(!q.empty()) {
		pair<int, int> c1 = q.front();
		q.pop();
		for(int i = 0; i < 4; i++) {
			int nx = c1.first+dx[i], ny = c1.second+dy[i];
			if(nx < 0 || nx >= r || ny < 0 || ny >= c) continue;
			if(dist[nx][ny] == -1 || dist[nx][ny] > dist[c1.first][c1.second]+1) {
				dist[nx][ny] = dist[c1.first][c1.second]+1;
				q.push({nx, ny});
			}
		}
	}
	vector<pair<pair<int, int>, int>> adj[r][c];
	bool v[r][c];
	memset(v, 0, sizeof(v));
	for(int i = 0; i < r; i++) {
		for(int j = 0; j < c; j++) if(!v[i][j] && a[i][j] != '#' && a[i-1][j] == '#') {
			int idx = i;
			while(idx < r && a[idx][j] != '#') v[idx++][j] = 1;
			for(int k = i; k < idx; k++) {
				adj[k][j].push_back({{i, j}, dist[k][j]});
				adj[k][j].push_back({{idx-1, j}, dist[k][j]});
			}
		}
	}
	memset(v, 0, sizeof(v));
	for(int i = 0; i < r; i++) {
		for(int j = 0; j < c; j++) if(!v[i][j] && a[i][j] != '#' && a[i][j-1] == '#') {
			int idx = j;
			while(idx < c && a[i][idx] != '#') v[i][idx++] = 1;
			for(int k = j; k < idx; k++) {
				adj[i][k].push_back({{i, j}, dist[i][k]});
				adj[i][k].push_back({{i, idx-1}, dist[i][k]});
			}
		}
	}
	for(int i = 0; i < r; i++) {
		for(int j = 0; j < c; j++) if(a[i][j] != '#') {
			for(int k = 0; k < 4; k++) {
				int nx = i+dx[k], ny = j+dy[k];
				if(nx < 0 || nx >= r || ny < 0 || ny >= c) continue;
				if(a[nx][ny] == '#') continue;
				adj[i][j].push_back({{nx, ny}, 1});
			}
		}
	}
	memset(dist, -1, sizeof(dist));
	dist[sx][sy] = 0;
	priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
	pq.push({0, {sx, sy}});
	while(!pq.empty()) {
		pair<int, pair<int, int>> c = pq.top();
		pq.pop();
		if(dist[c.second.first][c.second.second] != c.first) continue;
		for(auto i : adj[c.second.first][c.second.second]) {
			int nx = i.first.first, ny = i.first.second;
			if(dist[nx][ny] == -1 || dist[nx][ny] > dist[c.second.first][c.second.second]+i.second) {
				dist[nx][ny] = dist[c.second.first][c.second.second]+i.second;
				pq.push({dist[nx][ny], {nx, ny}});
			}
		}
	}
	cout << dist[ex][ey];
}

Compilation message

portals.cpp: In function 'int32_t main()':
portals.cpp:80:15: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
   80 |  dist[sx][sy] = 0;
      |  ~~~~~~~~~~~~~^~~
portals.cpp:80:15: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
portals.cpp:95:21: warning: 'ex' may be used uninitialized in this function [-Wmaybe-uninitialized]
   95 |  cout << dist[ex][ey];
      |                     ^
portals.cpp:95:21: warning: 'ey' may be used uninitialized in this function [-Wmaybe-uninitialized]
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 2 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 12 ms 6492 KB Output is correct
6 Correct 16 ms 6744 KB Output is correct
7 Correct 15 ms 7256 KB Output is correct
8 Correct 9 ms 7260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 2 ms 860 KB Output is correct
11 Correct 1 ms 856 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 13 ms 6496 KB Output is correct
15 Correct 15 ms 6748 KB Output is correct
16 Correct 16 ms 7260 KB Output is correct
17 Correct 13 ms 7172 KB Output is correct
18 Correct 16 ms 8284 KB Output is correct
19 Correct 26 ms 10076 KB Output is correct
20 Correct 22 ms 10076 KB Output is correct
21 Correct 13 ms 6744 KB Output is correct
22 Correct 12 ms 6768 KB Output is correct
23 Correct 16 ms 7004 KB Output is correct
24 Correct 19 ms 9820 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 9 ms 7256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 2 ms 856 KB Output is correct
10 Correct 2 ms 856 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 14 ms 6492 KB Output is correct
15 Correct 15 ms 6824 KB Output is correct
16 Correct 16 ms 7208 KB Output is correct
17 Correct 14 ms 7252 KB Output is correct
18 Correct 16 ms 8416 KB Output is correct
19 Correct 26 ms 10116 KB Output is correct
20 Correct 22 ms 9988 KB Output is correct
21 Correct 13 ms 6760 KB Output is correct
22 Correct 13 ms 6744 KB Output is correct
23 Correct 16 ms 7016 KB Output is correct
24 Correct 443 ms 178880 KB Output is correct
25 Correct 798 ms 240556 KB Output is correct
26 Correct 699 ms 240268 KB Output is correct
27 Correct 688 ms 240224 KB Output is correct
28 Correct 372 ms 155804 KB Output is correct
29 Correct 403 ms 158756 KB Output is correct
30 Correct 447 ms 161516 KB Output is correct
31 Correct 23 ms 10072 KB Output is correct
32 Correct 750 ms 239532 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 920 KB Output is correct
35 Correct 543 ms 188332 KB Output is correct
36 Correct 0 ms 344 KB Output is correct
37 Correct 10 ms 7260 KB Output is correct
38 Correct 275 ms 171020 KB Output is correct
39 Correct 274 ms 136856 KB Output is correct