cs [2019 KAKAO BLIND RECRUITMENT] 블록 (python) (95%)
본문 바로가기
  • 매일 한걸음씩
  • 매일 한걸음씩
개발/PYTHON 알고리즘 연습

[2019 KAKAO BLIND RECRUITMENT] 블록 (python) (95%)

by 시몬쯔 2021. 5. 5.
728x90
반응형

와우 넘 어려웠다. 테스트케이스14번이 안풀리는데 반례 계속 생각해봐도 안풀린다ㅠㅠ 한시간넘게 걸려서 푼듯...

역시 코테는 꾸준히 해야해

 

programmers.co.kr/learn/courses/30/lessons/42894#

 

코딩테스트 연습 - 블록 게임

[[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,4,0,0,0],[0,0,0,0,0,4,4,0,0,0],[0,0,0,0,3,0,4,0,0,0],[0,0,0,2,3,0,0,0,5,5],[1,2,2,2,3,3,0,0,0,5],[1,1,1,0,0,0,0,0,0,5]] 2

programmers.co.kr

import numpy as np
import itertools

def update_board(board, ele):
    for r, c in list(zip(list(np.where(board==ele)[0]),list(np.where(board==ele)[1]))):
        board[r,c] = 0
        
    return board

def check_shape(board, rc_tuples):
    

    row = set([x[0] for x in rc_tuples])
    col = set([x[1] for x in rc_tuples])
    
    needed = set(itertools.product(row,col)) - set(rc_tuples)   #채워넣어야할 블록 두개의 r,c tuple
    
    col = [x[1] for x in needed] #채울 col번호
    
    for r, c in needed:
        
        if (r-1,c) in rc_tuples or (r-2, c) in rc_tuples:
            return False

    
    for r,c in rc_tuples:

        try:
            if c in col and len(set(board[:r,c]))>1: 
                return False #위에 뭐가 있으면 못놓는당
        except:
            pass
    
    return True

    
def solution(board):
    
    answer = 0
    
    board = np.array(board)
    
    impo_cols = set()
  
    for i, row in enumerate(board):
        for j, ele in enumerate(row):
            if ele>0:
                if check_shape(board, list(zip(list(np.where(board==ele)[0]),list(np.where(board==ele)[1])))): # 제거 가능시 
                    answer += 1 
                    board = update_board(board, ele)
                    print(ele)
                    print(board)


    return answer
728x90
반응형

댓글