[포기] 자물쇠와 열쇠

developsy·2022년 7월 3일
0

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

구현 문제인데, 테스트 케이스에는 맞는 코드가 작성되긴 했으나 62점 정도만 테스트에서 통과하여 아무리 생각해도 반례가 생각나지 않아서 결국 포기한 문제이다... 나중에 생각나면 다시 풀어봐야 겠다.

#p324
#열쇠와 자물쇠의 크기가 다를 경우, 
import copy
#시계방향으로 회전시키는 함수.
def rotate(arr):
    result = [[0] * (len(arr)) for i in range(len(arr))]
    for y in range(len(arr)):
        for x in range(len(arr) - 1, -1, -1):
            result[y][abs(x-(len(arr)-1))] = arr[x][y]
    return result

#미리 구한 key를 이동한 1의 좌표와 lock배열을 넣어서 비교한다
def iscorrect(key, lock):
    count1 = len(key)
    count2 = 0
    position2 = []
    isequal = False

    for i in range(len(lock)):
        for j in range(len(lock)):
            if lock[i][j] == 0:
                position2.append((i, j))
                count2 += 1
    position2.sort()
    
    for i in range(len(lock)):
        for j in range(len(lock)):
            c = [(x[0] - i, x[1] - j) for x in key]
            c.sort()
            if c == position2:
                isequal = True
            if count1 == count2 and count1 > 0 and isequal:
                return True
    return False

def getposition(arr):
    position = []
    for i in range(len(arr)):
        for j in range(len(arr)):
            if arr[i][j] == 1:
                position.append((i, j))
    return position

#count 4될때까지 먼저 for문으로 이동할 수 있는 모든 경우의 수 따져보고 없다면 회전, 또 따져보고 없다면 회전...
def solution(key, lock):
    answer = True
    arr = key
    for i in range(4):
        arr = rotate(arr)
        #이동은 좌표로만 생각하자
        for x in range(-(len(key)), len(key)+1):
            for y in range(-(len(key)), len(key)+1):
                a = [(pos[0] + x, pos[1] + y) for pos in getposition(arr)]
                #구한 a리스트에서 인덱스를 벗어나는 원소는 제외
                b = [i for i in a if (0 <= i[0] <= (len(arr)-1)) and (0 <= i[1] <= (len(arr)-1))]
                if iscorrect(b, lock):
                    return answer
              
    answer = False
    return answer


solution([[0,1,1], [1,1,1], [1,1,1]], [[1,1,1], [1,0,1], [1,1,1]])
profile
공부 정리용 블로그

0개의 댓글