7569. 토마토

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




1. 접근


http://js1jj2sk3.tistory.com/59 이랑 똑같다.


맵만 삼차원으로 바뀌었음.



2. 풀이


방향만 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
71
72
73
74
#include <stdio.h>
#include <queue>
using namespace std;
 
int m, n, h, cnt;
int tomato[100][100][100];
int visit[100][100][100];
int dir[6][3= { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };
 
struct col {
    int x, y, z;
};
queue<col> q;
 
int bfs() {
    int ans = 1;
    while (!q.empty()) {
        int size = q.size();
        for (int i = 0; i < size++i) {
            col c1 = q.front();
            q.pop();
            int x = c1.x, y = c1.y, z = c1.z;
 
            if (visit[x][y][z])
                continue;
            else
                visit[x][y][z] = 1;
 
            for (int j = 0; j < 6++j) {
                int xx = x + dir[j][0];
                int yy = y + dir[j][1];
                int zz = z + dir[j][2];
 
                if (xx < 0 || xx >= h)
                    continue;
                if (yy < 0 || yy >= n)
                    continue;
                if (zz < 0 || zz >= m)
                    continue;
                if (tomato[xx][yy][zz] == -1)
                    continue;
 
                if (tomato[xx][yy][zz] == 0) {
                    q.push({ xx,yy,zz });
                    tomato[xx][yy][zz] = 1;
                    cnt--;
                }
                
                if (cnt == 0)
                    return ans;
            }
        }
        ans++;
    }
    return -1;
}
 
int main() {
    scanf("%d %d %d"&m, &n, &h);
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < n; ++j) {
            for (int k = 0; k < m; ++k) {
                scanf("%d"&tomato[i][j][k]);
                if (tomato[i][j][k] == 0)
                    cnt++;
                else if (tomato[i][j][k] == 1)
                    q.push({ i,j,k });
            }
        }
    }
    printf("%d\n", bfs());
 
    return 0;
}
cs



'알고리즘 > 브루트 포스' 카테고리의 다른 글

백준) 2606 바이러스  (0) 2017.10.09
백준) 2178 미로 탐색  (3) 2017.10.09
백준) 7576 토마토  (0) 2017.09.28
백준) 9466 텀 프로젝트  (0) 2017.09.03
백준) 1194 달이 차오른다, 가자.  (0) 2017.07.07

+ Recent posts