From 68e9948471e7f037206aa934872edd9b1c0f5502 Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Sun, 22 Feb 2026 00:04:12 +0900 Subject: [PATCH] =?UTF-8?q?[20260221]=20BOJ=20/=20G4=20/=20Puyo=20Puyo=20/?= =?UTF-8?q?=20=EC=9D=B4=EC=A4=80=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JHLEE325/202602/21 BOJ G4 Puyo Puyo.md | 90 ++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 JHLEE325/202602/21 BOJ G4 Puyo Puyo.md diff --git a/JHLEE325/202602/21 BOJ G4 Puyo Puyo.md b/JHLEE325/202602/21 BOJ G4 Puyo Puyo.md new file mode 100644 index 00000000..f28e4054 --- /dev/null +++ b/JHLEE325/202602/21 BOJ G4 Puyo Puyo.md @@ -0,0 +1,90 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + + static char[][] map = new char[12][6]; + static int[] dr = {-1, 1, 0, 0}; + static int[] dc = {0, 0, -1, 1}; + static boolean isPopped; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + for (int i = 0; i < 12; i++) { + map[i] = br.readLine().toCharArray(); + } + + int chainCount = 0; + while (true) { + isPopped = false; + boolean[][] visited = new boolean[12][6]; + + for (int i = 0; i < 12; i++) { + for (int j = 0; j < 6; j++) { + if (map[i][j] != '.' && !visited[i][j]) { + bfs(i, j, visited); + } + } + } + + if (!isPopped) break; + + fall(); + chainCount++; + } + + System.out.println(chainCount); + } + + static void bfs(int r, int c, boolean[][] visited) { + List puyos = new ArrayList<>(); + Queue q = new LinkedList<>(); + char color = map[r][c]; + + q.add(new int[]{r, c}); + puyos.add(new int[]{r, c}); + visited[r][c] = true; + + while (!q.isEmpty()) { + int[] cur = q.poll(); + + for (int i = 0; i < 4; i++) { + int nr = cur[0] + dr[i]; + int nc = cur[1] + dc[i]; + + if (nr >= 0 && nr < 12 && nc >= 0 && nc < 6) { + if (!visited[nr][nc] && map[nr][nc] == color) { + visited[nr][nc] = true; + q.add(new int[]{nr, nc}); + puyos.add(new int[]{nr, nc}); + } + } + } + } + + if (puyos.size() >= 4) { + for (int[] p : puyos) { + map[p[0]][p[1]] = '.'; + } + isPopped = true; + } + } + + static void fall() { + for (int c = 0; c < 6; c++) { + for (int r = 11; r > 0; r--) { + if (map[r][c] == '.') { + for (int k = r - 1; k >= 0; k--) { + if (map[k][c] != '.') { + map[r][c] = map[k][c]; + map[k][c] = '.'; + break; + } + } + } + } + } + } +} +```