본문 바로가기
Problem Solving/카카오 코딩테스트

[카카오 기출] 기둥과 보 설치

by ggyongi 2021. 9. 7.
반응형

https://programmers.co.kr/learn/courses/30/lessons/60061

 

코딩테스트 연습 - 기둥과 보 설치

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

programmers.co.kr

너무 빡코딩이라 스트레쓰를 받는다.

맨정신엔 못푼다.

def solution(n, build_frame):
    board = [[[False for _ in range(2)] for _ in range(n+1)] for _ in range(n+1)]
    
    def col_check(x, y):
        if y == 0:
            return True
        
        if 0<=x<=n and 0<=y<=n and board[x][y][1]:
            return True
        if 0<=x-1<=n and 0<=y<=n and board[x-1][y][1]:
            return True
        if 0<=x<=n and 0<=y-1<=n and board[x][y-1][0]:
            return True
        
        return False
    
    def bo_check(x,y):
        if 0<=x-1<=n and 0<=y<=n and board[x-1][y][1]:
            if 0<=x+1<=n and 0<=y<=n and board[x+1][y][1]:
                return True
        
        if (0<=x+1<=n and 0<=y-1<=n and board[x+1][y-1][0]) or (0<=x<=n and 0<=y-1<=n and board[x][y-1][0]):
                return True
        

        return False

    for x, y, a, b in build_frame:
        if b == 0: # delete      
            if a == 0: # column
                board[x][y][0] = False
                
 
                possible = True
                for i in range(n+1):
                    for j in range(n+1):
                        if board[i][j][0] and not col_check(i,j):
                            possible = False
                        if board[i][j][1] and not bo_check(i,j):
                            possible = False

                    
                if not possible:
                    board[x][y][0] = True

            else : # bo
                board[x][y][1] = False
                
                possible = True
                for i in range(n+1):
                    for j in range(n+1):
                        if board[i][j][0] and not col_check(i,j):
                            possible = False
                        if board[i][j][1] and not bo_check(i,j):
                            possible = False

                if not possible:
                    board[x][y][1] = True
                
        else: # setup
            
            if a == 0: # column
                if col_check(x,y):
                    board[x][y][0] = True
            else : # bo
                if bo_check(x,y):
                    board[x][y][1] = True
    
    answer = []
    
    for i in range(n+1):
        for j in range(n+1):
            for k in range(2):
                if board[i][j][k]:
                    answer.append([i,j,k])
    
    
    return answer

 

 

비전공자 네카라 신입 취업 노하우

시행착오 끝에 얻어낸 취업 노하우가 모두 담긴 전자책!

kmong.com

댓글