뜌릅

토마토 7569번 [백준] 본문

알고리즘/PS 문제

토마토 7569번 [백준]

TwoCastle9 2022. 8. 28. 21:11
반응형

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

 

7569번: 토마토

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

www.acmicpc.net

7576번 https://twocastle9.tistory.com/36 와 매우 유사한 문제이다. 위 아래의 방향도 고려하게 되었으나, 사실상 약간의 변형을 하면 되기에 큰 문제가 되지 않는다.

#include<bits/stdc++.h>
#include<sys/types.h>


using namespace std;
#define endl '\n'
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i, j) for(ll i=0;i<j;i++)
#define pii pair<int,int>
#define v(type) vector<type>
#define ff first
#define ss second

int dx[] = {-1, 1, 0, 0, 0, 0};
int dy[] = {0, 0, -1, 1, 0, 0};
int dz[] = {0, 0, 0, 0, 1, -1};

void solve() {
    int m, n, h;
    cin >> m >> n >> h;
    v(v(v(int))) box(h, v(v(int))(n, v(int)(m)));
    v(v(v(bool))) isVisited(h, v(v(bool))(n, v(bool)(m,false)));

    rep(i, h)rep(j, n)rep(k, m)cin >> box[i][j][k];
    queue<pair<pii, pii>> q;
    rep(i, h)rep(j, n)rep(k, m)
                if (box[i][j][k] == 1) {
                    q.push({{i, j},
                            {k, 0}});
                    isVisited[i][j][k] = true;
                }

    int ans = 0;
    while (!q.empty()) {
        int _z = q.front().ff.ff;
        int _y = q.front().ff.ss;
        int _x = q.front().ss.ff;
        int cur = q.front().ss.ss;
        ans = max(cur, ans);
        q.pop();

        rep(i, 6) {
            int x = _x + dx[i];
            int y = _y + dy[i];
            int z = _z + dz[i];
            if (x < 0 || x >= m || y < 0 || y >= n || z < 0 || z >= h)
                continue;
            if (!isVisited[z][y][x] && box[z][y][x] == 0) {
                box[z][y][x] = 1;
                isVisited[z][y][x] = true;
                q.push({{z, y},
                        {x, cur + 1}});
            }
        }
    }
    rep(i,h) {
        rep(j, n) {
            rep(k, m) {
                if (box[i][j][k] == 0) {
                    cout << -1 << endl;
                    return;
                }
            }
        }
    }
    cout << ans << endl;
}
int main() {
    fast_io;
    solve();
    return 0;
}
반응형

'알고리즘 > PS 문제' 카테고리의 다른 글

DSLR 9019번 [백준]  (0) 2022.08.30
이중 우선순위 큐 7662번 [백준]  (1) 2022.08.29
토마토 7576번 [백준]  (0) 2022.08.28
Integer List(AC) 5430번 [백준]  (0) 2022.08.27
Z 1074번 [백준]  (0) 2022.08.26