본문 바로가기
알고리즘/백준

[백준]230225 문제풀이

by stubborngastropod 2023. 2. 25.
728x90

11559. Puyo Puyo

from collections import deque

dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]

def puyo(color):
    global ispuyo
    temp = []
    cnt = 1
    while queue:
        qpop = queue.popleft()
        temp.append(qpop)
        for d in range(4):
            ni, nj = qpop[0] + dy[d], qpop[1] + dx[d]
            if ni < 0 or ni >= 12 or nj < 0 or nj >= 6:
                continue
            if visited[ni][nj]:
                continue
            if field[ni][nj] == color:
                visited[ni][nj] = 1
                queue.append((ni, nj))
                cnt += 1

    if cnt >= 4:
        for delete in temp:
            field[delete[0]][delete[1]] = '.'
        ispuyo = True

def down():
    for column in range(6):
        while True:
            find = False
            for row in range(11, 0, -1):
                if field[row][column] == '.' and field[row - 1][column] != '.':
                    field[row][column], field[row - 1][column] = field[row - 1][column], '.'
                    find = True
            if not find:
                break

field = [list(input()) for _ in range(12)]
queue = deque()

puyopuyo = 0

while 1:
    ispuyo = False
    visited = [[0] * 6 for _ in range(12)]
    for r in range(12):
        for c in range(6):
            if field[r][c] != '.':
                queue.append((r, c))
                visited[r][c] = 1
                puyo(field[r][c])
    if ispuyo:
        down()
        puyopuyo += 1
    else:
        break

print(puyopuyo)

푸는데 꽤 오래 걸렸는데, 오류가 떠서 오래 걸린게 아니라 그냥 구현하는 과정이 굉장히 오래 걸렸다. 인접한 색깔들을 bfs로 터뜨리기, 동시에 터뜨릴 수 있는건 동시에 터뜨리고 다 터뜨린 후 바닥까지 내려주고 카운트하기... 이런 식으로 진행했는데 bfs, 조건문, 반복문, 배열 등에 대한 응용이 동시에 이루어져야해서 상당히 머리쓸게 많았다. 게임하듯이 푼 재미있었던 문제

 

인덱스 에러가 한번 떴는데, if continue 절에서 column의 범위를 잘못 설정해버린...^^; 자나깨나 인덱스 조심...

728x90

'알고리즘 > 백준' 카테고리의 다른 글

[백준]230228-0301 문제풀이  (0) 2023.03.01
[백준]230226 문제풀이  (0) 2023.02.26
[백준]230223-24 문제풀이  (0) 2023.02.24
[백준]230220 문제풀이  (0) 2023.02.21
[백준]230217 문제풀이  (0) 2023.02.18

댓글