from copy import deepcopy

def solution(m, n, board):
    answer = 0
    board = [list(b) for b in board]

    while True:
        board = get_board_squares_removed(board)
        score = count_score(board)

        if answer!=0:
            score = score - answer
        if score==0:
            return answer
        answer += score

    return answer


def get_board_squares_removed(board):
    board_copy = deepcopy(board)
    for i in range(1, len(board)):
        for j in range(1, len(board[0])):
            if board[i-1][j-1]==board[i][j] and board[i-1][j]==board[i][j] and board[i][j-1]==board[i][j]:
                board_copy[i-1][j-1] = ''
                board_copy[i-1][j] = ''
                board_copy[i][j-1] = ''
                board_copy[i][j] = ''

    board = down_friends(board_copy)

    return board


def down_friends(board):
    for j in range(len(board[0])):
        for i in range(len(board)-2,-1,-1):
            if board[i][j]!='' and board[i+1][j]=='':
                down_idx = -1
                for k in range(i+1, len(board)):
                    if k==len(board)-1 or board[k+1][j]!='':
                        down_idx = k
                        break
                board[down_idx][j] = board[i][j]
                board[i][j] = ''

    return board


def count_score(board):
    score = 0
    for i in range(0, len(board)):
        for j in range(0, len(board[0])):
            if board[i][j]=='':
                score += 1

    return score

생각해보기

  • get_board_squares_removed()로 조건에 맞는 블록들을 제거해주고 count_score()로 점수를 더해나간다.
  • get_board_squares_removed() 안에서 실행되는 down_friends()는 빈공간 상단에 있는 블록들을 내려서 조건에 맞게 빈공간을 채워준다.

+ 따끈한 최근 게시물