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

[카카오 기출] 자물쇠와 열쇠

by ggyongi 2021. 9. 7.
반응형

https://programmers.co.kr/learn/courses/30/lessons/60059?language=python3 

 

코딩테스트 연습 - 자물쇠와 열쇠

[[0, 0, 0], [1, 0, 0], [0, 1, 1]] [[1, 1, 1], [1, 1, 0], [1, 0, 1]] true

programmers.co.kr

def solution(key, lock):
    m = len(key)
    n = len(lock)
    
    def rotate():
        rkey = [[0 for _ in range(m)] for _ in range(m)]
        for i in range(m):
            for j in range(m):
                rkey[j][-i+m-1] = key[i][j]
        return rkey            
            
    for _ in range(4):        
        # adjust the key's position
        for x in range(n+m):
            for y in range(n+m):               
                tried = [[0 for _ in range(n+2*m)] for _ in range(n+2*m)]
                for i in range(n):
                    for j in range(n):
                        tried[i+m][j+m] = lock[i][j]                    
                
                # sum
                for i in range(m):
                    for j in range(m):
                        tried[i+x][j+y] += key[i][j]
                
                #check
                stop = False
                for i in range(n):
                    for j in range(n):
                        if tried[i+m][j+m] != 1:
                            stop = True
                            break
                    if stop: break

                
                if not stop:
                    return True
                    
        key = rotate()
    return False
 

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

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

kmong.com

댓글