title: "[프로그래머스] [1차] 프렌즈4블록 Python 파이썬 해설 (Level 2) - 이도훈"
cleanUrl: "programmers/17679"
description: "프로그래머스 Level 2 문제 [[1차] 프렌즈4블록]의 풀이를 정리합니다."

문제 설명 및 제한사항

아이디어 및 해결 방법

코드

from copy import deepcopy

def process(board):
    newboard = deepcopy(board)
    
    v = 0
    for r in range(len(board) - 1):
        for c in range(len(board[0]) - 1):
            if board[r][c] == board[r+1][c] == board[r][c+1] == board[r+1][c+1]:
                if board[r][c] != '-':
                    
                    if newboard[r][c] != '-':
                        v += 1
                    if newboard[r+1][c] != '-':
                        v += 1
                    if newboard[r][c+1] != '-':
                        v += 1
                    if newboard[r+1][c+1] != '-':
                        v += 1
                        
                    newboard[r][c] = '-'
                    newboard[r+1][c] = '-'
                    newboard[r][c+1] = '-'
                    newboard[r+1][c+1] = '-'
                    
    return v, newboard
            
def movedown(board):
    newboard = [['-' for _ in range(len(board[0]))] for _ in range(len(board))]
    
    for c in range(len(board[0])):
        level = len(board) - 1
        for r in range(len(board) - 1, -1, -1):
            if board[r][c] != '-':
                newboard[level][c] = board[r][c]
                level -= 1
    return newboard

def solution(m, n, board):
    answer = 0
    board = [list(r) for r in board]
    
    while True:
        v, board = process(board)
        answer += v
        if v == 0:
            return answer

        board = movedown(board)

출처

프로그래머스 코딩테스트 연습 https://school.programmers.co.kr/learn/challenges