Skip to content

Commit 6cc3a7d

Browse files
authored
[20250909] PGM / LV2 / [1차] 프렌즈4블록 / 김수연
1 parent 4065b1f commit 6cc3a7d

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
```java
2+
import java.util.*;
3+
class Solution {
4+
static int[][] dir = new int[][]{{0,0}, {0, 1}, {1,0}, {1,1}};
5+
static int M, N, answer = 0;
6+
static char[][] map;
7+
public int solution(int m, int n, String[] board) {
8+
M = m;
9+
N = n;
10+
map = new char[M][N];
11+
for (int i = 0; i < m; i++) {
12+
for (int j = 0; j < n; j++) {
13+
map[i][j] = board[i].charAt(j);
14+
}
15+
}
16+
boolean flag = true;
17+
while (flag) {
18+
flag = check();
19+
}
20+
return answer;
21+
}
22+
// 없앨 수 있는 block 찾기
23+
private boolean check() {
24+
boolean[][] remove = new boolean[M][N];
25+
boolean flag = false;
26+
for (int i = 0; i < M-1; i++) {
27+
for (int j = 0; j < N-1; j++) {
28+
char curr = map[i][j];
29+
if (curr == '0') continue;
30+
if (curr == map[i+1][j] && curr == map[i][j+1] && curr == map[i+1][j+1]) {
31+
for (int[] d : dir) {
32+
int ny = i + d[0];
33+
int nx = j + d[1];
34+
if (!remove[ny][nx]) answer++;
35+
remove[ny][nx] = true;
36+
}
37+
flag = true;
38+
}
39+
}
40+
}
41+
if (flag) move(remove);
42+
return flag;
43+
}
44+
45+
private void move(boolean[][] remove) {
46+
for (int j = 0; j < N; j++) { // 각 열마다
47+
Stack<Character> stack = new Stack<>();
48+
49+
for (int i = 0; i < M; i++) {
50+
if (!remove[i][j] && map[i][j] != '0') {
51+
stack.push(map[i][j]);
52+
}
53+
}
54+
55+
for (int i = M - 1; i >= 0; i--) {
56+
if (!stack.isEmpty()) {
57+
map[i][j] = stack.pop();
58+
} else {
59+
map[i][j] = '0';
60+
}
61+
}
62+
}
63+
}
64+
65+
66+
class Point {
67+
int y, x;
68+
public Point(int y, int x) {
69+
this.y = y;
70+
this.x = x;
71+
}
72+
}
73+
}
74+
```

0 commit comments

Comments
 (0)