-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon_11559.java
More file actions
112 lines (87 loc) · 2.92 KB
/
baekjoon_11559.java
File metadata and controls
112 lines (87 loc) · 2.92 KB
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
105
106
107
108
109
110
111
112
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class baekjoon_11559 {
static char map[][] = new char[12][6];
static char dot = '.', pop = 'p';
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<12;i++) {
map[i] = br.readLine().toCharArray();
}
int result = 0;
while(true) {
int cnt = pop();
if(cnt>0) result++;
else if(cnt == 0) break;
}
System.out.println(result);
}
public static int pop() {
int cnt = 0;
for(int i=0;i<12;i++) {
for(int j=0;j<6;j++) {
if(map[i][j] == dot || map[i][j] == pop) continue;
List<int[]> popList = isPopable(i, j);
if(popList.size() >= 4) {
cnt++;
for(int[] p : popList) {
int r = p[0];
int c = p[1];
map[r][c] = pop;
}
}
}
}
gravity();
return cnt;
}
public static void gravity() {
for(int i=0;i<6;i++) {
Queue<Character> queue = new LinkedList<>();
for(int j=map.length - 1; j>=0;j--) {
if(map[j][i] == pop || map[j][i] == dot) {
continue;
}
queue.add(map[j][i]);
}
for(int j=map.length - 1; j>=0;j--) {
if(queue.isEmpty()) {
map[j][i] = dot;
continue;
}
char color = queue.poll();
map[j][i] = color;
}
}
}
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
public static List<int[]> isPopable(int r, int c) {
char color = map[r][c];
Queue<int[]> queue = new LinkedList<>();
List<int[]> result = new ArrayList<>();
queue.add(new int[]{r, c});
result.add(new int[]{r, c});
boolean[][] visited = new boolean[12][6];
visited[r][c] = true;
while(!queue.isEmpty()) {
int[] cur = queue.poll();
for(int i=0;i<4;i++) {
int nr = cur[0] + dr[i];
int nc = cur[1] + dc[i];
if(nr < 0 || nc < 0 || nr >= 12 || nc >= 6 || visited[nr][nc]) {
continue;
}
if(map[nr][nc] == color) {
visited[nr][nc] = true;
result.add(new int[]{nr, nc});
queue.add(new int[] {nr, nc});
}
}
}
return result;
}
}