#if 1
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
#include <queue>
#include <vector>
#include <unordered_map>
#define MAXN 201
using namespace std;
int H, R, C, tomatoCnt;
int map(MAXN)(MAXN)(MAXN);
struct Data {
int h, r, c, lev;
};
queue<Data> que;
int dh() = { -1,1,0,0,0,0 };
int dr() = { 0,0,-1,1,0,0 };
int dc() = { 0,0,0,0,-1,1 };
void push(int h, int r, int c, int lv)
{
if (map(h)(r)(c) == 0) return;
tomatoCnt--;
map(h)(r)(c) = 0;
que.push({ h,r,c,lv });
}
void input()
{
cin >> C >> R >> H;
for (int i = 1; i <= H; i++)
{
for (int j = 1; j <= R; j++)
{
for (int k = 1; k <= C; k++)
{
cin >> map(i)(j)(k);
map(i)(j)(k)++;
if (map(i)(j)(k))
tomatoCnt++;
if (map(i)(j)(k) == 2)
{
push(i, j, k, 0);
}
}
}
}
}
int BFS()
{
Data tmp;
while (!
que.empty())
{
tmp = que.front();
que.pop();
for (int i = 0; i < 6; i++)
{
push(tmp.h + dh(i), tmp.r + dr(i), tmp.c + dc(i), tmp.lev + 1);
}
}
if (tomatoCnt) return -1;
return tmp.lev;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#if _WIN64
freopen("input.txt", "r", stdin);
#endif
input();
cout<<BFS() << '\n';
}
#endif
토마토 문제입니다.
BFS를 통해 전체를 검색하여 최소값을 찾는 것은 문제라고 볼 수 있습니다.
https://www.acmicpc.net/problem/7569