[BaekJoon] 1012번 - 유기농 배추 [Java][C++]
[BaekJoon] 1012번 - 유기농 배추 [Java][C++]
1. 문제 풀이
테스트 케이스별로 필요한 최소의 배추흰지렁이 마리 수를 구하는 문제로 최소 수는 서로 연결된 배추 덩어리의 개수와 동일하다. 배추밭 전체를 순회하며 배추를 발견하면 해당 배추 덩어리를 방문 체크하고 덩어리의 수를 하나씩 세주면 간단하게 구할 수 있다. 배추밭을 한번만 순회하면 된다는 점에서 별도의 방문 체크 대신 방문한 배추를 $0$ 으로 변경하는 방식으로 구현했다.
2. 코드
1. BFS [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
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));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int T = Integer.parseInt(br.readLine());
for (int tc = 1; tc <= T; tc++) {
st = new StringTokenizer(br.readLine());
int M = Integer.parseInt(st.nextToken());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
boolean[][] map = new boolean[N][M];
for (int i = 0; i < K; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
map[y][x] = true;
}
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j]) {
bfs(i, j, N, M, map);
cnt++;
}
}
}
sb.append(cnt).append("\n");
}
System.out.println(sb);
}
static void bfs(int sr, int sc, int N, int M, boolean[][] map) {
Queue<int[]> q = new ArrayDeque<>();
q.offer(new int[]{sr, sc});
map[sr][sc] = false;
while (!q.isEmpty()) {
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]) continue;
q.offer(new int[]{nr, nc});
map[nr][nc] = false;
}
}
}
}
2. DFS [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
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};
static int N;
static int M;
static boolean[][] map;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int T = Integer.parseInt(br.readLine());
for (int tc = 1; tc <= T; tc++) {
st = new StringTokenizer(br.readLine());
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
map = new boolean[N][M];
for (int i = 0; i < K; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
map[y][x] = true;
}
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j]) {
dfs(i, j);
cnt++;
}
}
}
sb.append(cnt).append("\n");
}
System.out.println(sb);
}
static void dfs(int r, int c) {
map[r][c] = false;
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 (!map[nr][nc]) continue;
dfs(nr, nc);
}
}
}
3. BFS [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
#include <bits/stdc++.h>
using namespace std;
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
int n, m;
bool grid[50][50];
void bfs(int sr, int sc) {
queue<pair<int, int>> q;
q.push({sr, sc});
grid[sr][sc] = false;
while (!q.empty()) {
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]) continue;
q.push({nr, nc});
grid[nr][nc] = false;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
for (int tc = 1; tc <= t; tc++) {
int k;
cin >> m >> n >> k;
memset(grid, 0, sizeof(grid));
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
grid[y][x] = true;
}
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j]) {
bfs(i, j);
cnt++;
}
}
}
cout << cnt << '\n';
}
}
4. DFS [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
#include <bits/stdc++.h>
using namespace std;
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
int n, m;
bool grid[50][50];
void dfs(int r, int c) {
grid[r][c] = false;
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]) continue;
dfs(nr, nc);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
for (int tc = 1; tc <= t; tc++) {
int k;
cin >> m >> n >> k;
memset(grid, 0, sizeof(grid));
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
grid[y][x] = true;
}
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j]) {
dfs(i, j);
cnt++;
}
}
}
cout << cnt << '\n';
}
}
This post is licensed under CC BY 4.0 by the author.