Skip to content

Commit c6b8e63

Browse files
authored
Merge pull request #216 from AlgorithmStudy-Allumbus/YoonYn9915
YoonYn9915/ 5월 4주차 / 1문제
2 parents c2cfdf8 + 0a9fa53 commit c6b8e63

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from collections import deque
2+
from sys import stdin
3+
input = stdin.readline
4+
5+
EMPTY = '.'
6+
dx = (1, -1, 0, 0)
7+
dy = (0, 0, 1, -1)
8+
9+
10+
def delete_block():
11+
for x, y in blocks:
12+
board[x][y] = EMPTY
13+
14+
15+
def update_board():
16+
for y in range(6):
17+
for t in range(10, -1, -1):
18+
for x in range(11, t, -1):
19+
if board[x][y] == EMPTY and board[t][y] != EMPTY:
20+
board[x][y], board[t][y] = board[t][y], EMPTY
21+
22+
23+
def is_in_area(x, y):
24+
return 0 <= x < 12 and 0 <= y < 6
25+
26+
27+
def bfs(x, y):
28+
queue = deque([(x, y)])
29+
visited[x][y] = True
30+
same_blocks = [(x, y)]
31+
while queue:
32+
x, y = queue.popleft()
33+
for d in range(4):
34+
nx = x + dx[d]
35+
ny = y + dy[d]
36+
if is_in_area(nx, ny) and not visited[nx][ny] and board[x][y] == board[nx][ny]:
37+
queue.append((nx, ny))
38+
visited[nx][ny] = True
39+
same_blocks.append((nx, ny))
40+
return same_blocks
41+
42+
43+
44+
board = [[*input().rstrip()] for _ in range(12)]
45+
flag = True
46+
res = 0
47+
while flag:
48+
flag = False
49+
visited = [[False] * 6 for _ in range(12)]
50+
for i in range(12):
51+
for j in range(6):
52+
if board[i][j] != EMPTY and not visited[i][j]:
53+
blocks = bfs(i, j)
54+
if len(blocks) >= 4:
55+
flag = True
56+
delete_block()
57+
if flag:
58+
update_board()
59+
res += 1
60+
print(res)

0 commit comments

Comments
 (0)