Post

[BOJ] Puyo Puyo - 11559 (G4)

[BOJ] Puyo Puyo - 11559 (G4)
시간 제한메모리 제한
1 초256 MB

문제

뽀요뽀요 게임 시뮬레이션 문제이다. 같은 색 뽀요가 4개 이상 연결되면 터지고, 연쇄 작용이 발생한다.

풀이

BFS + 시뮬레이션 문제이다.

코드

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
field = list(map(list, [input() for _ in range(12)]))

dyx = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def bfs(sy, sx):
    visited = set()
    q = [(sy, sx)]
    visited.add((sy, sx))
    while q:
        y, x = q.pop(0)
        for dy, dx in dyx:
            ny, nx = y + dy, x + dx
            if not(0 <= ny < 12 and 0 <= nx < 6):
                continue
            if (ny, nx) in visited:
                continue
            
            if field[ny][nx] == field[sy][sx]:
                visited.add((ny, nx))
                q.append((ny, nx))
    
    if len(visited) >= 4:
        for y, x in visited:
            field[y][x] = '.'
        return True
    return False

cnt = 0
while True:
    is_remove = False
    for i in range(12):
        for j in range(6):
            if field[i][j] == '.':
                continue

            if bfs(i, j):
                is_remove = True

    if not is_remove:
        break

    # 블록 내리기
    for j in range(6):
        stack = []
        for i in range(11, -1, -1):
            if field[i][j] == '.':
                continue
            
            stack.append(field[i][j])
            field[i][j] = '.'
        
        i = 11
        while stack:
            field[i][j] = stack.pop(0)
            i -= 1

    cnt += 1

print(cnt)

시간 복잡도

O(12 × 6 × 연쇄수)

This post is licensed under CC BY 4.0 by the author.