File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed
YoonYn9915/implementation Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments