[백준] 7569번 C/C++ 풀이 _ 토마토



시간 제한메모리 제한제출정답맞은 사람정답 비율
1 초128 MB77672323167929.982%

문제

철수의 토마토 농장에서는 토마토를 보관하는 큰 창고를 가지고 있다. 토마토는 아래의 그림과 같이 격자모양 상자의 칸에 하나씩 넣은 다음, 상자들을 수직으로 쌓아 올려서 창고에 보관한다

창고에 보관되는 토마토들 중에는 잘 익은 것도 있지만, 아직 익지 않은 토마토들도 있을 수 있다. 보관 후 하루가 지나면, 익은 토마토들의 인접한 곳에 있는 익지 않은 토마토들은 익은 토마토의 영향을 받아 익게 된다. 하나의 토마토에 인접한 곳은 위, 아래, 왼쪽, 오른쪽, 앞, 뒤 여섯 방향에 있는 토마토를 의미한다. 대각선 방향에 있는 토마토들에게는 영향을 주지 못하며, 토마토가 혼자 저절로 익는 경우는 없다고 가정한다. 철수는 창고에 보관된 토마토들이 며칠이 지나면 다 익게 되는지 그 최소 일수를 알고 싶어 한다.

토마토를 창고에 보관하는 격자모양의 상자들의 크기와 익은 토마토들과 익지 않은 토마토들의 정보가 주어졌을 때, 며칠이 지나면 토마토들이 모두 익는지, 그 최소 일수를 구하는 프로그램을 작성하라. 단, 상자의 일부 칸에는 토마토가 들어있지 않을 수도 있다

입력

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

출력

여러분은 토마토가 모두 익을 때까지 최소 며칠이 걸리는지를 계산해서 출력해야 한다. 만약, 저장될 때부터 모든 토마토가 익어있는 상태이면 0을 출력해야 하고, 토마토가 모두 익지는 못하는 상황이면 -1 을 출력해야 한다.

예제 입력 

5 3 1
0 -1 0 0 0
-1 -1 0 1 1
0 0 0 1 1

예제 출력 

-1

힌트

알고리즘 분류




>> 개선하면 좋을 사항

- 다른 코드의 경우에는 상,하,좌,우,위,아래를 동일한 소스코드로 사용하고, for문을 통해서 해당 숫자를 집어넣었다. 
내가 작성한 코드는 중복이 상당히 많이 되기 때문에, 향후 개선사항으로 넣을 필요가 있어보인다. 


>> 소스코드

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <iostream>
#include <queue>
using namespace std;
 
typedef struct bfs_struct {
    int x;
    int y;
    int z; 
    int depth;
}bfs_struct;
 
int*** tomato_arr;
int col, row, high;
int not_ripe_tomato, ripe_tomato, none_tomato;
queue<bfs_struct*> bfs_queue;
int highest_depth = 0;
 
//void print() {
//    cout << endl;
//    cout << "The depth is " << highest_depth << endl;
//    // 토마토 개수 세기 
//
//    for (int k = 0; k < high; k++) {
//        for (int i = 0; i < row; i++) {
//            for (int j = 0; j < col; j++) {
//                cout << tomato_arr[i][j][k] << " "; 
//            }
//            cout << endl;
//        }
//        cout << endl;
//    }
//}
 
void bfs() { 
    // 익어 있는 얘들을 큐에다가 전부 넣어줍니다 
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            for (int k = 0; k < high; k++) {
                if (tomato_arr[i][j][k] == 1) {
                    bfs_struct* sub_struct = (bfs_struct*)malloc(sizeof(bfs_struct));
                    sub_struct->= i;
                    sub_struct->= j;
                    sub_struct->= k;
                    sub_struct->depth = 0;
                    bfs_queue.push(sub_struct);
                }
            }
        }
    }
 
    while (bfs_queue.size()) {
        //print();
 
        // 하나를 뽑아서 선택하고
        bfs_struct* this_round_struct = bfs_queue.front();
        bfs_queue.pop();
 
        // queue 에 있는 얘들 주변 얘들을 뽑아서 1로 만들어주고 큐에다 넣는다. 
        // 상
        if ((this_round_struct->- 1 >= 0&& (tomato_arr[this_round_struct->- 1][this_round_struct->y][this_round_struct->z] == 0)) {
            // 1로 변경 
            tomato_arr[this_round_struct->- 1][this_round_struct->y][this_round_struct->z] = 1;
 
            // 새로운 구조체 생성 후에 큐에 넣어준다. 
            bfs_struct* sub_struct = (bfs_struct*)malloc(sizeof(bfs_struct));
            sub_struct->= this_round_struct->- 1;
            sub_struct->= this_round_struct->y;
            sub_struct->= this_round_struct->z;
            sub_struct->depth = this_round_struct->depth + 1;
            bfs_queue.push(sub_struct);
 
            // 최고 높이 변경하고 익은 토마토 개수 변경 
            if (sub_struct->depth > highest_depth) highest_depth = sub_struct->depth;
            ripe_tomato++;
            not_ripe_tomato--;
        }
 
        // 하
        if ((this_round_struct->+ 1 < row) && (tomato_arr[this_round_struct->+ 1][this_round_struct->y][this_round_struct->z] == 0)) {
            // 1로 변경 
            tomato_arr[this_round_struct->+ 1][this_round_struct->y][this_round_struct->z] = 1;
 
            // 새로운 구조체 생성 후에 큐에 넣어준다. 
            bfs_struct* sub_struct = (bfs_struct*)malloc(sizeof(bfs_struct));
            sub_struct->= this_round_struct->+ 1;
            sub_struct->= this_round_struct->y;
            sub_struct->= this_round_struct->z;
            sub_struct->depth = this_round_struct->depth + 1;
            bfs_queue.push(sub_struct);
 
            // 최고 높이 변경하고 익은 토마토 개수 변경 
            if (sub_struct->depth > highest_depth) highest_depth = sub_struct->depth;
            ripe_tomato++;
            not_ripe_tomato--;
        }
 
        // 좌
        if ((this_round_struct->- 1 >= 0&& (tomato_arr[this_round_struct->x][this_round_struct->- 1][this_round_struct->z] == 0)) {
            // 1로 변경 
            tomato_arr[this_round_struct->x][this_round_struct->- 1][this_round_struct->z] = 1;
 
            // 새로운 구조체 생성 후에 큐에 넣어준다. 
            bfs_struct* sub_struct = (bfs_struct*)malloc(sizeof(bfs_struct));
            sub_struct->= this_round_struct->x;
            sub_struct->= this_round_struct->- 1;
            sub_struct->= this_round_struct->z;
            sub_struct->depth = this_round_struct->depth + 1;
            bfs_queue.push(sub_struct);
 
            // 최고 높이 변경하고 익은 토마토 개수 변경 
            if (sub_struct->depth > highest_depth) highest_depth = sub_struct->depth;
            ripe_tomato++;
            not_ripe_tomato--;
        }
 
        // 우
        if ((this_round_struct->+ 1 < col) && (tomato_arr[this_round_struct->x][this_round_struct->+ 1][this_round_struct->z] == 0)) {
            // 1로 변경 
            tomato_arr[this_round_struct->x][this_round_struct->+ 1][this_round_struct->z] = 1;
 
            // 새로운 구조체 생성 후에 큐에 넣어준다. 
            bfs_struct* sub_struct = (bfs_struct*)malloc(sizeof(bfs_struct));
            sub_struct->= this_round_struct->x;
            sub_struct->= this_round_struct->+ 1;
            sub_struct->= this_round_struct->z;
            sub_struct->depth = this_round_struct->depth + 1;
            bfs_queue.push(sub_struct);
 
            // 최고 높이 변경하고 익은 토마토 개수 변경 
            if (sub_struct->depth > highest_depth) highest_depth = sub_struct->depth;
            ripe_tomato++;
            not_ripe_tomato--;
        }
 
        // 아래
        if ((this_round_struct->- 1 >= 0&& (tomato_arr[this_round_struct->x][this_round_struct->y][this_round_struct->- 1== 0)) {
            // 1로 변경 
            tomato_arr[this_round_struct->x][this_round_struct->y][this_round_struct->- 1= 1;
 
            // 새로운 구조체 생성 후에 큐에 넣어준다. 
            bfs_struct* sub_struct = (bfs_struct*)malloc(sizeof(bfs_struct));
            sub_struct->= this_round_struct->x;
            sub_struct->= this_round_struct->y;
            sub_struct->= this_round_struct->- 1;
            sub_struct->depth = this_round_struct->depth + 1;
            bfs_queue.push(sub_struct);
 
            // 최고 높이 변경하고 익은 토마토 개수 변경 
            if (sub_struct->depth > highest_depth) highest_depth = sub_struct->depth;
            ripe_tomato++;
            not_ripe_tomato--;
        }
 
        // 위 
        if ((this_round_struct->+ 1 < high) && (tomato_arr[this_round_struct->x][this_round_struct->y ][this_round_struct->+ 1== 0)) {
            // 1로 변경 
            tomato_arr[this_round_struct->x][this_round_struct->y][this_round_struct->+ 1= 1;
 
            // 새로운 구조체 생성 후에 큐에 넣어준다. 
            bfs_struct* sub_struct = (bfs_struct*)malloc(sizeof(bfs_struct));
            sub_struct->= this_round_struct->x;
            sub_struct->= this_round_struct->y;
            sub_struct->= this_round_struct->z+1;
            sub_struct->depth = this_round_struct->depth + 1;
            bfs_queue.push(sub_struct);
 
            // 최고 높이 변경하고 익은 토마토 개수 변경 
            if (sub_struct->depth > highest_depth) highest_depth = sub_struct->depth;
            ripe_tomato++;
            not_ripe_tomato--;
        }
 
    }
}
 
int main() {
    cin >> col >> row >> high;
    tomato_arr = (int***)malloc(sizeof(int**)*row);
 
    // 토마토 배열 생성 
    for (int i = 0; i < row; i++){
        tomato_arr[i] = (int**)malloc(sizeof(int*)*col);
        for (int j = 0; j < col; j++) {
            tomato_arr[i][j] = (int*)malloc(sizeof(int)*high);
        }
    }
 
    // 토마토 정보 받아오기
 
    for (int k = 0; k < high; k++)
        for (int i = 0; i < row; i++
            for (int j = 0; j < col; j++
                cin >> tomato_arr[i][j][k];
 
 
    // 토마토 개수 세기 
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            for (int k = 0; k < high; k++) {
                if (tomato_arr[i][j][k] == 1) ripe_tomato++;
                else if (tomato_arr[i][j][k] == 0) not_ripe_tomato++;
                else if (tomato_arr[i][j][k] == -1) none_tomato++;
            }
        }
    }
 
    // 모든 토마토가 익어있으면 0 출력 
    if (ripe_tomato == row * col) {
        cout << 0;
        return 0;
    }
 
    // 모두 익지는 못하는 상황 1가지 처리(나머지 고립되어서 안익을 수 있는 건 따로 처리)
    else if (ripe_tomato == 0) {
        cout << -1;
        return 0;
    }
 
    bfs();
 
    // 다 되었는데 안익었으면 -1을 리턴
    cout << (not_ripe_tomato == 0 ? highest_depth : -1);
 
    return 0;
}
cs