반응형
🔺 문제
1012번: 유기농 배추
차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에
www.acmicpc.net
🔺 코드
1) DFS 풀이
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
|
import java.util.*;
import java.io.*;
public class Main {
static int[] dx = {0,0,-1,1};
static int[] dy = {1,-1,0,0};
static int[][] map;
static boolean[][] visited;
static int T, M, N, K;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
T = Integer.parseInt(br.readLine());
while(T -- > 0) {
st = new StringTokenizer(br.readLine(), " ");
M = Integer.parseInt(st.nextToken()); // 가로
N = Integer.parseInt(st.nextToken()); // 세로
K = Integer.parseInt(st.nextToken()); // 위치
map = new int[M][N];
visited = new boolean[M][N];
int cnt = 0;
while(K -- > 0) {
st = new StringTokenizer(br.readLine(), " ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
map[a][b] = 1;
}
for(int i = 0 ; i< M ; i++) {
for(int j = 0 ; j < N ; j++) {
if(!visited[i][j] && map[i][j] == 1) {
dfs(i, j);
cnt++;
}
}
}
sb.append(cnt).append("\n");
}
System.out.println(sb);
}
private static void dfs(int a, int b) {
if(visited[a][b]) return;
visited[a][b] = true;
for(int d = 0 ; d < 4 ; d++) {
int x = dx[d] + a;
int y = dy[d] + b;
if(x < 0 || x >= M || y < 0 || y >= N) continue;
if(!visited[x][y] && map[x][y] == 1) {
dfs(x, y);
}
}
}
}
|
cs |
2) BFS 풀이
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
|
import java.util.*;
import java.io.*;
public class Main {
static int[] dx = {0,0,-1,1};
static int[] dy = {1,-1,0,0};
static int[][] map;
static boolean[][] visited;
static int T, M, N, K;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
T = Integer.parseInt(br.readLine());
while(T -- > 0) {
st = new StringTokenizer(br.readLine(), " ");
M = Integer.parseInt(st.nextToken()); // 가로
N = Integer.parseInt(st.nextToken()); // 세로
K = Integer.parseInt(st.nextToken()); // 위치
map = new int[M][N];
visited = new boolean[M][N];
int cnt = 0;
while(K -- > 0) {
st = new StringTokenizer(br.readLine(), " ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
map[a][b] = 1;
}
for(int i = 0 ; i< M ; i++) {
for(int j = 0 ; j < N ; j++) {
if(!visited[i][j] && map[i][j] == 1) {
bfs(i, j);
cnt++;
}
}
}
sb.append(cnt).append("\n");
}
System.out.println(sb);
}
private static void bfs(int a, int b) {
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[] {a, b});
visited[a][b] = true;
while(!queue.isEmpty()) {
int[] now = queue.poll();
for(int d = 0 ; d < 4 ; d++) {
int x = dx[d] + now[0];
int y = dy[d] + now[1];
if(x < 0 || x >= M || y < 0 || y >= N) continue;
if(!visited[x][y] && map[x][y] == 1) {
queue.add(new int[] {x, y});
visited[x][y] = true;
}
}
}
}
}
|
cs |
✅ 해결 아이디어
✔ DFS / BFS
- 각 점에 대해 dfs/bfs 수행하면서 dfs/bfs 수행 횟수 계산 (= 덩어리 수)
🔺 다른 풀이들
- 대체로 비슷함
💬 느낀 점
재밌는 bfs dfs...
1회독 | 2회독 | 3회독 | 4회독 | 5회독 |
V |
반응형
'코테 > 백준' 카테고리의 다른 글
[백준/JAVA] 19637번: IF문 좀 대신 써줘 (0) | 2023.07.08 |
---|---|
[백준/JAVA] 7576번: 토마토 (0) | 2023.07.08 |
[백준/JAVA] 1026번: 보물 (0) | 2023.07.07 |
[백준/JAVA] 5585번: 거스름돈 (0) | 2023.07.07 |
[백준/JAVA] 1065번: 한수 (0) | 2023.07.06 |