핵심은
zip(*)
과copy.deepcopy
deepcopy를 통해서 사라질 수 있는 블록과 원본 블록판을 구별해서 check. -> 겹쳐진 블록들도 check 가능!
점수를 계산하고 사라진 블록만큼 -1로 채워놓은 후에 뒤에는 사라지지 않은 블록들로 업데이트.
score가 업데이트가 되지 않을 때까지 반복.
import copy
def solution(m, n, board):
board = list(map(list, zip(*board)))
def game(b):
score = 0
# 복사
tempB = copy.deepcopy(b)
for i in range(1,n):
for j in range(1,m):
if b[i][j] == -1: continue
if b[i][j] == b[i-1][j] == b[i-1][j-1] == b[i][j-1]:
tempB[i][j], tempB[i-1][j], tempB[i-1][j-1], tempB[i][j-1] = 0,0,0,0
for i,v in enumerate(tempB):
# 점수
cnt = v.count(0)
score += cnt
# 사라지는 블록만큼 왼쪽을 '-1'으로 채운다.
tempB[i] = [-1]*cnt + [a for a in v if a!=0]
return tempB, score
answer = 0
while True:
board, score = game(board)
if score == 0: return answer
answer += score