[BaekJoon] 7569번 - 토마토 [Java][C++]
[BaekJoon] 7569번 - 토마토 [Java][C++]
1. 문제 풀이
BaekJoon 7576번 - 토마토 문제에서 토마토가 수직으로도 쌓여있는 문제로 육방탐색을 활용하기만 하면 동일한 아이디어로 해결할 수 있다.
2. 코드
1. 풀이 [Java]
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
import java.io.*;
import java.util.*;
public class Main {
static final int[] dh = {0, 0, 0, 0, -1, 1};
static final int[] dr = {-1, 0, 1, 0, 0, 0};
static final int[] dc = {0, 1, 0, -1, 0, 0};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int M = Integer.parseInt(st.nextToken());
int N = Integer.parseInt(st.nextToken());
int H = Integer.parseInt(st.nextToken());
int[][][] map = new int[H][N][M];
Queue<int[]> q = new ArrayDeque<>();
boolean[][][] visited = new boolean[H][N][M];
int cnt = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < N; j++) {
st = new StringTokenizer(br.readLine());
for (int k = 0; k < M; k++) {
map[i][j][k] = Integer.parseInt(st.nextToken());
if (map[i][j][k] == 1) {
q.offer(new int[]{i, j, k});
visited[i][j][k] = true;
} else if (map[i][j][k] == 0) {
cnt++;
}
}
}
}
if (cnt == 0) {
System.out.println(cnt);
} else {
int result = bfs(q, visited, cnt, H, N, M, map);
System.out.println(result);
}
}
static int bfs(Queue<int[]> q, boolean[][][] visited, int cnt, int H, int N, int M, int[][][] map) {
int dist = 0;
while (!q.isEmpty()) {
int size = q.size();
while (size-- > 0) {
int[] node = q.poll();
for (int d = 0; d < 6; d++) {
int nh = node[0] + dh[d];
int nr = node[1] + dr[d];
int nc = node[2] + dc[d];
if (nh < 0 || nh >= H || nr < 0 || nr >= N || nc < 0 || nc >= M) continue;
if (map[nh][nr][nc] != 0 || visited[nh][nr][nc]) continue;
q.offer(new int[]{nh, nr, nc});
visited[nh][nr][nc] = true;
cnt--;
}
}
dist++;
if (cnt == 0) return dist;
}
return -1;
}
}
2. 풀이 [C++]
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
#include <bits/stdc++.h>
using namespace std;
int dh[6] = {0, 0, 0, 0, -1, 1};
int dr[6] = {-1, 0, 1, 0, 0, 0};
int dc[6] = {0, 1, 0, -1, 0, 0};
struct Node {
int h, r, c;
};
int H, n, m;
int grid[100][100][100];
bool visited[100][100][100];
queue<Node> q;
int bfs(int cnt) {
int dist = 0;
while (!q.empty()) {
int sz = q.size();
while (sz--) {
auto [h, r, c] = q.front();
q.pop();
for (int d = 0; d < 6; d++) {
int nh = h + dh[d];
int nr = r + dr[d];
int nc = c + dc[d];
if (nh < 0 || nh >= H || nr < 0 || nr >= n || nc < 0 || nc >= m) continue;
if (grid[nh][nr][nc] != 0 || visited[nh][nr][nc]) continue;
q.push({nh, nr, nc});
visited[nh][nr][nc] = true;
cnt--;
}
}
dist++;
if (cnt == 0) return dist;
}
return -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> m >> n >> H;
int cnt = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
cin >> grid[i][j][k];
if (grid[i][j][k] == 1) {
q.push({i, j, k});
visited[i][j][k] = true;
} else if (grid[i][j][k] == 0) {
cnt++;
}
}
}
}
if (cnt == 0) {
cout << cnt;
} else {
int res = bfs(cnt);
cout << res;
}
}
This post is licensed under CC BY 4.0 by the author.