[BaekJoon] 1012번 - 유기농 배추 [Java][C++]
[BaekJoon] 1012번 - 유기농 배추 [Java][C++]
1. 아이디어
필요한 최소 배추흰지렁이 마리 수는 배추밭에서 연결된 배추들을 한 묶음으로 볼 때, 각 묶음당 한 마리씩 배치하면 된다. 따라서 배추밭의 각 좌표를 순회하며 배추면 해당 배추 묶음을 전부 방문 처리하고 묶음의 수를 세주는 과정을 반복하면 된다. 방문 처리를 해주면 같은 묶음을 한번만 셀 수 있으며 구현할 때는 원본 배추밭에서 배추가 있던 자리를 배추가 없는 자리로 바꿔주는 방식으로 방문 체크를 해줬다.
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
67
68
import java.io.*;
import java.util.*;
public class Main {
static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, 1, 0, -1};
static int n, m;
static boolean[][] grid;
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());
while (t-- > 0) {
st = new StringTokenizer(br.readLine());
m = Integer.parseInt(st.nextToken());
n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
grid = new boolean[n][m];
while (k-- > 0) {
st = new StringTokenizer(br.readLine());
int c = Integer.parseInt(st.nextToken());
int r = Integer.parseInt(st.nextToken());
grid[r][c] = 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++;
}
}
}
sb.append(cnt).append("\n");
}
System.out.println(sb);
}
static void bfs(int sr, int sc) {
Queue<int[]> q = new ArrayDeque<>();
q.offer(new int[]{sr, sc});
grid[sr][sc] = false;
while (!q.isEmpty()) {
int[] cur = q.poll();
for (int d = 0; d < 4; d++) {
int nr = cur[0] + dr[d];
int nc = cur[1] + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue;
if (!grid[nr][nc]) continue;
q.offer(new int[]{nr, nc});
grid[nr][nc] = false;
}
}
}
}
2. 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
#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(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int k;
cin >> m >> n >> k;
while (k--) {
int c, r;
cin >> c >> r;
grid[r][c] = 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';
}
}
3. 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
import java.io.*;
import java.util.*;
public class Main {
static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, 1, 0, -1};
static int n, m;
static boolean[][] grid;
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());
while (t-- > 0) {
st = new StringTokenizer(br.readLine());
m = Integer.parseInt(st.nextToken());
n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
grid = new boolean[n][m];
while (k-- > 0) {
st = new StringTokenizer(br.readLine());
int c = Integer.parseInt(st.nextToken());
int r = Integer.parseInt(st.nextToken());
grid[r][c] = 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++;
}
}
}
sb.append(cnt).append("\n");
}
System.out.println(sb);
}
static 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);
}
}
}
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
#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(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int k;
cin >> m >> n >> k;
while (k--) {
int c, r;
cin >> c >> r;
grid[r][c] = 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';
}
}
3. 디버깅
없음.
4. 참고
없음.
This post is licensed under CC BY 4.0 by the author.