본문 바로가기

BOJ 7569번 : 토마토 (3차원) 본문

BOJ

BOJ 7569번 : 토마토 (3차원)

00rigin 2020. 4. 19. 21:33

이전 글의 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
int arr[102][102][102= { 0, };
int chk[102][102][102= { 0, };
int _max_ = 0;
int cnt = 0;
queue<int>qx;
queue<int>qy;
queue<int>qz;
 
void bfs() {
 
    while (!qx.empty()) {
        int temp_x = qx.front();
        int temp_y = qy.front();
        int temp_z = qz.front();
        qx.pop();
        qy.pop();
        qz.pop();
 
        _max_ = (_max_ <= chk[temp_z][temp_y][temp_x]) ? chk[temp_z][temp_y][temp_x] : _max_;
 
        if (!chk[temp_z][temp_y + 1][temp_x] && !arr[temp_z][temp_y + 1][temp_x]) { // 아래
            chk[temp_z][temp_y + 1][temp_x] = chk[temp_z][temp_y][temp_x] + 1;
            cnt++;
            qx.push(temp_x);
            qy.push(temp_y + 1);
            qz.push(temp_z);
        }
        if (!chk[temp_z][temp_y][temp_x + 1&& !arr[temp_z][temp_y][temp_x + 1]) { // 오른
            chk[temp_z][temp_y][temp_x + 1= chk[temp_z][temp_y][temp_x] + 1;
            cnt++;
            qx.push(temp_x + 1);
            qy.push(temp_y);
            qz.push(temp_z);
        }
        if (!chk[temp_z][temp_y - 1][temp_x] && !arr[temp_z][temp_y - 1][temp_x]) { // 위
            chk[temp_z][temp_y - 1][temp_x] = chk[temp_z][temp_y][temp_x] + 1;
            cnt++;
            qx.push(temp_x);
            qy.push(temp_y - 1);
            qz.push(temp_z);
        }
        if (!chk[temp_z][temp_y][temp_x - 1&& !arr[temp_z][temp_y][temp_x - 1]) { // 왼
            chk[temp_z][temp_y][temp_x - 1= chk[temp_z][temp_y][temp_x] + 1;
            cnt++;
            qx.push(temp_x - 1);
            qy.push(temp_y);
            qz.push(temp_z);
        }
 
        if (!chk[temp_z - 1][temp_y][temp_x] && !arr[temp_z - 1][temp_y][temp_x]) { // z+
            chk[temp_z - 1][temp_y][temp_x] = chk[temp_z][temp_y][temp_x] + 1;
            cnt++;
            qx.push(temp_x);
            qy.push(temp_y);
            qz.push(temp_z - 1);
        }
        if (!chk[temp_z + 1][temp_y][temp_x] && !arr[temp_z + 1][temp_y][temp_x]) { // z-
            chk[temp_z + 1][temp_y][temp_x] = chk[temp_z][temp_y][temp_x] + 1;
            cnt++;
            qx.push(temp_x);
            qy.push(temp_y);
            qz.push(temp_z + 1);
        }
    }
 
}
 
int main() {
    memset(arr, -1sizeof(arr));
    memset(chk, 0sizeof(chk));
    int m, n, h;
    cin >> n >> m >> h;
    
    for (int k = 1; k <= h; k++) {
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                cin >> arr[k][i][j];
                if (arr[k][i][j] == 1) {
                    qx.push(j);
                    qy.push(i);
                    qz.push(k);
                    cnt++;
                }
                else if (arr[k][i][j] == -1) {
                    chk[k][i][j] = 1;
                    cnt++;
                }
            }
        }
    }
    
    if (qx.empty())
        cout << -1 << endl;
    else if (qx.size() == m * n * h)
        cout << 0 << endl;
    else {
        bfs();
        if (cnt == m * n * h)
            cout << _max_ << endl;
        else
            cout << -1 << endl;
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

<문제>

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

 

7569번: 토마토

첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100, 1 ≤ H ≤ 100 이다. 둘째 줄부터는 가장 밑의 상자부터 가장 위의 상자까지에 저장된 토마토들의 정보가 주어진다. 즉, 둘째 줄부터 N개의 줄에는 하나의 상자에 담긴 토마토의 정보가 주어진다. 각 줄에는 상자 가로줄에 들어있는 토마

www.acmicpc.net

 

'BOJ' 카테고리의 다른 글

BOJ 2206번 : 벽 부수고 이동하기  (0) 2020.04.26
BOJ 1697번 : 숨바꼭질  (0) 2020.04.21
BOJ 7576번 : 토마토  (0) 2020.04.19
BOJ 2178번 : 미로탐색  (0) 2020.04.14
BOJ 2667번 : 단지번호붙이기  (0) 2020.04.14
Comments