ye0nn
영차영차
ye0nn
전체 방문자
오늘
어제
  • 분류 전체보기 (61)
    • CS (0)
      • 운영체제 (0)
      • 네트워크 (0)
      • 알고리즘 & 자료구조 (0)
    • 코딩테스트 (48)
      • 프로그래머스 (40)
      • 백준 (8)
    • 프로그래밍 (11)
      • 프론트엔드 (3)
      • 자바스크립트 (0)
      • 스위프트 (7)
      • 파이썬 (1)
    • 취준기록 (1)

인기 글

블로그 메뉴

  • 홈
  • 태그
  • 방명록

티스토리

hELLO · Designed By 정상우.
ye0nn

영차영차

[프로그래머스] Level 2 - [1차] 프렌즈4블록 (Python)
코딩테스트/프로그래머스

[프로그래머스] Level 2 - [1차] 프렌즈4블록 (Python)

2022. 5. 17. 00:05

 

 

 

https://programmers.co.kr/learn/courses/30/lessons/17679?language=python3

 

코딩테스트 연습 - [1차] 프렌즈4블록

프렌즈4블록 블라인드 공채를 통과한 신입 사원 라이언은 신규 게임 개발 업무를 맡게 되었다. 이번에 출시할 게임 제목은 "프렌즈4블록". 같은 모양의 카카오프렌즈 블록이 2×2 형태로 4개가 붙

programmers.co.kr

 

 

 

✅  Solution

  • 요소에 쉽게 접근하기 위해 board 를 2차원 배열로 만든다.
  • board 전체 돌면서 2x2 로 같은 블록이 있는지 확인 후, delete 배열에 넣어준다.
  • delete 배열안에서 겹치는 좌표가 있을 수 있으므로 집합으로 변환해준다.
  • 2x2 요소를 모두 제거 후, 빈 공간이 있을 때 위에서 아래로 내려준다.

 

 

 

✅ Code

def move_block(m, n, board):
    for y in range(n):
        for x in range(m - 1, -1, -1):
            if board[x][y] == '':
                nx = x - 1
                while nx >= 0:
                    if board[nx][y] != '':
                        board[x][y] = board[nx][y]
                        board[nx][y] = ''
                        break
                    else:
                        nx -= 1
    return board

def delete_block(m, n, board):
    delete = []

    for x in range(m - 1):
        for y in range(n - 1):
            if board[x][y] == '':
                continue

            if board[x][y] == board[x][y + 1] and board[x][y] == board[x + 1][y] and board[x][y] == board[x + 1][y + 1]:
                delete.append((x, y))
                delete.append((x + 1, y))
                delete.append((x, y + 1))
                delete.append((x + 1, y + 1))

    delete = set(delete)

    for x, y in delete:
        board[x][y] = ''

    return board, len(delete)


def solution(m, n, board):
    answer = 0
    board = [list(x) for x in board]
    board, count = delete_block(m, n, board)
    answer += count

    while count > 0:
        move_block(m, n, board)
        board, count = delete_block(m, n, board)
        answer += count

    return answer
저작자표시 (새창열림)

'코딩테스트 > 프로그래머스' 카테고리의 다른 글

[프로그래머스] Level 2 - [3차] 방금그곡 (Python)  (0) 2022.05.18
[프로그래머스] Level 2 - [1차] 캐시 (Python)  (0) 2022.05.17
[프로그래머스] Level 2 - 튜플 (Python)  (0) 2022.05.13
[프로그래머스] Level 2 - 후보키 (Python)  (0) 2022.05.13
[프로그래머스] Level 2 - 메뉴 리뉴얼 (Python)  (0) 2022.05.13
    '코딩테스트/프로그래머스' 카테고리의 다른 글
    • [프로그래머스] Level 2 - [3차] 방금그곡 (Python)
    • [프로그래머스] Level 2 - [1차] 캐시 (Python)
    • [프로그래머스] Level 2 - 튜플 (Python)
    • [프로그래머스] Level 2 - 후보키 (Python)
    ye0nn
    ye0nn
    프론트엔드 개발자의 개발기록

    티스토리툴바