[BaekJoon] 7576번 - 토마토 [Java][C++]
[BaekJoon] 7576번 - 토마토 [Java][C++]
1. 문제 풀이
주어진 창고에서 익은 토마토 옆의 토마토는 다음 날 익은 토마토가 될 때 경과를 찾아야 하는 문제로 최단 거리를 구할 수 있는 BFS를 활용하면 해결할 수 있다. 초기 안 익은 토마토의 수와 익은 토마토의 좌표를 찾은 후 BFS에서 익은 토마토 옆에 있는 안 익은 토마토는 다음 날 익으므로 큐에 넣어주고 안 익은 토마토의 수를 한 개 줄이면 된다. 안 익은 토마토가 없을 때 탐색을 종료하면 되며 큐가 비어서 탐색이 종료되면 모든 토마토를 익힐 수 없는 배치인 경우가 된다.
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
import java.io.*;
import java.util.*;
public class Main {
static final int[] dr = {-1, 0, 1, 0};
static final int[] dc = {0, 1, 0, -1};
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[][] map = new int[N][M];
Queue<int[]> q = new ArrayDeque<>();
boolean[][] visited = new boolean[N][M];
int cnt = 0;
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
if (map[i][j] == 1) {
q.offer(new int[]{i, j});
visited[i][j] = true;
} else if (map[i][j] == 0) {
cnt++;
}
}
}
if (cnt == 0) {
System.out.println(cnt);
} else {
int result = bfs(q, visited, cnt, N, M, map);
System.out.println(result);
}
}
static int bfs(Queue<int[]> q, boolean[][] visited, int cnt, 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 < 4; d++) {
int nr = node[0] + dr[d];
int nc = node[1] + dc[d];
if (nr < 0 || nr >= N || nc < 0 || nc >= M) continue;
if (map[nr][nc] != 0 || visited[nr][nc]) continue;
q.offer(new int[]{nr, nc});
visited[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
#include <bits/stdc++.h>
using namespace std;
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
int n, m;
int grid[1000][1000];
bool visited[1000][1000];
queue<pair<int, int>> q;
int bfs(int cnt) {
int dist = 0;
while (!q.empty()) {
int sz = q.size();
while (sz--) {
auto [r, c] = q.front();
q.pop();
for (int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue;
if (grid[nr][nc] != 0 || visited[nr][nc]) continue;
q.push({nr, nc});
visited[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;
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
if (grid[i][j] == 1) {
q.push({i, j});
visited[i][j] = true;
} else if (grid[i][j] == 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.