나의 브을로오그으

[c++] 7569번 : 토마토 본문

알고리즘/BaekJoon

[c++] 7569번 : 토마토

__jhp_+ 2022. 10. 19. 16:31

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

 

7569번: 토마토

첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,

www.acmicpc.net

#include <iostream>
#include <queue>

using namespace std;

#define RIPE			1
#define NOT_RIPE		0
#define NEVER_RIPE		-1

int tomatoBox[101][101][101] = { 0, };
int M, N, H;

struct Point3 {
	int m;
	int n;
	int h;
};

bool IsNotRipeTomato() {
	for (int h = 1; h <= H; ++h)
	{
		for (int n = 1; n <= N; ++n)
		{
			for (int m = 1; m <= M; ++m)
			{
				if (tomatoBox[h][n][m] == NOT_RIPE)
				{
					return true;
				}
			}
		}
	}

	return false;
}

bool IsBoundOfBox(int m, int n, int h)
{
	return h >= 1 && n >= 1 && m >= 1 && h <= H && n <= N && m <= M;
}

void ChangeTomato(queue<Point3>& tomatoQ, const Point3& _pt)
{
	int h, n, m;
	for (int i = 0; i < 3; ++i)
	{
		for (int j = -1; j < 2; j+=2)
		{
			h = i == 2 ? _pt.h + j : _pt.h;
			n = i == 1 ? _pt.n + j : _pt.n;
			m = i == 0 ? _pt.m + j : _pt.m;
			if (IsBoundOfBox(m, n, h) == true && tomatoBox[h][n][m] == NOT_RIPE)
			{
				tomatoBox[h][n][m] = RIPE;
				tomatoQ.push(Point3{ m, n, h });
			}
		}
	}
	return;
}

int CountOfDaysForRipeTomato(queue<Point3>& tomatoQ) {
	int days = 0;

	while (true)
	{
		int len = tomatoQ.size();
		for (int i = 0; i < len; ++i)
		{
			Point3 tomatoInfo = tomatoQ.front();
			ChangeTomato(tomatoQ, tomatoInfo);
			tomatoQ.pop();
		}

		if (tomatoQ.empty() == true)
		{
			break;
		}
		else
		{
			++days;
		}
	}

	return IsNotRipeTomato() == true ? -1 : days;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	queue<Point3> tomatoQ;
	int days;

	cin >> M >> N >> H;
	for (int h = 1; h <= H; ++h)
	{
		for (int n = 1; n <= N; ++n)
		{
			for (int m = 1; m <= M; ++m)
			{
				cin >> tomatoBox[h][n][m];
				if (tomatoBox[h][n][m] == RIPE)
				{
					tomatoQ.push(Point3{ m, n, h });
				}
			}
		}
	}

	days = CountOfDaysForRipeTomato(tomatoQ);
	cout << days << '\n';

	return 0;
}

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

[c++] 7662번 : 이중 우선순위 큐  (0) 2022.10.26
[c++] 7576번 : 토마토  (0) 2022.10.21
[c++] 18870번 : 좌표 압축  (0) 2022.10.12
[c++] 17626번 : Four Squares  (0) 2022.10.04
[c++] 11727번 : 2 x n 타일링 2  (0) 2022.09.27