Skip to content

Commit 65e2830

Browse files
authored
[백준 11559] Puyo Puyo - 시뮬레이션
1 parent fbb7c7a commit 65e2830

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

hoseok/week35/Boj11559.java

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class Main {
5+
6+
static class Node {
7+
int r, c;
8+
9+
Node(int r, int c) {
10+
this.r = r;
11+
this.c = c;
12+
}
13+
}
14+
15+
private static final int[] rows = {-1, 0, 1, 0};
16+
private static final int[] cols = {0, 1, 0, -1};
17+
18+
private static final char[][] map = new char[12][6];
19+
private static final List<Node> bubbles = new ArrayList<>();
20+
21+
public static void main(String[] args) throws Exception {
22+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
23+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
24+
25+
for (int i = 0; i < 12; i++) {
26+
String line = br.readLine();
27+
for (int j = 0; j < 6; j++) {
28+
map[i][j] = line.charAt(j);
29+
}
30+
}
31+
32+
int count = 0;
33+
while (bubblePop()) {
34+
count++;
35+
moveDown();
36+
}
37+
38+
bw.write(count + "");
39+
bw.flush();
40+
bw.close();
41+
}
42+
43+
public static void moveDown() {
44+
for (int i = 0; i < 6; i++) {
45+
while (true) {
46+
// 일단 해당 열의 마지막 행부터 차례로 .을 찾기
47+
int firstDot = findFirstDot(i);
48+
if (firstDot == -1) {
49+
break;
50+
}
51+
// . 이후에 심볼이 있다면 그 사이 공간은 터진 공간임
52+
int firstSymbolIndex = findFirstSymbolAfter(i, firstDot);
53+
if (firstSymbolIndex == -1) {
54+
break;
55+
}
56+
// 첫번째 심볼이후 끝나는 자리를 찾음
57+
int secondSymbolIndex = findLastSymbolAfter(i, firstSymbolIndex);
58+
59+
// 찾은 심볼을 firstDot위치부터 채워넣음
60+
moveBubbleDown(i, firstDot, firstSymbolIndex, secondSymbolIndex);
61+
}
62+
}
63+
}
64+
65+
public static void moveBubbleDown(int col, int firstDot, int startInclusive, int endInclusive) {
66+
for (int i = startInclusive; i >= endInclusive; i--) {
67+
map[firstDot--][col] = map[i][col];
68+
map[i][col] = '.';
69+
}
70+
}
71+
72+
public static int findLastSymbolAfter(int col, int index) {
73+
for (int i = index; i >= 0; i--) {
74+
if (map[i][col] == '.') {
75+
return i + 1;
76+
}
77+
}
78+
return 0;
79+
}
80+
81+
public static int findFirstSymbolAfter(int col, int index) {
82+
for (int i = index; i >= 0; i--) {
83+
if (map[i][col] != '.') {
84+
return i;
85+
}
86+
}
87+
return -1;
88+
}
89+
90+
public static int findFirstDot(int col) {
91+
for (int i = 11; i >= 0; i--) {
92+
if (map[i][col] == '.') {
93+
return i;
94+
}
95+
}
96+
return -1;
97+
}
98+
99+
public static boolean bubblePop() {
100+
boolean isPop = false;
101+
boolean[][] visited = new boolean[12][6];
102+
103+
for (int i = 0; i < 12; i++) {
104+
for (int j = 0; j < 6; j++) {
105+
if (!visited[i][j] && map[i][j] != '.') {
106+
isPop |= bfs(visited, new Node(i, j));
107+
}
108+
}
109+
}
110+
if (isPop) {
111+
removeBubbles();
112+
bubbles.clear();
113+
}
114+
return isPop;
115+
}
116+
117+
public static void removeBubbles() {
118+
for (Node node : bubbles) {
119+
map[node.r][node.c] = '.';
120+
}
121+
}
122+
123+
public static boolean bfs(boolean[][] visited, Node startNode) {
124+
List<Node> tempNodes = new ArrayList<>();
125+
Queue<Node> que = new LinkedList<>();
126+
127+
tempNodes.add(startNode);
128+
que.offer(startNode);
129+
visited[startNode.r][startNode.c] = true;
130+
char targetSymbol = map[startNode.r][startNode.c];
131+
132+
while (!que.isEmpty()) {
133+
Node curNode = que.poll();
134+
135+
for (int i = 0; i < 4; i++) {
136+
int nextR = rows[i] + curNode.r;
137+
int nextC = cols[i] + curNode.c;
138+
139+
if (checks(nextR, nextC)) {
140+
continue;
141+
}
142+
143+
if (!visited[nextR][nextC] && map[nextR][nextC] == targetSymbol) {
144+
visited[nextR][nextC] = true;
145+
Node nextNode = new Node(nextR, nextC);
146+
tempNodes.add(nextNode);
147+
que.offer(nextNode);
148+
}
149+
}
150+
}
151+
if (tempNodes.size() >= 4) {
152+
bubbles.addAll(tempNodes);
153+
return true;
154+
}
155+
return false;
156+
}
157+
158+
public static boolean checks(int r, int c) {
159+
return r < 0 || r >= 12 || c < 0 || c >= 6;
160+
}
161+
}

0 commit comments

Comments
 (0)