-
카카오 신입 공채 1차 코딩 테스트 문제 6. 프렌즈4블록(난이도: 상) (Python)공부 2017. 12. 4. 16:17반응형
2017/11/03 - [공부] - 카카오 신입 공채 1차 코딩 테스트 문제 6. 프렌즈4블록(난이도: 상)
Java에 이어 Python으로도 작성해 봤다.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253def find_and_remove_44_blocks(m, n, board):num_of_remove = 0board_to_remove = [[False for col in range(n)] for row in range(m)]# find to removefor i in range(0, m - 1):for j in range(0, n - 1):if board[i][j] != ' ':if board[i][j] == board[i][j + 1] \and board[i][j] == board[i + 1][j + 1] \and board[i][j] == board[i + 1][j]:board_to_remove[i][j] = Trueboard_to_remove[i][j + 1] = Trueboard_to_remove[i + 1][j + 1] = Trueboard_to_remove[i + 1][j] = Truefor i in range(m):for j in range(n):if board_to_remove[i][j]:# board[i][j] = ' 'board[i] = board[i][:j] + ' ' + board[i][j + 1:]num_of_remove += 1while True:is_moved = Falsefor i in range(m - 1, 0, -1):for j in range(n):if board[i][j] == ' ' and board[i - 1][j] != ' ':board[i] = board[i][:j] + board[i - 1][j] + board[i][j + 1:]# board[i-1][j] = ' 'board[i - 1] = board[i - 1][:j] + ' ' + board[i - 1][j + 1:]is_moved = Trueif not is_moved:breakreturn num_of_removedef question6(m, n, board):total_num_of_remove = 0is_done = Truewhile is_done:temp_num_of_remove = find_and_remove_44_blocks(m, n, board)if temp_num_of_remove == 0:is_done = Falseelse:total_num_of_remove += temp_num_of_removereturn total_num_of_removecs 반응형