ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [7569] 토마토
    BOJ 2021. 10. 16. 09:51

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

     

    7569번: 토마토

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

    www.acmicpc.net

    <문제>

    2차원에서 방향데이터 2개, 각각 4방향이였다면

    3차원에서는 방향데이터 3개, 각각 6방향으로 구현하면 된다.

     

    3차원인것을 제외하면 크게 달라진 점은 없다.

    인덱스 헷갈려서 실수하기 쉬우므로, 순서에 유의하여 구현해야 하겠다 

    <소스코드>

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    #include <limits.h>
    #include <stdio.h>
     
    #include <algorithm>
    #include <queue>
    using namespace std;
    queue<int> q;
    int n, m, height, a[101][101][101], dp[101][101][101];
    int dy[6= {010-100}, dx[6= {10-1000},
        dh[6= {00001-1};
    void f(void) {
        int ty, tx, th, y, x, h, i;
        while (!q.empty()) {
            th = q.front();
            q.pop();
            ty = q.front();
            q.pop();
            tx = q.front();
            q.pop();
            for (i = 0; i < 6; i++) {
                h = th + dh[i];
                y = ty + dy[i];
                x = tx + dx[i];
                if (y < 0 || x < 0 || h < 0 || y >= n || x >= m || h >= height)
                    continue;
                if (dp[h][y][x] >= 0continue;
                if (a[h][y][x] == 0) {
                    q.push(h);
                    q.push(y);
                    q.push(x);
                    dp[h][y][x] = dp[th][ty][tx] + 1;
                    a[h][y][x] = 1;
                }
            }
        }
        return;
    }
    int main(void) {
        int i, j, k;
        scanf("%d %d %d"&m, &n, &height);
        for (i = 0; i < height; i++) {
            for (j = 0; j < n; j++) {
                for (k = 0; k < m; k++) {
                    scanf("%d"&a[i][j][k]);
                    if (a[i][j][k] == 1) {
                        q.push(i);
                        q.push(j);
                        q.push(k);
                    }
                    if (a[i][j][k] == 0) dp[i][j][k] = -1;
                }
            }
        }
        int ans = 0;
        f();
        for (i = 0; i < height; i++) {
            for (j = 0; j < n; j++) {
                for (k = 0; k < m; k++) {
                    if (dp[i][j][k] == -1) {
                        ans = -1;
                        goto allBreak;
                    }
                    ans = max(ans, dp[i][j][k]);
                }
            }
        }
    allBreak:
        printf("%d", ans);
        return 0;
    }
    cs

     

    'BOJ' 카테고리의 다른 글

    [2178] 미로 탐색  (0) 2021.10.16
    [4963] 섬의 개수  (0) 2021.10.16
    [2667] 단지번호붙이기  (0) 2021.10.16
    [16946] 벽 부수고 이동하기 4  (0) 2021.10.16
    [15903] 카드 합체 놀이  (0) 2021.10.14

    댓글