프로그래머스/LEVEL 2
[1차] 프렌즈4블록
GenieLove!
2022. 4. 24. 22:55
728x90
반응형
Python
positions = ((0, 0), (0, 1), (1, 0), (1, 1))
board_list = []
def solution(m, n, board):
global board_list
column = 0
row = 0
board_list = [list(s) for s in board]
moved = False#옮긴게 있는지 체크
remove_indexs = []#삭제할 indexs 저장 리스트
count = 0
while True:
if row >= n - 1:
column += 1
row = 0
if column >= m - 1:#끝까지 다 검사했으면
if not moved:
break
count += move_board(remove_indexs)
moved = False#다시 처음으로 세팅
column, row = 0, 0#다시 처음부터 시작
remove_indexs = []
found, indexs = find_correct((column, row))
if found:#찾았으면
moved = True#움직였음
remove_indexs.extend(indexs)#indexs를 삭제할 remove_indexs에 더함
row += 1
return count
#positions에 저장한 값과 비교
def find_correct(indexs):
global board_list
global positions
column, row = indexs
now_str = board_list[column][row]
if now_str == "#":# #이 저장되어있으면 빈 것이므로 넘어감
return False, []
result_indexs = []#결과 indexs 저장될 리스트
found = True
for x, y in positions:
if now_str != board_list[column + x][row + y]:#일치하지 않으면 (2 * 2가 맞지 않음)
found = False
result_indexs = []
break
result_indexs.append((column + x, row + y))#일치하면 indexs list에 append
return found, result_indexs#2 * 2로 맞으면 found = True
def move_board(remove_indexs):#해당 인덱스에 있는 값 삭제
global board_list
count = 0
#remove_indexs를 위 값부터 움직이기 위해서 sorted
for indexs in sorted(list(set(remove_indexs)), key= lambda x: (x[0], x[1])):
count += 1
column, row = indexs
for i in range(column, 0, -1):#현재 행의 위부터 하나씩 아래로 내리기
board_list[i][row] = board_list[i - 1][row]
board_list[0][row] = '#'#맨 위는 채워지지 않으므로 #로 저장
return count#총 삭제된 count 저장
728x90
반응형