7569번: 토마토

myeongrangcoding·2023년 12월 20일
0

백준

목록 보기
35/47

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

풀이

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <queue>

using namespace std;

struct Data
{
	int Row, Col, Height, Days;
	Data(int Row, int Col, int Height, int Days)
	{
		this->Row = Row;
		this->Col = Col;
		this->Height = Height;
		this->Days = Days;
	}
};

int Boxes[100][100][100];
int N{}, M{}, H{};
queue<Data> Q;
int Answer;

void BFS()
{
	int DirRow[6]{-1, 0, 1, 0, 0, 0};
	int DirCol[6]{0, 1, 0, -1, 0, 0};
	int DirHeight[6]{0, 0, 0, 0, -1, 1};

	while (!Q.empty())
	{
		Data CurrentData = Q.front();
		Q.pop();

		Answer = CurrentData.Days;

		for (int i = 0; i < 6; ++i)
		{
			int CheckRow = CurrentData.Row + DirRow[i];
			int CheckCol = CurrentData.Col + DirCol[i];
			int CheckHeight = CurrentData.Height + DirHeight[i];

			if (CheckRow < 0 || CheckCol < 0 || CheckHeight < 0 ||
				CheckRow >= N || CheckCol >= M || CheckHeight >= H)
			{
				continue;
			}

			if (-1 == Boxes[CheckRow][CheckCol][CheckHeight]||
				1 == Boxes[CheckRow][CheckCol][CheckHeight])
			{
				continue;
			}

			Q.push(Data(CheckRow, CheckCol, CheckHeight, CurrentData.Days + 1));
			Boxes[CheckRow][CheckCol][CheckHeight] = 1;
		}
	}
}

bool IsFruitsRipe()
{
	for (int h = 0; h < H; ++h)
	{
		for (int n = 0; n < N; ++n)
		{
			for (int m = 0; m < M; ++m)
			{
				if (0 == Boxes[n][m][h])
				{
					return false;
				}
			}
		}
	}

	return true;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cout.tie(nullptr);
	cin.tie(nullptr);
	//freopen("input.txt", "rt", stdin);

	cin >> M >> N >> H;

	for (int h = 0; h < H; ++h)
	{
		for (int n = 0; n < N; ++n)
		{
			for (int m = 0; m < M; ++m)
			{
				cin >> Boxes[n][m][h];
				if (1 == Boxes[n][m][h])
				{
					Q.push(Data(n, m, h, 0));
					Boxes[n][m][h] = 1;
				}
			}
		}
	}

	BFS();

	if (IsFruitsRipe())
	{
		cout << Answer;
	}
	else
	{
		cout << -1;
	}

	return 0;
}
profile
명랑코딩!

0개의 댓글