백준/5000~
[백준 / C#] 7569 : 토마토
무오_
2023. 6. 28. 18:27
using System;
using System.Collections.Generic;
using System.Linq;
struct Tomato
{
public int x, y, z;
public Tomato(int h, int i, int j)
{
y = i;
x = j;
z = h;
}
}
class Program
{
static int[] M_N_Z = Console.ReadLine().Split().Select(int.Parse).ToArray();
static int m = M_N_Z[0], n = M_N_Z[1], h = M_N_Z[2], result = 0;
static int[] dx = { 1, 0, 0, -1, 0, 0 }, dy = { 0, 1, 0, 0, -1, 0 }, dz = { 0, 0, 1, 0, 0, -1 };
static int[,,] box = new int[h + 1, n + 1, m + 1];
static Queue<Tomato> q = new Queue<Tomato>();
static void BFS()
{
while (!q.Count.Equals(0))
{
int y = q.Peek().y;
int x = q.Peek().x;
int z = q.Peek().z;
q.Dequeue();
for (int i = 0; i < 6; i++)
{
int newY = y + dy[i];
int newX = x + dx[i];
int newZ = z + dz[i];
if (0 <= newX && newX < m && 0 <= newY && newY < n
&& 0 <= newZ && newZ < h && box[newZ,newY, newX] == 0)
{
box[newZ,newY, newX] = box[z,y, x] + 1;
q.Enqueue(new Tomato(newZ, newY, newX));
}
}
}
}
static void Soulution()
{
BFS();
for (int t = 0; t < h; t++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (box[t, i, j] == 0)
{
Console.WriteLine("-1");
return;
}
if (result < box[t,i, j])
result = box[t,i, j];
}
}
}
Console.WriteLine(result - 1);
}
static void Input()
{
for (int t = 0; t < h; t++)
{
for (int i = 0; i < n; i++)
{
int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
for (int j = 0; j < m; j++)
{
box[t, i, j] = input[j];
if (box[t,i, j] == 1)
q.Enqueue(new Tomato(t, i, j));
}
}
}
}
static void Solve()
{
Input();
Soulution();
}
static void Main(string[] args)
{
Solve();
}
}