[ ALGORITHM ]/[ 백 준 ]
[BAEKJOON] 7569번 : 토 마 토
HiStar__
2020. 9. 3. 12:20
문 제
소 스 코 드
#include <iostream>
#include <queue>
#include <algorithm>
constexpr int MAX{ 100 };
struct Spread {
int x, y, z, level;
};
int field[MAX][MAX][MAX]{ {{0,},}, };
bool SearchNot(const int&, const int&, const int&);
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int M, N, H;
std::queue<Spread> spr;
std::cin >> M >> N >> H;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < N; ++j) {
for (int k = 0; k < M; ++k) {
std::cin >> field[i][j][k];
if (1 == field[i][j][k]) spr.push({ i, j, k, 0 });
}
}
}
int day{ -1 };
while (!spr.empty()) {
Spread temp{ spr.front() };
spr.pop();
day = std::max(day, temp.level);
//
if ((temp.x > 0) && (0 == field[temp.x - 1][temp.y][temp.z])) {
field[temp.x - 1][temp.y][temp.z] = 1;
spr.push({ temp.x - 1, temp.y, temp.z, temp.level + 1 });
}
if ((temp.x < H - 1) && (0 == field[temp.x + 1][temp.y][temp.z])) {
field[temp.x + 1][temp.y][temp.z] = 1;
spr.push({ temp.x + 1, temp.y, temp.z, temp.level + 1 });
}
if ((temp.y > 0) && (0 == field[temp.x][temp.y - 1][temp.z])) {
field[temp.x][temp.y - 1][temp.z] = 1;
spr.push({ temp.x, temp.y - 1, temp.z, temp.level + 1 });
}
if ((temp.y < N - 1) && (0 == field[temp.x][temp.y + 1][temp.z])) {
field[temp.x ][temp.y + 1][temp.z] = 1;
spr.push({ temp.x, temp.y + 1, temp.z, temp.level + 1 });
}
if ((temp.z > 0) && (0 == field[temp.x][temp.y][temp.z - 1])) {
field[temp.x][temp.y][temp.z - 1] = 1;
spr.push({ temp.x, temp.y, temp.z - 1, temp.level + 1 });
}
if ((temp.z < M - 1) && (0 == field[temp.x][temp.y][temp.z + 1])) {
field[temp.x][temp.y][temp.z + 1] = 1;
spr.push({ temp.x, temp.y, temp.z + 1, temp.level + 1 });
}
}
if (SearchNot(M, N, H)) std::cout << day << '\n';
else std::cout << "-1 \n";
return 0;
}
bool SearchNot(const int& M, const int& N, const int& H) {
for (int i = 0; i < H; ++i) {
for (int j = 0; j < N; ++j) {
for (int k = 0; k < M; ++k) {
if (0 == field[i][j][k]) return false;
}
}
}
return true;
}
풀 이
더보기
" [백준] 7576번 : 토 마 토 "의 경우에는 2차원으로 이루어 져있는데, 이 문제의 경우에는 3차원의 크기를 가지고 있다. 이 문제의 경우 2차원으로 이루어진 토마토 문제에서 위, 아래 즉 z축을 판단하는 조건문을 추가하면 간단하게 만들 수 있다.
출 력 값
문 제 출 처
문제 링크 : www.acmicpc.net/problem/7569
7569번: 토마토
첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,
www.acmicpc.net