답안 #412515

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
412515 2021-05-27T03:26:11 Z rqi 포탈들 (BOI14_portals) C++14
100 / 100
400 ms 46584 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef vector<int> vi;
typedef long long ll;
typedef long double db;
typedef pair<int, int> pi;

#define mp make_pair
#define f first
#define s second
#define pb push_back

#define sz(x) (int)(x).size()
 
const int MOD = 1e9+7;
const int mx = 1005;

const vi dx = vi{1, 0, -1, 0};
const vi dy = vi{0, 1, 0, -1};

int R, C;
bool wall[mx][mx];
pi hit[mx][mx][4];

void setHit(int i, int j, int d){
	if(hit[i][j][d] != mp(-1, -1)) return;
	if(wall[i+dx[d]][j+dy[d]]){
		hit[i][j][d] = mp(i, j);
		return;
	}
	setHit(i+dx[d], j+dy[d], d);
	hit[i][j][d] = hit[i+dx[d]][j+dy[d]][d];
}

int wall_dist[mx][mx];
int dist[mx][mx];
 
int main(){
	cin.tie(0)->sync_with_stdio(0);
	pi start_pos;
	pi end_pos;
	cin >> R >> C;
	for(int i = 0; i <= R+1; i++){
		wall[i][0] = wall[i][C+1] = 1;
	}
	for(int i = 0; i <= C+1; i++){
		wall[0][i] = wall[R+1][i] = 1;
	}


	for(int i = 1; i <= R; i++){
		string s;
		cin >> s;
		for(int j = 1; j <= C; j++){
			if(s[j-1] == '#'){
				wall[i][j] = 1;
			}
			else if(s[j-1] == 'S'){
				start_pos = mp(i, j);
			}
			else if(s[j-1] == 'C'){
				end_pos = mp(i, j);
			}
		}
	}

	for(int d = 0; d < 4; d++){
		for(int i = 0; i <= R+1; i++){
			for(int j = 0; j <= C+1; j++){
				hit[i][j][d] = mp(-1, -1);
			}
		}
		for(int i = 0; i <= R+1; i++){
			for(int j = 0; j <= C+1; j++){
				if(!wall[i][j]){
					setHit(i, j, d);
				}
			}
		}
	}

	queue<pi> q;
	for(int i = 0; i <= R+1; i++){
		for(int j = 0; j <= C+1; j++){
			wall_dist[i][j] = MOD;
			if(wall[i][j]){
				wall_dist[i][j] = 0;
				q.push(mp(i, j));
			}
		}
	}
	while(sz(q)){
		pi pos = q.front();
		q.pop();
		for(int d = 0; d < 4; d++){
			pi new_pos = mp(pos.f+dx[d], pos.s+dy[d]);
			if(new_pos.f < 0 || new_pos.f > R+1 || new_pos.s < 0 || new_pos.s > C+1) continue;
			int new_dist = wall_dist[pos.f][pos.s]+1;
			if(wall_dist[new_pos.f][new_pos.s] <= new_dist) continue;
			wall_dist[new_pos.f][new_pos.s] = new_dist;
			q.push(new_pos);
		}
	}

	// for(int i = 0; i <= R+1; i++){
	// 	for(int j = 0; j <= C+1; j++){
	// 		cout << wall_dist[i][j] << " ";
	// 	}
	// 	cout << "\n";
	// }

	priority_queue<pair<int, pi>, vector<pair<int, pi>>, greater<pair<int, pi>>> pq;
	for(int i = 0; i <= R+1; i++){
		for(int j = 0; j <= C+1; j++){
			dist[i][j] = MOD;
		}
	}
	dist[start_pos.f][start_pos.s] = 0;
	pq.push(mp(0, start_pos));
	while(sz(pq)){
		int test_dist = pq.top().f; 
		pi pos = pq.top().s;
		pq.pop();
		if(dist[pos.f][pos.s] < test_dist) continue;
		if(pos == end_pos){
			cout << test_dist << "\n";
			exit(0);
		}
		for(int d = 0; d < 4; d++){
			pi new_pos = mp(pos.f+dx[d], pos.s+dy[d]);
			if(wall[new_pos.f][new_pos.s]) continue;
			int new_dist = dist[pos.f][pos.s]+1;
			if(dist[new_pos.f][new_pos.s] <= new_dist) continue;
			dist[new_pos.f][new_pos.s] = new_dist;
			pq.push(mp(dist[new_pos.f][new_pos.s], new_pos));
		}
		for(int d = 0; d < 4; d++){
			pi new_pos = hit[pos.f][pos.s][d];
			if(wall[new_pos.f][new_pos.s]) continue;
			int new_dist = dist[pos.f][pos.s]+wall_dist[pos.f][pos.s];
			if(dist[new_pos.f][new_pos.s] <= new_dist) continue;
			dist[new_pos.f][new_pos.s] = new_dist;
			pq.push(mp(dist[new_pos.f][new_pos.s], new_pos));
		}
	}
	assert(false);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 452 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 452 KB Output is correct
9 Correct 2 ms 1100 KB Output is correct
10 Correct 2 ms 1100 KB Output is correct
11 Correct 2 ms 1096 KB Output is correct
12 Correct 2 ms 1092 KB Output is correct
13 Correct 2 ms 1100 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 1100 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 10 ms 4428 KB Output is correct
6 Correct 11 ms 4472 KB Output is correct
7 Correct 9 ms 4428 KB Output is correct
8 Correct 6 ms 4428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 452 KB Output is correct
6 Correct 1 ms 444 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 2 ms 1100 KB Output is correct
10 Correct 2 ms 1100 KB Output is correct
11 Correct 2 ms 1100 KB Output is correct
12 Correct 2 ms 1100 KB Output is correct
13 Correct 2 ms 1100 KB Output is correct
14 Correct 11 ms 4436 KB Output is correct
15 Correct 11 ms 4428 KB Output is correct
16 Correct 11 ms 4424 KB Output is correct
17 Correct 9 ms 4428 KB Output is correct
18 Correct 11 ms 4428 KB Output is correct
19 Correct 8 ms 4172 KB Output is correct
20 Correct 10 ms 4300 KB Output is correct
21 Correct 10 ms 4428 KB Output is correct
22 Correct 10 ms 4452 KB Output is correct
23 Correct 11 ms 4428 KB Output is correct
24 Correct 12 ms 4292 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 2 ms 1100 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 6 ms 4428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 452 KB Output is correct
7 Correct 1 ms 448 KB Output is correct
8 Correct 1 ms 452 KB Output is correct
9 Correct 2 ms 1100 KB Output is correct
10 Correct 2 ms 1080 KB Output is correct
11 Correct 2 ms 1100 KB Output is correct
12 Correct 2 ms 1100 KB Output is correct
13 Correct 2 ms 1100 KB Output is correct
14 Correct 10 ms 4428 KB Output is correct
15 Correct 10 ms 4428 KB Output is correct
16 Correct 10 ms 4428 KB Output is correct
17 Correct 10 ms 4432 KB Output is correct
18 Correct 11 ms 4412 KB Output is correct
19 Correct 6 ms 4172 KB Output is correct
20 Correct 10 ms 4300 KB Output is correct
21 Correct 9 ms 4428 KB Output is correct
22 Correct 10 ms 4392 KB Output is correct
23 Correct 11 ms 4428 KB Output is correct
24 Correct 249 ms 45360 KB Output is correct
25 Correct 400 ms 42080 KB Output is correct
26 Correct 130 ms 41832 KB Output is correct
27 Correct 223 ms 41976 KB Output is correct
28 Correct 207 ms 45556 KB Output is correct
29 Correct 270 ms 46556 KB Output is correct
30 Correct 224 ms 46584 KB Output is correct
31 Correct 12 ms 4284 KB Output is correct
32 Correct 316 ms 41924 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 2 ms 1100 KB Output is correct
35 Correct 193 ms 43868 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 8 ms 4684 KB Output is correct
38 Correct 133 ms 44712 KB Output is correct
39 Correct 148 ms 45964 KB Output is correct