| ps | |
|---|---|
| 링크 | programmers.co.kr/… |
| 출처 | 프로그래머스 |
| 문제 번호 | 42894 |
| 문제명 | 블록 게임 |
| 레벨 | Level 4 |
| 분류 |
구현 |
| 시간복잡도 | O(n^2) |
| 인풋사이즈 | n<=50 |
| 사용한 언어 | Python |
| 해결날짜 | 2020/12/27 |
11 12 12 22
"""Solution code for "Programmers 42894. 블록 게임".
- Problem link: https://programmers.co.kr/learn/courses/30/lessons/42894
- Solution link: http://www.teferi.net/ps/problems/programmers/42894
"""
import collections
import operator
EMPTY = 0
BLACK = -1
def can_remove(block, block_coords, board):
if len(block_coords) != 4:
return False
r1 = min(block_coords, key=operator.itemgetter(0))[0]
r2 = max(block_coords, key=operator.itemgetter(0))[0]
c1 = min(block_coords, key=operator.itemgetter(1))[1]
c2 = max(block_coords, key=operator.itemgetter(1))[1]
for r in range(r1, r2 + 1):
if any(x not in (BLACK, block) for x in board[r][c1:c2 + 1]):
return False
return True
def fill_black(board, r, c):
board[r][c] = (BLACK if r == 0 or board[r - 1][c] == BLACK else EMPTY)
def solution(board):
N = len(board)
removed_count = 0
coords_by_block = collections.defaultdict(list)
for r in range(N):
for c in range(N):
block = board[r][c]
if block == EMPTY:
fill_black(board, r, c)
else:
block_coords = coords_by_block[block]
block_coords.append((r, c))
if not can_remove(block, block_coords, board):
continue
removed_count += 1
for block_r, block_c in block_coords:
fill_black(board, block_r, block_c)
return removed_count