2021-06-16 · 2

BOJ 7569 — Tomato

algorithmboj

This post is over 2 years old. The content may be outdated.

https://www.acmicpc.net/problem/7569 <- the problem-solving site


#include <cstdio>
#include <iostream>
#include <queue>
#include <tuple>
 
#define endly "\n"
using namespace std;
 
int d[100][100][100];
bool visited[100][100][100];
 
int dx[] = {1, -1, 0, 0, 0, 0};
int dy[] = {0, 0, 1, -1, 0, 0};
int dz[] = {0, 0, 0, 0, 1, -1};
queue<tuple<int, int, int>> q;

int main() {

    int m, n, h;
    cin >> m >> n >> h;
    bool all_one;
    for (int z = 0; z < h; z++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> d[i][j][z];
                // check whether 0 was entered
                if (d[i][j][z] == 0) all_one = true;
                if (d[i][j][z] == 1) {
                    q.push(make_tuple(i, j, z));
                    visited[i][j][z] = true;
                }
            }
        }
    }
    // if there is no input of 0
    if (!all_one) {
        printf("0");
        return 0;
    }
    while (!q.empty()) {
        int x, y, z;
        tie(x, y, z) = q.front();
        q.pop();
        for (int k = 0; k < 6; k++) {
            int nx = x + dx[k];
            int ny = y + dy[k];
            int nz = z + dz[k];
            if (nx >= 0 && nx < n && ny >= 0 && ny < m && nz >= 0 && nz < h) {
                if (d[nx][ny][nz] == 0 && !visited[nx][ny][nz]) {
                    q.push(make_tuple(nx, ny, nz));
                    visited[nx][ny][nz] = true;
                    d[nx][ny][nz] = d[x][y][z] + 1;
                }
            }
        }
    }
    int ans = 0;
    for (int z = 0; z < h; z++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (d[i][j][z] == 0) { // if there is an unripe tomato
                    printf("-1");
                    return 0;
                }
                if (ans < d[i][j][z]) ans = d[i][j][z];
            }
        }
    }
    printf("%d", ans-1);
    return 0;
}

Original (Korean): tistory — published 2021-06-16, migrated to this blog. This translation was generated with the help of AI.

Comments

Delete this comment?

Related posts

BOJ 7569 — Tomato · 나봄하랑