From 5b9a65ae45432169814f8ca52e4057f3915b816d Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Sun, 29 Mar 2026 22:36:21 +0900 Subject: [PATCH] baekjoon 20260329 --- code_study/Baekjoon/java/_16946.java | 117 +++++++++++++++++++++++++++ code_study/Baekjoon/python/2985.py | 1 + 2 files changed, 118 insertions(+) create mode 100644 code_study/Baekjoon/java/_16946.java create mode 100644 code_study/Baekjoon/python/2985.py diff --git a/code_study/Baekjoon/java/_16946.java b/code_study/Baekjoon/java/_16946.java new file mode 100644 index 0000000..11900ed --- /dev/null +++ b/code_study/Baekjoon/java/_16946.java @@ -0,0 +1,117 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +class Axis { + int x, y; + Axis(int x, int y) { + this.x = x; + this.y = y; + } +} + +public class _16946 { + static int N, M; + static int[][] MAP, groupMAP; + static boolean[][] visited; + + static int[] dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1}; + + static int findGroupSize(int x, int y, int group) { + Queue qu = new ArrayDeque<>(); + qu.add(new Axis(x, y)); + + visited[y][x] = true; + groupMAP[y][x] = group; + + int size = 1; + + while(!qu.isEmpty()) { + Axis now = qu.poll(); + + for(int i=0; i<4; i++) { + int nx = now.x + dx[i], ny = now.y + dy[i]; + + if(0 <= nx && nx < M && 0 <= ny && ny < N && !visited[ny][nx] && MAP[ny][nx] != 1) { + visited[ny][nx] = true; + size++; + qu.add(new Axis(nx, ny)); + groupMAP[ny][nx] = group; + } + } + } + + return size; + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); M = Integer.parseInt(st.nextToken()); + + MAP = new int[N][M]; + + for(int i=0; i groupSize = new ArrayList<>(); + int group = 0; + + for(int i=0; i nx || nx >= M || 0 > ny || ny >= N || MAP[ny][nx] == 1) continue; + + nearGroups[idx++] = groupMAP[ny][nx]; + } + + for(int k=0; k