Skip to content

Commit 271191f

Browse files
committed
[BOJ]#11559. 뿌요뿌요/골드4/3hour
https://www.acmicpc.net/problem/11559
1 parent ee548e9 commit 271191f

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

Hongjoo/백준/뿌요뿌요.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
20.19
3+
https://www.acmicpc.net/problem/11559
4+
BOJ.#11559_Gold4
5+
6+
#problem
7+
- goal) 연쇄 "연속" 횟수 구하기
8+
[condition]
9+
- 1연쇄 : 해당 turn 에서 상하좌우 4개 연결 -> 삭제
10+
- 삭제 후 위에 있는 element 는 하강
11+
12+
- 입력: 현 filed 상황(12x6)
13+
- (빈공간) / R, G, B, P, Y
14+
- 빈공간 : 0
15+
- 색상 : R,G, B, P, Y = 1,2,3,4,5
16+
[flow] # BFS
17+
1. 상하좌우 연쇄 확인
18+
- 연쇄 확인
19+
- 삭제
20+
- 연쇄 횟수 증가
21+
22+
"""
23+
import sys
24+
from collections import deque
25+
input = sys.stdin.readline
26+
#1. field 현황 리스트에 담기
27+
field = [list(input())[:-1] for _ in range(12)]
28+
29+
#2. bfs
30+
dy = [-1,1,0,0]
31+
dx =[0,0,-1,1]
32+
33+
34+
def refine_field(x) : # 중간 빈자리
35+
stack = deque()
36+
#1.아레=> 위로 스택에 뿌요뿌요 순서대로 축척하기
37+
for ny in range(11, -1,-1):
38+
if field[ny][x] != ".":
39+
stack.append(field[ny][x])
40+
for ny in range(11, -1, -1) :
41+
if stack :
42+
field[ny][x] = stack.popleft()
43+
else :
44+
field[ny][x] = "."
45+
46+
# 연쇄 확인 및 터짐
47+
def bfs(sy,sx):
48+
q = deque()
49+
q.append([sy,sx])
50+
pop_positions = [[sy,sx]]
51+
cur_color = field[sy][sx]
52+
visited.append([sy,sx])
53+
while q :
54+
cy,cx = q.popleft()
55+
for d in range(4):
56+
ny ,nx = cy + dy[d] ,cx + dx[d]
57+
# 같은 색상 -> 삭제 등록하기
58+
if 0<= ny<12 and 0<= nx < 6 and [ny,nx] not in visited and field[ny][nx] == cur_color :
59+
q.append([ny,nx])
60+
pop_positions.append([ny,nx])
61+
visited.append([ny,nx])
62+
63+
#2) 터짐 확인
64+
if len(pop_positions) >= 4 :
65+
for y,x in pop_positions :
66+
field[y][x] = "."
67+
return True
68+
return False# 안 터짐
69+
70+
answer = 0
71+
flag = True
72+
k = 0
73+
while flag :
74+
visited = []
75+
flag =False
76+
#1. 전체 field에서 뿌요뿌요 탐색
77+
for i in range(12):
78+
for j in range(6):
79+
if field[i][j]!="." and [i,j] not in visited:
80+
now_f = bfs(i,j) # 2.해당 위치에서 터짐 여부 확인
81+
flag = flag or now_f
82+
#2. 연쇄 계수 추가
83+
if not flag : # False - 안터짐
84+
break
85+
else :
86+
for row in range(6):
87+
refine_field(row)
88+
# 현재 turn 에서 1번 이상 터짐
89+
answer += 1
90+
print(answer)

0 commit comments

Comments
 (0)