Hello Ocean! ๐ŸŒผ

[C++/๋ฐฑ์ค€] 17244. ์•„๋งž๋‹ค์šฐ์‚ฐ ๋ณธ๋ฌธ

Algorithm

[C++/๋ฐฑ์ค€] 17244. ์•„๋งž๋‹ค์šฐ์‚ฐ

bba_dda 2021. 8. 3. 15:35
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://www.acmicpc.net/problem/17244

 

17244๋ฒˆ: ์•„๋งž๋‹ค์šฐ์‚ฐ

๊ฒฝ์žฌ์”จ๋Š” ์ €๋… ์•ฝ์†์„ ๊ฐ€๊ธฐ ์ „ ์ฑ™๊ธฐ์ง€ ์•Š์€ ๋ฌผ๊ฑด๋“ค์ด ์žˆ๋Š” ์ง€ ํ™•์ธํ•˜๊ณ  ์žˆ๋‹ค. ํ•„์š”ํ•œ ๋ฌผ๊ฑด์€ ์ „๋ถ€ ์ฑ™๊ธด ๊ฒƒ ๊ฐ™์•˜๊ณ  ์™ธ์ถœ ํ›„ ๋Œ์•„์˜ค๋Š” ๊ธธ์— ๊ฒฝ์žฌ์”จ๋Š” ์™ธ์ณค๋‹ค. "์•„ ๋งž๋‹ค ์šฐ์‚ฐ!!!" ๊ฒฝ์žฌ ์”จ๋Š” ๋งค๋ฒˆ ์™ธ์ถœ

www.acmicpc.net

2์ฐจ์› ๋งต์—์„œ, ์ถœ๋ฐœ์—์„œ ๋„์ฐฉ์ ์œผ๋กœ ๊ฐ€๋Š”๋ฐ, X๋กœ ํ‘œ์‹œ๋œ ํ•„์š”ํ•œ ๋ฌผ๊ฑด๋“ค์„ ๋ชจ๋‘ ์ฑ™๊ฒจ์„œ ๋‚˜๊ฐ€๋Š” ์ตœ์†Œ ์ด๋™๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. 

 

ํ’€์ด

๊ทธ๋ž˜ํ”„์ƒ์—์„œ ์ตœ์†Œ ์ด๋™ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๊ธฐ ๋•Œ๋ฌธ์— BFS๋ฅผ ์ด์šฉํ–ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ , ๊ทธ๋ž˜ํ”„์ƒ์— X๊ฐ€ ์žˆ๋Š” ์ง€์ ์„ ๋ชจ๋‘ ๋ฐฉ๋ฌธํ•˜๋Š”๊ฒŒ ์กฐ๊ฑด์ด๊ธฐ ๋•Œ๋ฌธ์— X๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ์–ด๋–ป๊ฒŒ ํ• ์ง€ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€, X๊ฐ€ ์ตœ๋Œ€ 5๊ฐœ๋ผ๊ณ  ํ•ด์„œ ๊ธธ์ด 5์งœ๋ฆฌ ๋น„ํŠธ๋งˆ์Šคํฌ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค. 

 

๊ฐ X๋“ค์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด, ์ž…๋ ฅ๋ฐ›์„ ๋•Œ X๋ฅผ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ์ˆซ์ž(0~5)๋กœ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋‹ค. 

 

์ฝ”๋“œ

#include <iostream>
#include <queue>

#define sc scanf_s

using namespace std;

int N, M;
char table[51][51];
bool visited[51][51][1 << 5];
int dr[4] = { -1,1,0,0 };
int dc[4] = { 0,0,-1,1 };
int x = 0; //X๊ฐœ์ˆ˜

struct Node {
	int r; int c; int bitmap; int count;
};
int BFS(int startR, int startC) {
	queue<Node> q;
	q.push({ startR, startC, 0, 0 });

	while (!q.empty()) {
		int r = q.front().r;
		int c = q.front().c;
		int bitmap = q.front().bitmap;
		int count = q.front().count;
		q.pop();
		for (int i = 0;i < 4;i++) {
			int newR = r + dr[i]; int newC = c + dc[i];
			if (newR < 0 || newR >= M || newC < 0 || newC >= N) continue;
			if (table[newR][newC] == '#') continue;
			else if (table[newR][newC] == 'E') {
				if (bitmap == (1 << x)-1) {
					return count + 1;
				}
				if (!visited[newR][newC][bitmap]) {
					q.push({ newR, newC, bitmap, count + 1 });
					visited[newR][newC][bitmap] = 1;
				}
			}
			else if (table[newR][newC] == '.') { // ํ‰๋ฒ”
				if (!visited[newR][newC][bitmap]) {
					q.push({ newR, newC, bitmap, count + 1 });
					visited[newR][newC][bitmap] = 1;
				}
					
			} 
			else { // X
				int temp = table[newR][newC] - '0';
				int newBitmap = bitmap | (1 << temp);
				if (!visited[newR][newC][newBitmap]) {
					q.push({ newR,newC,newBitmap,count + 1 });
					visited[newR][newC][newBitmap] = 1;
				}
			}
		}
	}
	return -1;
}
int main() {
	sc("%d %d", &N, &M); // N : col, M : row
	int startR, startC;
	for (int m = 0;m < M;m++) {
		for (int n = 0;n < N;n++) {
			sc("%c", &table[m][n]); // %c ์•ž์— ๊ณต๋ฐฑ ๋„ฃ์–ด์ฃผ๊ธฐ ๋ณ„ํ‘œ๋‹ค์„ฏ๊ฐœ
			if (table[m][n] == 'X') {
				//table[m][n] = x + '0';
				x++;
			}
			else if (table[m][n] == 'S') {
				startR = m;
				startC = n;
				table[m][n] = '.';
			}
		}
	}
	int result = BFS(startR, startC);
	printf("%d\n", result);
	return 0;
}

 

๊ฒฐ๊ณผ

๋ฐ˜์‘ํ˜•