1일1알

백준 6593번 상범 빌딩 C++ 본문

알고리즘

백준 6593번 상범 빌딩 C++

영춘권의달인 2022. 3. 16. 12:19

출처 : https://www.acmicpc.net/problem/6593

 

 bfs인데, 상하 도 있기 때문에 보통의 2차원 배열이 아닌 3차원 배열을 이용해서 문제를 해결하였다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>

using namespace std;
using ll = long long;

int dHeight[6] =	{ 1,-1, 0, 0, 0, 0 };
int dRow[6] =		{ 0, 0,-1, 0, 1, 0 };
int dCol[6] =		{ 0, 0, 0, 1, 0,-1 };

struct Info {
	int h;
	int r;
	int c;
	int cnt;
};

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

	int l, r, c;
	while (true) {
		cin >> l >> r >> c;
		if (l == 0 && r == 0 && c == 0) break;
		vector<vector<vector<char>>> board(l, vector<vector<char>>(r, vector<char>(c)));
		vector<vector<vector<bool>>> found(l, vector<vector<bool>>(r, vector<bool>(c, false)));
		Info start, end;
		for (int i = 0; i < l; i++) {
			for (int j = 0; j < r; j++) {
				string str;
				cin >> str;
				for (int k = 0; k < c; k++) {
					board[i][j][k] = str[k];
					if (str[k] == 'S') {
						start = { i,j,k,0 };
						found[i][j][k] = true;
					}
					if (str[k] == 'E') end = { i,j,k,0 };
				}
			}
		}
		queue<Info> q;
		q.push(start);
		int ans = -1;
		while (!q.empty()) {
			auto curr = q.front();
			if (curr.h == end.h && curr.r == end.r && curr.c == end.c) {
				ans = curr.cnt;
				break;
			}
			q.pop();
			for (int i = 0; i < 6; i++) {
				int nextH = curr.h + dHeight[i];
				int nextRow = curr.r + dRow[i];
				int nextCol = curr.c + dCol[i];
				if (nextH < 0 || nextH >= l) continue;
				if (nextRow < 0 || nextRow >= r) continue;
				if (nextCol < 0 || nextCol >= c) continue;
				if (board[nextH][nextRow][nextCol] == '#') continue;
				if (found[nextH][nextRow][nextCol]) continue;
				found[nextH][nextRow][nextCol] = true;
				q.push({ nextH,nextRow,nextCol,curr.cnt + 1 });
			}
		}
		if (ans == -1) {
			cout << "Trapped!" << "\n";
		}
		else {
			cout << "Escaped in " << ans << " minute(s)." << "\n";
		}
	}
};

'알고리즘' 카테고리의 다른 글

백준 12904번 A와 B C++  (0) 2022.03.18
백준 1405번 미친 로봇 C++  (0) 2022.03.17
백준 9084번 동전 C++  (0) 2022.03.15
백준 13023번 ABCDE C++  (0) 2022.03.14
백준 14226번 이모티콘 C++  (0) 2022.03.13