-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path프렌즈4블록.java
104 lines (81 loc) · 2.37 KB
/
프렌즈4블록.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package week27;
import java.util.*;
class 프렌즈4블록 {
static int M,N;
static char[][] board;
static boolean[][] bombBoard;
static int[][] direction = {{1, 0}, {0, 1}, {1, 1}};
static void checkBomb(int row, int col) {
char target = board[row][col];
for (int[] d : direction) {
if (board[row + d[0]][col + d[1]] != target) {
return;
}
}
bombBoard[row][col] = true;
for (int[] d : direction) {
bombBoard[row + d[0]][col + d[1]] = true;
}
}
static int bomb() {
int count = 0;
for (int row = 0; row < M; row++) {
for (int col = 0; col < N; col++) {
if (bombBoard[row][col]) {
count++;
board[row][col] = ' ';
bombBoard[row][col] = false;
}
}
}
return count;
}
static void check() {
for (int row = 0; row < M - 1; row++) {
for (int col = 0; col < N - 1; col++) {
if (board[row][col] == ' ') {
continue;
}
checkBomb(row, col);
}
}
}
static void down() {
for (int col = 0; col < N; col++) {
Queue<int[]> emptyNodes = new LinkedList<>();
for (int row = M - 1; row >= 0; row--) {
if (board[row][col] == ' ') {
emptyNodes.offer(new int[]{row, col});
continue;
}
if (emptyNodes.isEmpty()) {
continue;
}
int[] node = emptyNodes.poll();
board[node[0]][node[1]] = board[row][col];
board[row][col] = ' ';
emptyNodes.offer(new int[]{row, col});
}
}
}
public int solution(int m, int n, String[] input) {
int answer = 0;
M = m;
N = n;
board = new char[M][N];
bombBoard = new boolean[M][N];
for (int row = 0; row < M; row++) {
board[row] = input[row].toCharArray();
}
while (true) {
check();
int count = bomb();
if (count == 0) {
break;
}
answer += count;
down();
}
return answer;
}
}