답안 #208709

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
208709 2020-03-12T05:00:11 Z E869120 포탈들 (BOI14_portals) C++14
100 / 100
898 ms 85172 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <tuple>
#include <functional>
using namespace std;

int H, W;
int sx, sy, gx, gy;
char c[1009][1009];
int dp[1009][1009];
vector<pair<int, int>> v[1009][1009];

int dist[1009][1009];
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> Q;

int main() {
	// ステップ 1. 入力
	cin >> H >> W;
	for (int i = 0; i <= H + 1; i++) {
		for (int j = 0; j <= W + 1; j++) c[i][j] = '#';
	}
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			cin >> c[i][j];
			if (c[i][j] == 'S') { sx = i; sy = j; c[i][j] = '.'; }
			if (c[i][j] == 'C') { gx = i; gy = j; c[i][j] = '.'; }
		}
	}
	H += 2; W += 2;

	// ステップ 2. グラフに辺を貼る
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) dp[i][j] = (1 << 30);
	}
	for (int i = 0; i < H; i++) {
		int last = 0;
		for (int j = 0; j < W; j++) {
			if (c[i][j] == '#') last = j;
			dp[i][j] = min(dp[i][j], abs(j - last));
			v[i][j].push_back(make_pair(i, last + 1));
		}
		last = 0;
		for (int j = W - 1; j >= 0; j--) {
			if (c[i][j] == '#') last = j;
			dp[i][j] = min(dp[i][j], abs(j - last));
			v[i][j].push_back(make_pair(i, last - 1));
		}
	}
	for (int j = 0; j < W; j++) {
		int last = 0;
		for (int i = 0; i < H; i++) {
			if (c[i][j] == '#') last = i;
			dp[i][j] = min(dp[i][j], abs(i - last));
			v[i][j].push_back(make_pair(last + 1, j));
		}
		last = 0;
		for (int i = H - 1; i >= 0; i--) {
			if (c[i][j] == '#') last = i;
			dp[i][j] = min(dp[i][j], abs(i - last));
			v[i][j].push_back(make_pair(last - 1, j));
		}
	}

	// ステップ 3. 最短距離計算
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) dist[i][j] = (1 << 30);
	}
	dist[sx][sy] = 0;
	Q.push(make_tuple(0, sx, sy));

	while (!Q.empty()) {
		int vx = get<1>(Q.top()), vy = get<2>(Q.top()); Q.pop();
		int dx[4] = { 0, 1, 0, -1 };
		int dy[4] = { 1, 0, -1, 0 };
		for (int i = 0; i < 4; i++) {
			int ex = vx + dx[i], ey = vy + dy[i];
			if (c[ex][ey] == '#') continue;
			if (dist[ex][ey] > dist[vx][vy] + 1) {
				dist[ex][ey] = dist[vx][vy] + 1;
				Q.push(make_tuple(dist[ex][ey], ex, ey));
			}
		}
		for (pair<int, int> i : v[vx][vy]) {
			int ex = i.first, ey = i.second;
			if (dist[ex][ey] > dist[vx][vy] + dp[vx][vy]) {
				dist[ex][ey] = dist[vx][vy] + dp[vx][vy];
				Q.push(make_tuple(dist[ex][ey], ex, ey));
			}
		}
	}

	cout << dist[gx][gy] << endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 24312 KB Output is correct
2 Correct 20 ms 24312 KB Output is correct
3 Correct 20 ms 24312 KB Output is correct
4 Correct 19 ms 24312 KB Output is correct
5 Correct 20 ms 24316 KB Output is correct
6 Correct 19 ms 24312 KB Output is correct
7 Correct 20 ms 24312 KB Output is correct
8 Correct 19 ms 24312 KB Output is correct
9 Correct 20 ms 24312 KB Output is correct
10 Correct 21 ms 24312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24312 KB Output is correct
2 Correct 19 ms 24312 KB Output is correct
3 Correct 19 ms 24312 KB Output is correct
4 Correct 20 ms 24312 KB Output is correct
5 Correct 19 ms 24312 KB Output is correct
6 Correct 19 ms 24312 KB Output is correct
7 Correct 19 ms 24312 KB Output is correct
8 Correct 20 ms 24312 KB Output is correct
9 Correct 22 ms 24952 KB Output is correct
10 Correct 20 ms 24824 KB Output is correct
11 Correct 21 ms 24824 KB Output is correct
12 Correct 20 ms 24824 KB Output is correct
13 Correct 21 ms 24824 KB Output is correct
14 Correct 19 ms 24312 KB Output is correct
15 Correct 21 ms 24952 KB Output is correct
16 Correct 22 ms 24312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 24312 KB Output is correct
2 Correct 19 ms 24312 KB Output is correct
3 Correct 19 ms 24312 KB Output is correct
4 Correct 20 ms 24316 KB Output is correct
5 Correct 38 ms 28020 KB Output is correct
6 Correct 37 ms 28024 KB Output is correct
7 Correct 40 ms 28152 KB Output is correct
8 Correct 36 ms 28152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 24184 KB Output is correct
2 Correct 20 ms 24312 KB Output is correct
3 Correct 19 ms 24312 KB Output is correct
4 Correct 19 ms 24312 KB Output is correct
5 Correct 20 ms 24312 KB Output is correct
6 Correct 20 ms 24312 KB Output is correct
7 Correct 19 ms 24312 KB Output is correct
8 Correct 20 ms 24364 KB Output is correct
9 Correct 26 ms 24824 KB Output is correct
10 Correct 20 ms 24824 KB Output is correct
11 Correct 20 ms 24824 KB Output is correct
12 Correct 21 ms 24952 KB Output is correct
13 Correct 20 ms 24824 KB Output is correct
14 Correct 37 ms 28024 KB Output is correct
15 Correct 39 ms 28024 KB Output is correct
16 Correct 40 ms 28024 KB Output is correct
17 Correct 39 ms 28024 KB Output is correct
18 Correct 40 ms 28152 KB Output is correct
19 Correct 43 ms 28152 KB Output is correct
20 Correct 41 ms 28152 KB Output is correct
21 Correct 36 ms 28024 KB Output is correct
22 Correct 39 ms 28024 KB Output is correct
23 Correct 39 ms 28024 KB Output is correct
24 Correct 39 ms 28024 KB Output is correct
25 Correct 19 ms 24312 KB Output is correct
26 Correct 21 ms 24824 KB Output is correct
27 Correct 20 ms 24312 KB Output is correct
28 Correct 35 ms 28024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 24312 KB Output is correct
2 Correct 20 ms 24316 KB Output is correct
3 Correct 20 ms 24312 KB Output is correct
4 Correct 20 ms 24312 KB Output is correct
5 Correct 19 ms 24312 KB Output is correct
6 Correct 19 ms 24312 KB Output is correct
7 Correct 20 ms 24312 KB Output is correct
8 Correct 19 ms 24312 KB Output is correct
9 Correct 21 ms 24824 KB Output is correct
10 Correct 20 ms 24952 KB Output is correct
11 Correct 21 ms 24824 KB Output is correct
12 Correct 21 ms 24824 KB Output is correct
13 Correct 22 ms 24956 KB Output is correct
14 Correct 36 ms 28024 KB Output is correct
15 Correct 40 ms 28024 KB Output is correct
16 Correct 39 ms 28024 KB Output is correct
17 Correct 38 ms 28024 KB Output is correct
18 Correct 41 ms 28152 KB Output is correct
19 Correct 43 ms 28152 KB Output is correct
20 Correct 39 ms 28152 KB Output is correct
21 Correct 36 ms 28024 KB Output is correct
22 Correct 38 ms 28152 KB Output is correct
23 Correct 40 ms 28152 KB Output is correct
24 Correct 708 ms 83960 KB Output is correct
25 Correct 898 ms 85172 KB Output is correct
26 Correct 801 ms 85112 KB Output is correct
27 Correct 788 ms 85112 KB Output is correct
28 Correct 646 ms 84856 KB Output is correct
29 Correct 667 ms 84856 KB Output is correct
30 Correct 712 ms 84732 KB Output is correct
31 Correct 39 ms 28152 KB Output is correct
32 Correct 765 ms 84984 KB Output is correct
33 Correct 20 ms 24312 KB Output is correct
34 Correct 21 ms 24824 KB Output is correct
35 Correct 716 ms 85112 KB Output is correct
36 Correct 19 ms 24312 KB Output is correct
37 Correct 35 ms 28152 KB Output is correct
38 Correct 584 ms 84984 KB Output is correct
39 Correct 642 ms 84732 KB Output is correct