답안 #499393

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
499393 2021-12-28T10:47:59 Z jansenkenpegrasio 포탈들 (BOI14_portals) C++14
100 / 100
286 ms 26320 KB
#include<stdio.h>
#include<utility>
#include<queue>
#define pp pair<int, pair <int, int> >
using namespace std;
int main()
{
	int R, C;
	scanf("%d%d", &R, &C);
	vector < vector <char> > board (R, vector <char> (C));
	pair <int, int> start, cake;
	vector < vector <int> > tembok(R, vector <int> (C, 1e9));
	for (int i = 0; i < R; i++)
	{
		for (int j = 0; j < C; j++)
		{
			if (i == 0 || i == R - 1 || j == 0 || j == C - 1)
			{
				tembok[i][j] = 1;
			}
		}
	}
	for (int i = 0; i < R; i++)
	{
		for (int j = 0; j < C; j++)
		{
			scanf(" %c", &board[i][j]);
			if (board[i][j] == 'S')
				start = make_pair(i, j);
			if (board[i][j] == 'C')
				cake = make_pair(i, j);
			if (board[i][j] == '#')
				tembok[i][j] = 0;
		}
	}
//	1. Cari untuk setiap titik, jarak terdekat dengan temboknya berapa?
	for (int i = 0; i < R; i++)
	{
		for (int j = 1; j < C; j++)
		{
			tembok[i][j] = min(tembok[i][j], tembok[i][j - 1] + 1);
		}
	}
	for (int i = 1; i < R; i++)
	{
		for (int j = 0; j < C; j++)
		{
			tembok[i][j] = min(tembok[i][j], tembok[i - 1][j] + 1);
		}
	}
	for (int i = 0; i < R; i++)
	{
		for (int j = C - 2; j >= 0; j--)
		{
			tembok[i][j] = min(tembok[i][j], tembok[i][j + 1] + 1);
		}
	}
	for (int i = R - 2; i >= 0; i--)
	{
		for (int j = 0; j < C; j++)
		{
			tembok[i][j] = min(tembok[i][j], tembok[i + 1][j] + 1);
		}
	}
//  Tembok arah bawah
	vector <vector <int> > bawah(R, vector <int>(C, -1));
	for (int i = 0; i < C; i++)
	{
		int index = R;
		for (int j = R - 1; j >= 0; j--)
		{
			if (board[j][i] == '#')
				index = j;
			bawah[j][i] = index;
//			printf("i j index %d %d %d\n", i, j, index);
		}
	}
//	printf("Bawah\n");
//	for (int i = 0; i < R; i++)
//	{
//		for (int j = 0; j < C; j++)
//		{
//			printf("%d ", bawah[i][j]);
//		}
//		printf("\n");
//	}
// 	Tembok Arah Atas
	vector <vector <int> > atas(R, vector <int>(C, -1));
	for (int i = 0; i < C; i++)
	{
		int index = -1;
		for (int j = 0; j < R; j++)
		{
			if (board[j][i] == '#')
				index = j;
			atas[j][i] = index;
//			printf("i j index %d %d %d\n", i, j, index);
		}
	}
//	printf("Atas\n");
//	for (int i = 0; i < R; i++)
//	{
//		for (int j = 0; j < C; j++)
//		{
//			printf("%d ", atas[i][j]);
//		}
//		printf("\n");
//	}
//  Tembok Arah Kiri
	vector <vector <int> > kiri(R, vector <int>(C, -1));
	for (int i = 0; i < R; i++)
	{
		int index = -1;
		for (int j = 0; j < C; j++)
		{
			if (board[i][j] == '#')
				index = j;
			kiri[i][j] = index;
//			printf("i j index %d %d %d\n", i, j, index);
		}
	}
//	printf("Kiri\n");
//	for (int i = 0; i < R; i++)
//	{
//		for (int j = 0; j < C; j++)
//		{
//			printf("%d ", kiri[i][j]);
//		}
//		printf("\n");
//	}
//  Tembok Arah Kanan
	vector <vector <int> > kanan(R, vector <int>(C, -1));
	for (int i = 0; i < R; i++)
	{
		int index = C;
		for (int j = C - 1; j >= 0; j--)
		{
			if (board[i][j] == '#')
				index = j;
			kanan[i][j] = index;
//			printf("i j index %d %d %d\n", i, j, index);
		}
	}
//	printf("kanan\n");
//	for (int i = 0; i < R; i++)
//	{
//		for (int j = 0; j < C; j++)
//			printf("%d ", kanan[i][j]);
//		printf("\n");
//	}
	priority_queue<pp, vector <pp>, greater <pp> > djikstra;
	vector <vector <int> > distance(R, vector <int> (C, 1e9));
//	first -> jarak, second.first -> x, second.second
	djikstra.push(make_pair(0, make_pair(start.first, start.second)));
	distance[start.first][start.second] = 0;
	while (!djikstra.empty())
	{
		int weight = djikstra.top().first;
		int x = djikstra.top().second.first;
		int y = djikstra.top().second.second;
//		printf("wxy %d %d %d\n", weight, x, y);
		djikstra.pop();
		if (x >= R || y >= C || x < 0 || y < 0)
			continue;
//		printf("A");
		if (board[x][y] == '#')
			continue;
//		printf("B");
		if (weight != distance[x][y])
			continue;
//		printf("C\n");
//		1. Kiri kanan atas bawah
		if (x + 1 < R && distance[x + 1][y] > weight + 1)
		{
			djikstra.push(make_pair(weight + 1, make_pair(x + 1, y)));
			distance[x + 1][y] = min(distance[x + 1][y], weight + 1);
		}
//		printf("91\n");
		if (y + 1 < C && distance[x][y + 1] > weight + 1)
		{
			djikstra.push(make_pair(weight + 1, make_pair(x, y + 1)));
			distance[x][y + 1] = min(distance[x][y + 1], weight + 1);
		}
//		printf("97\n");
		if (x - 1 >= 0 && distance[x - 1][y] > weight + 1)
		{
			djikstra.push(make_pair(weight + 1, make_pair(x - 1, y)));
			distance[x - 1][y] = min(distance[x - 1][y], weight + 1);
		}
		if (y - 1 >= 0 && distance[x][y - 1] > weight + 1)
		{
			djikstra.push(make_pair(weight + 1, make_pair(x, y - 1)));
			distance[x][y - 1] = min(distance[x][y - 1], weight + 1);
		}
//		printf("106\n");
//		2. Nembak portal biru masuk lewat tembok terdekat
//		a. Arah Bawah
		int temp_x = bawah[x][y]; // inget x itu berhubungannya dengan baris
		if (distance[temp_x - 1][y] > weight + tembok[x][y])
		{
			djikstra.push(make_pair(weight + tembok[x][y], make_pair(temp_x - 1, y)));
			distance[temp_x - 1][y] = min(distance[temp_x - 1][y], weight + tembok[x][y]);
		}
//		printf("Bawah");
//		b. Arah atas
		temp_x = atas[x][y];
//		printf("Atas\n");
		if (distance[temp_x + 1][y] > weight + tembok[x][y])
		{
			djikstra.push(make_pair(weight + tembok[x][y], make_pair(temp_x + 1, y)));
			distance[temp_x + 1][y] = min(distance[temp_x + 1][y], weight + tembok[x][y]);
		}
//		c. Arah kanan
		int temp_y = kanan[x][y];
//		printf("Kanan\n");
		if (distance[x][temp_y - 1] > weight + tembok[x][y])
		{
			djikstra.push(make_pair(weight + tembok[x][y], make_pair(x, temp_y - 1)));
			distance[x][temp_y - 1] = min(distance[x][temp_y - 1], weight + tembok[x][y]);
		}
//		d. Arah Kiri
		temp_y = kiri[x][y];
//		printf("Kiri\n");
		if (distance[x][temp_y + 1] > weight + tembok[x][y])
		{
			djikstra.push(make_pair(weight + tembok[x][y], make_pair(x, temp_y + 1)));
			distance[x][temp_y + 1] = min(distance[x][temp_y + 1], weight + tembok[x][y]);
		}
	}
	printf("%d\n", distance[cake.first][cake.second]);
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:9:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |  scanf("%d%d", &R, &C);
      |  ~~~~~^~~~~~~~~~~~~~~~
portals.cpp:27:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |    scanf(" %c", &board[i][j]);
      |    ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 448 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 7 ms 1228 KB Output is correct
6 Correct 9 ms 1228 KB Output is correct
7 Correct 8 ms 1372 KB Output is correct
8 Correct 4 ms 1356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 7 ms 1228 KB Output is correct
15 Correct 8 ms 1228 KB Output is correct
16 Correct 8 ms 1296 KB Output is correct
17 Correct 9 ms 1312 KB Output is correct
18 Correct 9 ms 1316 KB Output is correct
19 Correct 9 ms 1292 KB Output is correct
20 Correct 9 ms 1368 KB Output is correct
21 Correct 7 ms 1316 KB Output is correct
22 Correct 8 ms 1348 KB Output is correct
23 Correct 8 ms 1308 KB Output is correct
24 Correct 7 ms 1356 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 5 ms 1292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 7 ms 1292 KB Output is correct
15 Correct 10 ms 1228 KB Output is correct
16 Correct 8 ms 1356 KB Output is correct
17 Correct 9 ms 1296 KB Output is correct
18 Correct 9 ms 1356 KB Output is correct
19 Correct 11 ms 1392 KB Output is correct
20 Correct 10 ms 1356 KB Output is correct
21 Correct 7 ms 1356 KB Output is correct
22 Correct 9 ms 1356 KB Output is correct
23 Correct 8 ms 1356 KB Output is correct
24 Correct 224 ms 26132 KB Output is correct
25 Correct 286 ms 26320 KB Output is correct
26 Correct 226 ms 26076 KB Output is correct
27 Correct 224 ms 26052 KB Output is correct
28 Correct 172 ms 26020 KB Output is correct
29 Correct 192 ms 25924 KB Output is correct
30 Correct 229 ms 25940 KB Output is correct
31 Correct 7 ms 1356 KB Output is correct
32 Correct 206 ms 26012 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 209 ms 26016 KB Output is correct
36 Correct 0 ms 204 KB Output is correct
37 Correct 4 ms 1228 KB Output is correct
38 Correct 103 ms 25964 KB Output is correct
39 Correct 199 ms 25908 KB Output is correct