ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 카카오 신입 공채 1차 코딩 테스트 문제 6. 프렌즈4블록(난이도: 상) (Python)
    공부 2017. 12. 4. 16:17
    반응형

    2017/11/03 - [공부] - 카카오 신입 공채 1차 코딩 테스트 문제 6. 프렌즈4블록(난이도: 상)


    Java에 이어 Python으로도 작성해 봤다.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    def find_and_remove_44_blocks(m, n, board):
        num_of_remove = 0
     
        board_to_remove = [[False for col in range(n)] for row in range(m)]
     
        # find to remove
        for 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] = True
                        board_to_remove[i][j + 1= True
                        board_to_remove[i + 1][j + 1= True
                        board_to_remove[i + 1][j] = True
     
        for 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 += 1
     
        while True:
            is_moved = False
            for i in range(m - 10-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 = True
            if not is_moved:
                break
     
        return num_of_remove
     
     
    def question6(m, n, board):
        total_num_of_remove = 0
        is_done = True
     
        while is_done:
            temp_num_of_remove = find_and_remove_44_blocks(m, n, board)
     
            if temp_num_of_remove == 0:
                is_done = False
            else:
                total_num_of_remove += temp_num_of_remove
     
        return total_num_of_remove
     
    cs


    반응형
Designed by Tistory.