diff --git "a/suyeun84/202509/09 PGM LV2 [1\354\260\250] \355\224\204\353\240\214\354\246\2104\353\270\224\353\241\235.md" "b/suyeun84/202509/09 PGM LV2 [1\354\260\250] \355\224\204\353\240\214\354\246\2104\353\270\224\353\241\235.md" new file mode 100644 index 00000000..986e8d35 --- /dev/null +++ "b/suyeun84/202509/09 PGM LV2 [1\354\260\250] \355\224\204\353\240\214\354\246\2104\353\270\224\353\241\235.md" @@ -0,0 +1,74 @@ + ```java +import java.util.*; +class Solution { + static int[][] dir = new int[][]{{0,0}, {0, 1}, {1,0}, {1,1}}; + static int M, N, answer = 0; + static char[][] map; + public int solution(int m, int n, String[] board) { + M = m; + N = n; + map = new char[M][N]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + map[i][j] = board[i].charAt(j); + } + } + boolean flag = true; + while (flag) { + flag = check(); + } + return answer; + } + // 없앨 수 있는 block 찾기 + private boolean check() { + boolean[][] remove = new boolean[M][N]; + boolean flag = false; + for (int i = 0; i < M-1; i++) { + for (int j = 0; j < N-1; j++) { + char curr = map[i][j]; + if (curr == '0') continue; + if (curr == map[i+1][j] && curr == map[i][j+1] && curr == map[i+1][j+1]) { + for (int[] d : dir) { + int ny = i + d[0]; + int nx = j + d[1]; + if (!remove[ny][nx]) answer++; + remove[ny][nx] = true; + } + flag = true; + } + } + } + if (flag) move(remove); + return flag; + } + + private void move(boolean[][] remove) { + for (int j = 0; j < N; j++) { // 각 열마다 + Stack stack = new Stack<>(); + + for (int i = 0; i < M; i++) { + if (!remove[i][j] && map[i][j] != '0') { + stack.push(map[i][j]); + } + } + + for (int i = M - 1; i >= 0; i--) { + if (!stack.isEmpty()) { + map[i][j] = stack.pop(); + } else { + map[i][j] = '0'; + } + } + } + } + + + class Point { + int y, x; + public Point(int y, int x) { + this.y = y; + this.x = x; + } + } +} +```