From ab06e247c2b6bd5013d0cbf155b9101c2d4fc03c Mon Sep 17 00:00:00 2001 From: hyanyul Date: Wed, 13 Aug 2025 20:25:16 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=EC=9C=A0=EA=B8=B0=EB=86=8D=EB=B0=B0?= =?UTF-8?q?=EC=B6=94/=EC=8B=A4=EB=B2=842/50=EB=B6=84/X?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\355\225\234\354\204\261\354\247\200.java" | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 "re01/BOJ_1012/\354\234\240\352\270\260\353\206\215\353\260\260\354\266\224_\355\225\234\354\204\261\354\247\200.java" diff --git "a/re01/BOJ_1012/\354\234\240\352\270\260\353\206\215\353\260\260\354\266\224_\355\225\234\354\204\261\354\247\200.java" "b/re01/BOJ_1012/\354\234\240\352\270\260\353\206\215\353\260\260\354\266\224_\355\225\234\354\204\261\354\247\200.java" new file mode 100644 index 0000000..922f056 --- /dev/null +++ "b/re01/BOJ_1012/\354\234\240\352\270\260\353\206\215\353\260\260\354\266\224_\355\225\234\354\204\261\354\247\200.java" @@ -0,0 +1,71 @@ +package BOJ_1012; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class 유기농배추_한성지 { + + static int T; // 테스트 케이스 개수 + static int M, N, K; // M: 가로, N: 세로, K: 배추 개수 + static int[][] cabbage; // 배추 좌표 + static boolean[][] visited; // 방문 여부 + static int[] dx = {0, 0, 1, -1}; // 상하좌우 이동 + static int[] dy = {1, -1, 0, 0}; + static int count; // 지렁이 수 + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + + T = Integer.parseInt(br.readLine()); + + // 테스트 T번 + for (int i = 0; i < T; i++) { + count = 0; // 지렁이 수 초기화 + st = new StringTokenizer(br.readLine()); + M = Integer.parseInt(st.nextToken()); + N = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + cabbage = new int[M][N]; + visited = new boolean[M][N]; + + // 배추밭 정보 채워넣기기 + for (int j = 0; j < K; j++) { + st = new StringTokenizer(br.readLine()); + int X = Integer.parseInt(st.nextToken()); + int Y = Integer.parseInt(st.nextToken()); + cabbage[X][Y] = 1; + } + + // 모든 좌표 순회하며 지렁이 수 카운트 + for (int x = 0; x < M; x++) { + for (int y = 0; y < N; y++) { + if (!visited[x][y] && cabbage[x][y] == 1) { // 방문한 적 없고, 배추 심어져 있는 경우우 + dfs(x, y); + count++; + } + } + } + + System.out.println(count); + } + } + + static void dfs(int x, int y) { + visited[x][y] = true; + + for (int i = 0; i < 4; i++) { + int cx = x + dx[i]; + int cy = y + dy[i]; + + if (cx >= 0 && cy >= 0 && cx < M && cy < N) { // 배추밭 범위 확인 + if (!visited[cx][cy] && cabbage[cx][cy] == 1) { // 방문한적 없고 배추 있을 경우 + dfs(cx, cy); + } + } + } + } +} From 3b1ff1feb3cf56e2962e0494098bdd78bd34b964 Mon Sep 17 00:00:00 2001 From: hyanyul Date: Wed, 13 Aug 2025 20:33:05 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=EC=A3=BC=EC=84=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...224_\355\225\234\354\204\261\354\247\200.java" | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git "a/re01/BOJ_1012/\354\234\240\352\270\260\353\206\215\353\260\260\354\266\224_\355\225\234\354\204\261\354\247\200.java" "b/re01/BOJ_1012/\354\234\240\352\270\260\353\206\215\353\260\260\354\266\224_\355\225\234\354\204\261\354\247\200.java" index 922f056..7e49122 100644 --- "a/re01/BOJ_1012/\354\234\240\352\270\260\353\206\215\353\260\260\354\266\224_\355\225\234\354\204\261\354\247\200.java" +++ "b/re01/BOJ_1012/\354\234\240\352\270\260\353\206\215\353\260\260\354\266\224_\355\225\234\354\204\261\354\247\200.java" @@ -32,7 +32,7 @@ public static void main(String[] args) throws IOException { cabbage = new int[M][N]; visited = new boolean[M][N]; - // 배추밭 정보 채워넣기기 + // 배추밭 정보 채워넣기 for (int j = 0; j < K; j++) { st = new StringTokenizer(br.readLine()); int X = Integer.parseInt(st.nextToken()); @@ -40,11 +40,11 @@ public static void main(String[] args) throws IOException { cabbage[X][Y] = 1; } - // 모든 좌표 순회하며 지렁이 수 카운트 + // (0,0)부터 모든 좌표 순회하며 지렁이 수 카운트 for (int x = 0; x < M; x++) { for (int y = 0; y < N; y++) { - if (!visited[x][y] && cabbage[x][y] == 1) { // 방문한 적 없고, 배추 심어져 있는 경우우 - dfs(x, y); + if (!visited[x][y] && cabbage[x][y] == 1) { // 방문한 적 없고, 배추 심어져 있는 경우 + dfs(x, y); // 연결된 군집 전체 방문 count++; } } @@ -54,16 +54,19 @@ public static void main(String[] args) throws IOException { } } + // 깊이 우선 탐색 static void dfs(int x, int y) { visited[x][y] = true; + // 상하좌우 이동하면서 확인 for (int i = 0; i < 4; i++) { int cx = x + dx[i]; int cy = y + dy[i]; if (cx >= 0 && cy >= 0 && cx < M && cy < N) { // 배추밭 범위 확인 - if (!visited[cx][cy] && cabbage[cx][cy] == 1) { // 방문한적 없고 배추 있을 경우 - dfs(cx, cy); + // 상하좌우로 연결된 밭에 배추가 심어져있고, 방문한 적 없는 경우 재귀 호출 + if (!visited[cx][cy] && cabbage[cx][cy] == 1) { + dfs(cx, cy); // 재귀 호출로 깊이 탐색 } } } From 785165f217e7db5d61b7eab1d5f323a115fe1479 Mon Sep 17 00:00:00 2001 From: hyanyul Date: Wed, 20 Aug 2025 14:58:21 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=EB=8B=A8=EC=A7=80=EB=B2=88=ED=98=B8?= =?UTF-8?q?=EB=B6=99=EC=9D=B4=EA=B8=B0/=EC=8B=A4=EB=B2=841/50=EB=B6=84/O?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\355\225\234\354\204\261\354\247\200.java" | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 "re02/BOJ_2667/\353\213\250\354\247\200\353\262\210\355\230\270\353\266\231\354\235\264\352\270\260_\355\225\234\354\204\261\354\247\200.java" diff --git "a/re02/BOJ_2667/\353\213\250\354\247\200\353\262\210\355\230\270\353\266\231\354\235\264\352\270\260_\355\225\234\354\204\261\354\247\200.java" "b/re02/BOJ_2667/\353\213\250\354\247\200\353\262\210\355\230\270\353\266\231\354\235\264\352\270\260_\355\225\234\354\204\261\354\247\200.java" new file mode 100644 index 0000000..757c7b1 --- /dev/null +++ "b/re02/BOJ_2667/\353\213\250\354\247\200\353\262\210\355\230\270\353\266\231\354\235\264\352\270\260_\355\225\234\354\204\261\354\247\200.java" @@ -0,0 +1,70 @@ +package BOJ_2667; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; + +class 단지번호붙이기_한성지 { + + static int N; // 지도 길이 및 높이 + static int[][] map; // 지도 값 + static boolean[][] visit; // 방문 여부 + static int[] dx = {0, 1, 0, -1}; // 상하좌우 좌표 + static int[] dy = {1, 0, -1, 0}; + static int count = 0; // 단지 수 + static int houseCount; // 단지 내 집 수 + static ArrayList answer = new ArrayList<>(); + static StringBuilder sb = new StringBuilder(); + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + N = Integer.parseInt(br.readLine()); + map = new int[N][N]; + visit = new boolean[N][N]; + for (int i = 0; i < N; i++) { // 지도 채우기 + String[] arr = br.readLine().split(""); + for (int j = 0; j < N; j++) { + map[i][j] = Integer.parseInt(arr[j]); + } + } + + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + if (!visit[i][j] && map[i][j] == 1) { // 방문한 적 없고, 집 있을 경우 + houseCount = 0; + dfs(i, j); + count++; + answer.add(houseCount); + } + } + } + + Collections.sort(answer); + + for (int n : answer) { + sb.append(n).append("\n"); + } + + System.out.println(count); + System.out.print(sb); + } + + static void dfs(int x, int y) { + visit[x][y] = true; // 방문 표시 + houseCount++; + + for (int i = 0; i < 4; i++) { // 상하좌우 이동 + int cx = x + dx[i]; + int cy = y + dy[i]; + + if (cx >= 0 && cy >= 0 && cx < N && cy < N) { // 지도 내의 좌표이고 + if (!visit[cx][cy] && map[cx][cy] == 1) { // 방문한 적 없으며, 집이 있을 때 + dfs(cx, cy); // 재귀호출을 통한 깊이 탐색 + } + } + } + } +} \ No newline at end of file From 8e0a17108ac0366a0ca094901b475ba68a357fe2 Mon Sep 17 00:00:00 2001 From: hyanyul Date: Wed, 20 Aug 2025 15:01:17 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=EB=8B=A8=EC=A7=80=EB=B2=88=ED=98=B8?= =?UTF-8?q?=EB=B6=99=EC=9D=B4=EA=B8=B0/=EC=8B=A4=EB=B2=841/50=EB=B6=84/O?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\352\270\260_\355\225\234\354\204\261\354\247\200.java" | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git "a/re02/BOJ_2667/\353\213\250\354\247\200\353\262\210\355\230\270\353\266\231\354\235\264\352\270\260_\355\225\234\354\204\261\354\247\200.java" "b/re02/BOJ_2667/\353\213\250\354\247\200\353\262\210\355\230\270\353\266\231\354\235\264\352\270\260_\355\225\234\354\204\261\354\247\200.java" index 757c7b1..10018bf 100644 --- "a/re02/BOJ_2667/\353\213\250\354\247\200\353\262\210\355\230\270\353\266\231\354\235\264\352\270\260_\355\225\234\354\204\261\354\247\200.java" +++ "b/re02/BOJ_2667/\353\213\250\354\247\200\353\262\210\355\230\270\353\266\231\354\235\264\352\270\260_\355\225\234\354\204\261\354\247\200.java" @@ -13,7 +13,6 @@ class 단지번호붙이기_한성지 { static boolean[][] visit; // 방문 여부 static int[] dx = {0, 1, 0, -1}; // 상하좌우 좌표 static int[] dy = {1, 0, -1, 0}; - static int count = 0; // 단지 수 static int houseCount; // 단지 내 집 수 static ArrayList answer = new ArrayList<>(); static StringBuilder sb = new StringBuilder(); @@ -36,7 +35,6 @@ public static void main(String[] args) throws IOException { if (!visit[i][j] && map[i][j] == 1) { // 방문한 적 없고, 집 있을 경우 houseCount = 0; dfs(i, j); - count++; answer.add(houseCount); } } @@ -48,7 +46,7 @@ public static void main(String[] args) throws IOException { sb.append(n).append("\n"); } - System.out.println(count); + System.out.println(answer.size()); System.out.print(sb); } @@ -61,7 +59,7 @@ static void dfs(int x, int y) { int cy = y + dy[i]; if (cx >= 0 && cy >= 0 && cx < N && cy < N) { // 지도 내의 좌표이고 - if (!visit[cx][cy] && map[cx][cy] == 1) { // 방문한 적 없으며, 집이 있을 때 + if (!visit[cx][cy] && map[cx][cy] == 1) { // 방문한적 없으며, 집이 있을 때 dfs(cx, cy); // 재귀호출을 통한 깊이 탐색 } } From 5d027a50c6f0a304909eca49fefb5b4594763bfd Mon Sep 17 00:00:00 2001 From: hyanyul Date: Wed, 20 Aug 2025 15:07:27 +0900 Subject: [PATCH 5/5] =?UTF-8?q?pr=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\355\225\234\354\204\261\354\247\200.java" | 74 ------------------- 1 file changed, 74 deletions(-) delete mode 100644 "re01/BOJ_1012/\354\234\240\352\270\260\353\206\215\353\260\260\354\266\224_\355\225\234\354\204\261\354\247\200.java" diff --git "a/re01/BOJ_1012/\354\234\240\352\270\260\353\206\215\353\260\260\354\266\224_\355\225\234\354\204\261\354\247\200.java" "b/re01/BOJ_1012/\354\234\240\352\270\260\353\206\215\353\260\260\354\266\224_\355\225\234\354\204\261\354\247\200.java" deleted file mode 100644 index 7e49122..0000000 --- "a/re01/BOJ_1012/\354\234\240\352\270\260\353\206\215\353\260\260\354\266\224_\355\225\234\354\204\261\354\247\200.java" +++ /dev/null @@ -1,74 +0,0 @@ -package BOJ_1012; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.StringTokenizer; - -public class 유기농배추_한성지 { - - static int T; // 테스트 케이스 개수 - static int M, N, K; // M: 가로, N: 세로, K: 배추 개수 - static int[][] cabbage; // 배추 좌표 - static boolean[][] visited; // 방문 여부 - static int[] dx = {0, 0, 1, -1}; // 상하좌우 이동 - static int[] dy = {1, -1, 0, 0}; - static int count; // 지렁이 수 - - public static void main(String[] args) throws IOException { - - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - StringTokenizer st; - - T = Integer.parseInt(br.readLine()); - - // 테스트 T번 - for (int i = 0; i < T; i++) { - count = 0; // 지렁이 수 초기화 - st = new StringTokenizer(br.readLine()); - M = Integer.parseInt(st.nextToken()); - N = Integer.parseInt(st.nextToken()); - K = Integer.parseInt(st.nextToken()); - cabbage = new int[M][N]; - visited = new boolean[M][N]; - - // 배추밭 정보 채워넣기 - for (int j = 0; j < K; j++) { - st = new StringTokenizer(br.readLine()); - int X = Integer.parseInt(st.nextToken()); - int Y = Integer.parseInt(st.nextToken()); - cabbage[X][Y] = 1; - } - - // (0,0)부터 모든 좌표 순회하며 지렁이 수 카운트 - for (int x = 0; x < M; x++) { - for (int y = 0; y < N; y++) { - if (!visited[x][y] && cabbage[x][y] == 1) { // 방문한 적 없고, 배추 심어져 있는 경우 - dfs(x, y); // 연결된 군집 전체 방문 - count++; - } - } - } - - System.out.println(count); - } - } - - // 깊이 우선 탐색 - static void dfs(int x, int y) { - visited[x][y] = true; - - // 상하좌우 이동하면서 확인 - for (int i = 0; i < 4; i++) { - int cx = x + dx[i]; - int cy = y + dy[i]; - - if (cx >= 0 && cy >= 0 && cx < M && cy < N) { // 배추밭 범위 확인 - // 상하좌우로 연결된 밭에 배추가 심어져있고, 방문한 적 없는 경우 재귀 호출 - if (!visited[cx][cy] && cabbage[cx][cy] == 1) { - dfs(cx, cy); // 재귀 호출로 깊이 탐색 - } - } - } - } -}