티스토리 뷰

 


[BOJ 6593(G4) 리뷰]

이전에 풀었던 토마토 문제와 굉장히 비슷하다.

3D배열을 이용해 풀면된다.

 

 

 


 

#include <iostream>
#include <algorithm>
#include <list>
#include <string>
#include <vector>
#include <stack>
#include <utility>
#include <queue>
#include <deque>
#include <tuple>
using namespace std;

#define X first
#define Y second

char board[32][32][32];
int dist[32][32][32];
int dx[6] = { 1,0,-1,0,0,0 };
int dy[6] = { 0,1,0,-1,0,0 };
int dz[6] = { 0,0,0,0,1,-1 };

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(0);
	while (1)
	{
		int L, R, C;
		cin >> L >> R >> C;
		if (L == 0 && R == 0 && C == 0) return 0;
		queue <pair<int, pair<int, int>>> Q;
		tuple <int, int, int> dest;
		for (int i = 0; i < L; i++)
		{
			for (int j = 0; j < R; j++)
			{
				fill(dist[i][j], dist[i][j] + C, 0);
			}
		}
		for (int i = 0; i < L; i++)
		{
			for (int j = 0; j < R; j++)
			{
				for (int k = 0; k < C; k++)
				{
					cin >> board[i][j][k];
					if (board[i][j][k] == 'S')
					{
						dist[i][j][k] = 1;
						Q.push({ i,{j,k} });
					}
					if (board[i][j][k] == 'E')
					{
						dest = { i,j,k };
					}
				}

			}
		}
		auto cur = Q.front();
		while (!Q.empty())
		{
			cur = Q.front();
			Q.pop();
			if (board[cur.second.X][cur.second.Y][cur.first] == 'E') break;
			for (int dir = 0; dir < 6; dir++)
			{
				int nx = cur.second.X + dx[dir];
				int ny = cur.second.Y + dy[dir];
				int nz = cur.first + dz[dir];
				if (nx < 0 || ny < 0 || nz < 0 || nx >= R || ny >= C || nz >= L) continue;
				if (dist[nz][nx][ny] != 0 || board[nz][nx][ny] == '#') continue;
				dist[nz][nx][ny] = dist[cur.first][cur.second.X][cur.second.Y] + 1;
				Q.push({ nz,{nx,ny} });
			}
		}
		if (dist[get<0>(dest)][get<1>(dest)][get<2>(dest)])
			cout << "Escaped in " << dist[get<0>(dest)][get<1>(dest)][get<2>(dest)] - 1 << " minute(s).\n";
		else
			cout << "Trapped!\n";


	}

}

 

'알고리즘 풀이 > BFS' 카테고리의 다른 글

BOJ : 3055 탈출  (0) 2020.08.05
BOJ : 13913 숨바꼭질 4  (0) 2020.08.05
BOJ 5014 : 스타트링크  (0) 2020.08.04
BOJ 2146 : 다리 만들기  (0) 2020.08.04
BOJ : 4179 불!  (0) 2020.08.03
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함