https://programmers.co.kr/learn/courses/30/lessons/17679?language=python3
✅ 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 |