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