-
Notifications
You must be signed in to change notification settings - Fork 0
/
Puyo Puyo.py
74 lines (59 loc) · 1.67 KB
/
Puyo Puyo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
시작 시간: 2023-01-04 11:37 PM
소요 시간: 1시간 10분
풀이 방법: 무한루프,,
"""
def get_connection(x, y, is_visited):
global MAP, WIDTH, HEIGHT
color = MAP[x][y]
connection = []
stack = [ (x, y) ]
diffs = ((-1, 0), (1, 0), (0, 1), (0, -1))
while stack:
x, y = stack.pop()
if not (0 <= x < HEIGHT) or not (0 <= y < WIDTH) or is_visited[x][y] or color != MAP[x][y]:
continue
for dx, dy in diffs:
nx, ny = x + dx, y + dy
stack.append((nx, ny))
is_visited[x][y] = True
connection.append((x, y))
return connection
def explode(bombs):
global MAP, BLK
for bomb in bombs:
for x, y in bomb:
MAP[x][y] = BLK
def set_gravity():
global MAP, WIDTH, HEIGHT
puyo_heights = [HEIGHT-1] * WIDTH
for x in range(HEIGHT - 1, -1, -1):
for y in range(WIDTH):
if MAP[x][y] == BLK:
while puyo_heights[y] >= 0 and puyo_heights[y] == BLK:
puyo_heights[y] -= 1
MAP[x][y] = MAP[puyo_heights[y]][y]
MAP[puyo_heights[y]][y] = BLK
puyo_heights[y] -= 1
BLK = '.'
WIDTH = 6
HEIGHT = 12
MAP = []
for _ in range(HEIGHT):
MAP.append(list(input()))
ans = 0
while True:
is_visited = [[False]*WIDTH for _ in range(HEIGHT)]
bombs = []
for x in range(HEIGHT - 1, -1, -1):
for y in range(0, WIDTH):
bomb = get_connection(x, y, is_visited)
if len(bomb) < 4:
continue
bombs.append(bomb)
if len(bombs) < 0:
break
explode(bombs)
set_gravity()
ans += 1
print(ans)