Submission #943268

# Submission time Handle Problem Language Result Execution time Memory
943268 2024-03-11T09:46:39 Z WongYiKai Portals (BOI14_portals) C++14
100 / 100
540 ms 228208 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll r,c;

ll a(ll i, ll j) {
	return (i)*(c+2)+j;
}
pair<ll,ll> ra(ll x) {
	return {(x)/(c+2),(x)%(c+2)};
}


int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> r >> c;
	//if (r*c>100) return 0;
	ll grid[r+5][c+5];
	ll s,e;
	memset(grid,0,sizeof(grid));
	for (int i=1;i<=r;i++){
		string temp;
		cin >> temp;
		for (int j=1;j<=c;j++){
			if (temp[j-1]=='.') grid[i][j] = 1;
			else if (temp[j-1]=='#') grid[i][j] = 0;
			else if (temp[j-1]=='C') {
				grid[i][j] = 1;
				e = a(i,j);
			}
			else{
				grid[i][j] = 1;
				s = a(i,j);
			}
		}
	}
	ll visited[r*c+r+r+c+c+10];
	queue<pair<ll,ll>> bfs;
	memset(visited,0,sizeof(visited));
	for (int i=1;i<=r;i++){
		for (int j=1;j<=c;j++){
			if (grid[i][j]==0) continue;
			ll check=0;
			if (grid[i-1][j]==0) check=1;
			else if (grid[i+1][j]==0) check=1;
			else if (grid[i][j-1]==0) check=1;
			else if (grid[i][j+1]==0) check=1;
			if (check==0) continue;
			bfs.push({a(i,j),0});
			visited[a(i,j)]=1;
		}
	}
	ll dist[r*c+r+r+c+c+10];
	memset(dist,-1,sizeof(dist));
	while (!bfs.empty()){
		ll x = bfs.front().first;
		ll rnk = bfs.front().second;
		dist[x] = rnk;
		bfs.pop();
		ll item;
		pair<ll,ll> temp;
		item=x-1;
		if (visited[item]==0){
			temp = ra(item);
			if (grid[temp.first][temp.second]==1){
				visited[item] = 1;
				bfs.push({item,rnk+1});
			}
		}
		item=x+1;
		if (visited[item]==0){
			temp = ra(item);
			if (grid[temp.first][temp.second]==1){
				visited[item] = 1;
				bfs.push({item,rnk+1});
			}
		}
		item=x-c-2;
		if (visited[item]==0){
			temp = ra(item);
			if (grid[temp.first][temp.second]==1){
				visited[item] = 1;
				bfs.push({item,rnk+1});
			}
		}
		item=x+c+2;
		if (visited[item]==0){
			temp = ra(item);
			if (grid[temp.first][temp.second]==1){
				visited[item] = 1;
				bfs.push({item,rnk+1});
			}
		}
	}
	/*
	for (int i=1;i<=r;i++){
		for (int j=1;j<=c;j++){
			cout << dist[a(i,j)] << " ";
		}
		cout << "\n";
	}
	*/
	ll left[r*c+r+r+c+c+10],right[r*c+r+r+c+c+10],up[r*c+r+r+c+c+10],down[r*c+r+r+c+c+10];
	for (int j=1;j<=c;j++) up[a(0,j)] = a(1,j);
	for (int i=1;i<=r;i++){
		if (grid[i][1]==0) {
			left[a(i,1)]=a(i,2);
			up[a(i,1)]=a(i+1,1);
		}
		else {
			left[a(i,1)]=a(i,1);
			up[a(i,1)] = up[a(i-1,1)];
		}
		for (int j=2;j<=c;j++){
			if (grid[i][j]==0) {
				left[a(i,j)] = a(i,j+1);
				up[a(i,j)] = a(i+1,j);
			}
			else {
				left[a(i,j)] = left[a(i,j-1)];
				up[a(i,j)] = up[a(i-1,j)];
			}
		}
	}
	//right down
	for (int j=1;j<=c;j++) down[a(r+1,j)] = a(r,j);
	for (int i=r;i>=1;i--){
		if (grid[i][c]==0) {
			right[a(i,c)]=a(i,c-1);
			down[a(i,c)]=a(i-1,c);
		}
		else {
			right[a(i,c)]=a(i,c);
			down[a(i,c)] = down[a(i+1,c)];
		}
		for (int j=c-1;j>=1;j--){
			if (grid[i][j]==0) {
				right[a(i,j)] = a(i,j-1);
				down[a(i,j)] = a(i-1,j);
			}
			else {
				right[a(i,j)] = right[a(i,j+1)];
				down[a(i,j)] = down[a(i+1,j)];
			}
		}
	}
	/*
	cout << "left\n";
	for (int i=1;i<=r;i++){
		for (int j=1;j<=c;j++){
			cout << ra(left[a(i,j)]).first << "," << ra(left[a(i,j)]).second << " ";
		}
		cout << "\n";
	}
	cout << "right\n";
	for (int i=1;i<=r;i++){
		for (int j=1;j<=c;j++){
			cout << ra(right[a(i,j)]).first << "," << ra(right[a(i,j)]).second << " ";
		}
		cout << "\n";
	}
	cout << "down\n";
	for (int i=1;i<=r;i++){
		for (int j=1;j<=c;j++){
			cout << ra(down[a(i,j)]).first << "," << ra(down[a(i,j)]).second << " ";
		}
		cout << "\n";
	}
	cout << "up\n";
	for (int i=1;i<=r;i++){
		for (int j=1;j<=c;j++){
			cout << ra(up[a(i,j)]).first << "," << ra(up[a(i,j)]).second << " ";
		}
		cout << "\n";
	}
	*/
	
	vector<pair<ll,ll>> adj[r*c+r+r+c+c+10];
	for (int i=1;i<=r;i++){
		for (int j=1;j<=c;j++){
			if (grid[i][j]==0) continue;
			ll x = a(i,j);
			ll item;
			pair<ll,ll> temp;
			item=x-1;
			temp = ra(item);
			if (grid[temp.first][temp.second]==1){
				adj[x].push_back({item,1});
			}
			item=x+1;
			temp = ra(item);
			if (grid[temp.first][temp.second]==1){
				adj[x].push_back({item,1});
			}
			item=x-c-2;
			temp = ra(item);
			if (grid[temp.first][temp.second]==1){
				adj[x].push_back({item,1});
			}
			item=x+c+2;
			temp = ra(item);
			if (grid[temp.first][temp.second]==1){
				adj[x].push_back({item,1});
			}
			adj[x].push_back({left[x],max(0LL,dist[x])+1});
			adj[x].push_back({down[x],max(0LL,dist[x])+1});
			adj[x].push_back({up[x],max(0LL,dist[x])+1});
			adj[x].push_back({right[x],max(0LL,dist[x])+1});
		}
	}
	
	/*
	for (int i=1;i<=r;i++){
		for (int j=1;j<=c;j++){
			cout << i << "," << j << ": ";
			for (auto item:adj[a(i,j)]){
				cout << ra(item.first).first << "," << ra(item.first).second << "," << item.second << " ";
			}
			cout << "\n";
		}
	}
	*/
	
	ll dis[r*c+r+r+c+c+10];
	priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq;
	memset(dis,-1,sizeof(dis));
	dis[s]=0;
	pq.push({0,s});
	while (!pq.empty())	{
		pair<ll,ll> cur = pq.top();
		pq.pop();
		ll x = cur.second, d = cur.first;
		if (d>dis[x]) continue;
		for (vector<pair<ll,ll>>::iterator it=adj[x].begin();it!=adj[x].end();++it){
			ll nx = it->first, nd = d+it->second;
			if (nx>a(r,c) || nx<a(1,1)) continue;
			if (dis[nx]!=-1 && dis[nx]<=nd) continue;
			dis[nx] = nd;
			pq.push({nd,nx});
		}
	}
	
	/*
	for (int i=1;i<=r;i++){
		for (int j=1;j<=c;j++){
			cout << dis[a(i,j)] << " ";
		}
		cout << "\n";
	}
	*/
	cout << dis[e];	
	//cout << "a";
	
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:252:15: warning: 'e' may be used uninitialized in this function [-Wmaybe-uninitialized]
  252 |  cout << dis[e];
      |               ^
# 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 344 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 408 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 560 KB Output is correct
10 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 1 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 344 KB Output is correct
6 Correct 0 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 2 ms 860 KB Output is correct
10 Correct 2 ms 860 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 0 ms 348 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 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 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 9 ms 7000 KB Output is correct
6 Correct 10 ms 7260 KB Output is correct
7 Correct 11 ms 7516 KB Output is correct
8 Correct 9 ms 7516 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 1 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 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 1 ms 860 KB Output is correct
13 Correct 1 ms 856 KB Output is correct
14 Correct 9 ms 7000 KB Output is correct
15 Correct 14 ms 7384 KB Output is correct
16 Correct 11 ms 7516 KB Output is correct
17 Correct 13 ms 7536 KB Output is correct
18 Correct 14 ms 8284 KB Output is correct
19 Correct 14 ms 9564 KB Output is correct
20 Correct 15 ms 9564 KB Output is correct
21 Correct 12 ms 7284 KB Output is correct
22 Correct 10 ms 7336 KB Output is correct
23 Correct 10 ms 7256 KB Output is correct
24 Correct 13 ms 9564 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 9 ms 7772 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 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 600 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 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 12 ms 7004 KB Output is correct
15 Correct 10 ms 7260 KB Output is correct
16 Correct 11 ms 7516 KB Output is correct
17 Correct 10 ms 7516 KB Output is correct
18 Correct 15 ms 8284 KB Output is correct
19 Correct 14 ms 9556 KB Output is correct
20 Correct 13 ms 9564 KB Output is correct
21 Correct 9 ms 7260 KB Output is correct
22 Correct 10 ms 7124 KB Output is correct
23 Correct 10 ms 7260 KB Output is correct
24 Correct 326 ms 183524 KB Output is correct
25 Correct 540 ms 228208 KB Output is correct
26 Correct 478 ms 228000 KB Output is correct
27 Correct 473 ms 227924 KB Output is correct
28 Correct 279 ms 167648 KB Output is correct
29 Correct 303 ms 169512 KB Output is correct
30 Correct 330 ms 171272 KB Output is correct
31 Correct 16 ms 9564 KB Output is correct
32 Correct 463 ms 227720 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 860 KB Output is correct
35 Correct 355 ms 192772 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 8 ms 7516 KB Output is correct
38 Correct 245 ms 180964 KB Output is correct
39 Correct 248 ms 157412 KB Output is correct