Skip to content

Commit 98c02ec

Browse files
committed
[BOJ] #11559. Puyo Puyo / 골드4 / 60분 / 실패
1 parent 8165038 commit 98c02ec

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
FIELD_X = 12
6+
FIELD_Y = 6
7+
8+
# 1. 입력
9+
field = [list(input().strip()) for _ in range(FIELD_X)]
10+
11+
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
12+
combo = 0
13+
14+
# 상하좌우로 동일한 블록을 탐색해 해당 좌표들을 가진 리스트 반환
15+
def bfs(x, y):
16+
queue = deque([(x, y)])
17+
color = field[x][y]
18+
visited[x][y] = True
19+
same_blocks = [(x, y)] # 같은 블록의 좌표 리스트
20+
21+
while queue:
22+
x, y = queue.popleft()
23+
for dx, dy in directions:
24+
nx, ny = x + dx, y + dy
25+
if (0 <= nx < FIELD_X and 0 <= ny < FIELD_Y) and \
26+
field[nx][ny] == color and not visited[nx][ny]:
27+
queue.append((nx, ny))
28+
visited[nx][ny] = True
29+
same_blocks.append((nx, ny))
30+
31+
return same_blocks
32+
33+
34+
# 동일한 블록 제거
35+
def delete(same_blocks):
36+
for x, y in same_blocks:
37+
field[x][y] = '.'
38+
39+
40+
# 반복문 돌면서 위에서 아래로 블록 내리기
41+
def down():
42+
for y in range(FIELD_Y):
43+
for x in range(10, -1, -1):
44+
for k in range(FIELD_X - 1, x, -1):
45+
if field[x][y] != '.' and field[k][y] == '.':
46+
field[k][y] = field[x][y]
47+
field[x][y] = '.'
48+
49+
50+
while True:
51+
pang = False
52+
visited = [[False for _ in range(FIELD_Y)] for _ in range(FIELD_X)]
53+
54+
for i in range(FIELD_X):
55+
for j in range(FIELD_Y):
56+
if field[i][j] != '.' and not visited[i][j]:
57+
same_blocks = bfs(i, j)
58+
59+
# 동일한 블록이 4개 이상일 경우 터트리기
60+
if len(same_blocks) >= 4:
61+
pang = True
62+
delete(same_blocks)
63+
64+
65+
# 터뜨린 블록이 있으면 밑으로 내리기
66+
if pang:
67+
down()
68+
combo += 1
69+
else:
70+
# 더이상 터뜨릴 게 없다면 종료
71+
break
72+
73+
print(combo)

0 commit comments

Comments
 (0)