답안 #413904

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
413904 2021-05-29T16:59:38 Z Blagojce 포탈들 (BOI14_portals) C++11
100 / 100
425 ms 47336 KB
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
  
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const ll inf = 1e18;
const int i_inf = 1e9;	
const ll mod = 1e9+7;
  
const int mxn = 1e3+3;
 
const ll A = 911382323;
const ll B = 972663749;
 
mt19937 _rand(time(NULL));
clock_t z;
 
int n, m;
char a[mxn][mxn];

int d[mxn][mxn];


int dr[4] = {-1, 0, +1, 0};
int dc[4] = {0, +1, 0, -1};


bool inside(int r, int c){
	return (0 <= r && r < n && 0 <= c && c < m);
}
bool portable(int r, int c){
	fr(i, 0, 4){
		int rp = r + dr[i];
		int cp = c + dc[i];
		if(inside(rp, cp) && a[rp][cp] == '#'){
			return true;
		}
	}
	return false;
}
bool vis[mxn][mxn];
void fill_d(){
	
	queue<pii> Q;
	fr(i, 0, n){
		fr(j, 0, m){
			if(a[i][j] == '#') continue;
			if(portable(i, j)){
				vis[i][j] = true;
				Q.push({i, j});
			}
		}
	}
	while(!Q.empty()){
		int r = Q.front().st;
		int c = Q.front().nd;
		Q.pop();
		fr(i, 0, 4){
			int rp = r + dr[i];
			int cp = c + dc[i];
			if(inside(rp, cp) && !vis[rp][cp] && a[rp][cp] != '#'){
				vis[rp][cp] = true;
				d[rp][cp] = d[r][c] + 1;
				Q.push({rp, cp});
			}
		}
	}
}
pii tel[mxn][mxn][4];

void fill_tel(){
	fr(i, 0, n){
		int r = -1, c = -1;
		fr(j, 1, m){
			if(a[i][j] == '#'){
				
				tel[i][j][0] = {-1, -1};
				continue;
			}
			if(a[i][j-1] == '#'){
				r = i, c = j;
			}
			tel[i][j][0] = {r, c};
		}
		r = -1, c = -1;
		for(int j = m-2; j >= 0; j --){
			if(a[i][j] == '#'){	
				tel[i][j][1] = {-1, -1};
				continue;
			}
			if(a[i][j+1] == '#'){
				r = i, c = j;
			}
			tel[i][j][1] = {r, c};
		}
	}
	fr(j, 0, m){
		int r = -1, c = -1;
		fr(i, 1, n){
			if(a[i][j] == '#'){
				tel[i][j][2] = {-1, -1};
				continue;
			}
			if(a[i-1][j] == '#'){
				r = i, c = j;
			}
			tel[i][j][2] = {r, c};
		}
		r = -1, c = -1;
		for(int i = n-2; i >= 0; i --){
			if(a[i][j] == '#'){
				tel[i][j][3] = {-1, -1};
				continue;
			}
			if(a[i+1][j] == '#'){
				r = i, c = j;
			}
			tel[i][j][3] = {r, c};
		}
	}
}



	
int dist[mxn][mxn];
void dijkstra(){
	memset(vis, false, sizeof(vis));
	
	fr(i, 0, n){
		fr(j, 0, m){
			dist[i][j] = n*m;
		}
	}
	int sr, sc;
	fr(i, 0, n){
		fr(j, 0, m){
			if(a[i][j] == 'S'){
				sr = i;
				sc = j;
			}
		}
	}
	
	dist[sr][sc] = 0;
	pq<pair<int, pii> > Q;
	Q.push({0, {sr, sc}});
	while(!Q.empty()){
		int r = Q.top().nd.st;
		int c = Q.top().nd.nd;
		
		Q.pop();
		
		if(vis[r][c])continue;
		vis[r][c] = true;
		
		fr(i, 0, 4){
			int rp = r + dr[i];
			int cp = c + dc[i];
			if(inside(rp, cp) && a[rp][cp] != '#' && dist[r][c] + 1 < dist[rp][cp]){
				dist[rp][cp] = dist[r][c] + 1;
				Q.push({-dist[rp][cp], {rp, cp}});
			}
		}
		fr(i, 0, 4){
			if(tel[r][c][i].st == -1) continue;
			
			int rp = tel[r][c][i].st;
			int cp = tel[r][c][i].nd;
			if(dist[r][c] + d[r][c] + 1< dist[rp][cp]){
				dist[rp][cp] = dist[r][c] + d[r][c] + 1;
				Q.push({-dist[rp][cp], {rp, cp}});
			}
		}
	}
	int er, ec;
	fr(i, 0, n){
		fr(j, 0, m){
			if(a[i][j] == 'C'){
				er = i;
				ec = j;
				break;
			}
		}
	}
	cout<<dist[er][ec]<<endl;
	return;

}


void solve(){
	cin >> n >> m;
	fr(i, 0, n+2) fr(j, 0, m+2) a[i][j] = '#';
	
	fr(i, 1, n+1){
		fr(j, 1, m+1){
			cin >> a[i][j];
		}
	}
	n += 2;
	m += 2;
	
	fill_d();
	fill_tel();
	
	
	
	dijkstra();
	
	
	
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	solve();
}

Compilation message

portals.cpp: In function 'void dijkstra()':
portals.cpp:193:19: warning: 'ec' may be used uninitialized in this function [-Wmaybe-uninitialized]
  193 |  cout<<dist[er][ec]<<endl;
      |                   ^
portals.cpp:193:19: warning: 'er' may be used uninitialized in this function [-Wmaybe-uninitialized]
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1228 KB Output is correct
2 Correct 1 ms 1356 KB Output is correct
3 Correct 2 ms 1356 KB Output is correct
4 Correct 1 ms 1356 KB Output is correct
5 Correct 1 ms 1356 KB Output is correct
6 Correct 1 ms 1356 KB Output is correct
7 Correct 1 ms 1356 KB Output is correct
8 Correct 1 ms 1356 KB Output is correct
9 Correct 1 ms 1356 KB Output is correct
10 Correct 2 ms 1356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1228 KB Output is correct
2 Correct 1 ms 1356 KB Output is correct
3 Correct 1 ms 1356 KB Output is correct
4 Correct 1 ms 1356 KB Output is correct
5 Correct 1 ms 1356 KB Output is correct
6 Correct 1 ms 1356 KB Output is correct
7 Correct 1 ms 1352 KB Output is correct
8 Correct 1 ms 1356 KB Output is correct
9 Correct 2 ms 1996 KB Output is correct
10 Correct 2 ms 1996 KB Output is correct
11 Correct 2 ms 1868 KB Output is correct
12 Correct 2 ms 1996 KB Output is correct
13 Correct 2 ms 1996 KB Output is correct
14 Correct 1 ms 1356 KB Output is correct
15 Correct 2 ms 1996 KB Output is correct
16 Correct 1 ms 1356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1228 KB Output is correct
2 Correct 2 ms 1356 KB Output is correct
3 Correct 1 ms 1356 KB Output is correct
4 Correct 1 ms 1356 KB Output is correct
5 Correct 8 ms 4508 KB Output is correct
6 Correct 10 ms 4500 KB Output is correct
7 Correct 10 ms 4484 KB Output is correct
8 Correct 6 ms 4476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1352 KB Output is correct
2 Correct 1 ms 1356 KB Output is correct
3 Correct 1 ms 1356 KB Output is correct
4 Correct 1 ms 1356 KB Output is correct
5 Correct 1 ms 1356 KB Output is correct
6 Correct 1 ms 1356 KB Output is correct
7 Correct 1 ms 1356 KB Output is correct
8 Correct 1 ms 1352 KB Output is correct
9 Correct 3 ms 2004 KB Output is correct
10 Correct 2 ms 2004 KB Output is correct
11 Correct 2 ms 1872 KB Output is correct
12 Correct 2 ms 1996 KB Output is correct
13 Correct 2 ms 1996 KB Output is correct
14 Correct 8 ms 4508 KB Output is correct
15 Correct 10 ms 4500 KB Output is correct
16 Correct 10 ms 4600 KB Output is correct
17 Correct 10 ms 5340 KB Output is correct
18 Correct 14 ms 5336 KB Output is correct
19 Correct 13 ms 5260 KB Output is correct
20 Correct 12 ms 5208 KB Output is correct
21 Correct 9 ms 5068 KB Output is correct
22 Correct 10 ms 5080 KB Output is correct
23 Correct 10 ms 5420 KB Output is correct
24 Correct 11 ms 5184 KB Output is correct
25 Correct 1 ms 1356 KB Output is correct
26 Correct 2 ms 1996 KB Output is correct
27 Correct 1 ms 1356 KB Output is correct
28 Correct 6 ms 4476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1228 KB Output is correct
2 Correct 1 ms 1356 KB Output is correct
3 Correct 1 ms 1356 KB Output is correct
4 Correct 2 ms 1356 KB Output is correct
5 Correct 1 ms 1356 KB Output is correct
6 Correct 1 ms 1364 KB Output is correct
7 Correct 1 ms 1356 KB Output is correct
8 Correct 2 ms 1356 KB Output is correct
9 Correct 2 ms 1996 KB Output is correct
10 Correct 2 ms 1996 KB Output is correct
11 Correct 3 ms 1868 KB Output is correct
12 Correct 3 ms 1996 KB Output is correct
13 Correct 2 ms 1996 KB Output is correct
14 Correct 9 ms 4508 KB Output is correct
15 Correct 10 ms 4524 KB Output is correct
16 Correct 11 ms 4496 KB Output is correct
17 Correct 11 ms 5328 KB Output is correct
18 Correct 12 ms 5336 KB Output is correct
19 Correct 13 ms 5196 KB Output is correct
20 Correct 13 ms 5284 KB Output is correct
21 Correct 9 ms 5068 KB Output is correct
22 Correct 10 ms 5176 KB Output is correct
23 Correct 10 ms 5332 KB Output is correct
24 Correct 244 ms 46124 KB Output is correct
25 Correct 425 ms 43188 KB Output is correct
26 Correct 336 ms 42896 KB Output is correct
27 Correct 314 ms 42704 KB Output is correct
28 Correct 179 ms 38824 KB Output is correct
29 Correct 201 ms 47248 KB Output is correct
30 Correct 230 ms 47336 KB Output is correct
31 Correct 13 ms 5196 KB Output is correct
32 Correct 306 ms 42636 KB Output is correct
33 Correct 1 ms 1356 KB Output is correct
34 Correct 2 ms 1996 KB Output is correct
35 Correct 252 ms 43220 KB Output is correct
36 Correct 1 ms 1356 KB Output is correct
37 Correct 6 ms 4476 KB Output is correct
38 Correct 126 ms 38748 KB Output is correct
39 Correct 165 ms 38816 KB Output is correct