* Computer Science/Algorithm

baekjun 7569. 토마토2

soicem 2018. 10. 5. 23:20

 bfs 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
#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;
 
int M, N, H;
int box[101][101][101];
int visitedList[101][101][101];
 
/*
0 : raw tomato
-1 : empty
1 : cooked tomato
*/
int main(){
    int rawCnt = 0;
    list<vector<int>> que;
    cin >> M >> N >> H;
    for(int h = 1; h <= H; h++){
        for(int i = 1; i <= N; i++)
            for(int j = 1; j <= M; j++){
                cin >> box[h][i][j];
                if(box[h][i][j] == 1){
                    vector<int> tmp = {h, i, j};
                    que.push_front(tmp);
                    visitedList[h][i][j] = 1;
                } else if(box[h][i][j] == 0){
                    visitedList[h][i][j] = 0;
                    rawCnt++;
                } else {
                    visitedList[h][i][j] = -1;
                }
            }
    }
    int day = 0;
    while(rawCnt){
        day++;
        list<vector<int>> buf;
        if(que.empty()){
            cout << -1;
            return 0;
        }
        while(!que.empty()){
            vector<int> ret = que.back();
            int h = ret[0],i = ret[1], j=ret[2];
            que.pop_back();
            if(h - 1 >0){
                if(visitedList[h-1][i][j] == 0){
                    rawCnt--;
                    buf.push_back({h-1, i, j});
                    visitedList[h-1][i][j] = 1;
                }
            }
            if(h +1 <=H){
                if(visitedList[h+1][i][j] == 0){
                    rawCnt--;
                    buf.push_back({h+1, i, j});
                    visitedList[h+1][i][j] = 1;
                }
            }
            if(i-1 > 0){
                if(visitedList[h][i-1][j] == 0){
                    rawCnt--;
                    buf.push_back({h, i-1, j});
                    visitedList[h][i-1][j] = 1;
                }
            }
            if(i + 1 <= N){
                if(visitedList[h][i+1][j] == 0){
                    rawCnt--;
                    buf.push_back({h, i+1, j});
                    visitedList[h][i+1][j] = 1;
                }
            }
            if(j-1 > 0){
                if(visitedList[h][i][j-1== 0){
                    rawCnt--;
                    buf.push_back({h, i, j-1});
                    visitedList[h][i][j-1= 1;
                }
            }
            if(j+1 <= M){
                if(visitedList[h][i][j+1== 0){
                    rawCnt--;
                    buf.push_back({h, i, j+1});
                    visitedList[h][i][j+1= 1;
                }
            }
        }
        que = buf;
    }
 
    cout << day;
    return 0;
}
 
cs


'* Computer Science > Algorithm' 카테고리의 다른 글

baekjun 11048. 이동하기  (0) 2018.10.29
baekjun 바이러스  (0) 2018.10.29
baekjun 7576. 토마토  (0) 2018.10.05
baekjun 조세푸스 수열  (0) 2018.08.27
baekjun 1966. printerQueue  (0) 2018.08.24