def solution(m, n, board):
answer = 0
maps = []
# maps 좌표 만들기
for i in range(0, m):
tmp = []
for j in range(0, n):
tmp.append(board[i][j])
maps.append(tmp)
while True:
rm = []
# 지워질 블록 수집
for x in range(0, m-1):
for y in range(0, n-1):
if maps[x][y] == maps[x+1][y] and maps[x][y+1] == maps[x+1][y+1] and maps[x][y] == maps[x+1][y+1] and maps[x][y] != 0:
rm.append((x, y))
rm.append((x+1, y))
rm.append((x, y+1))
rm.append((x+1, y+1))
rm = set(rm)
answer += len(rm)
if not len(rm):
return answer
else:
# 지워질 블록 지우기
for rmv in rm:
maps[rmv[0]][rmv[1]] = 0
# 지워진 칸 채우기
while True:
mv = 0
for x in range(0, m-1):
for y in range(0, n):
if maps[x+1][y] == 0 and maps[x][y]: # 밑 칸이 비어있을 때
maps[x+1][y] = maps[x][y]
maps[x][y] = 0
mv = 1
else: # 밑에 칸이 비어있지 않을 때
continue
if mv == 0:
break
return answer
칸 내려서 채우기 부분에서
0 0
0 0
인 반례를 생각하지 못했다. maps[x+1][y] == 0만 했을땐 0도 계속 내리기 때문에 무한 반복이 되었는데
maps[x][y]가 0일때를 생각해 보니 0이 아니고, [x+1][y] == 0 이어야 조건이 성립하는 것.. 삽질했다..
'Programing > 자료구조, Algorithm' 카테고리의 다른 글
바이너리 서치 python (binary search python) (0) | 2022.04.15 |
---|---|
programmers 추석트래픽 [python] (0) | 2022.03.15 |
프로그래머스(programmers) 신고 결과 받기 -python- (0) | 2022.03.10 |
백준 1700 반례 (python) (0) | 2022.02.17 |
백준 1062 (python) (0) | 2022.02.16 |