본문 바로가기

컴퓨터/코테

프로그래머스5 - 방의 개수

def solution(arrows):
    direction = [
        [0,1],
        [1,1],
        [1,0],
        [1,-1],
        [0,-1],
        [-1,-1],
        [-1,0],
        [-1,1],
    ]
    answer = 0
    dic = {}
    path = {}
    x, y = 0, 0
    dic[str([x, y])] = True
    for item in arrows:
        for i in range(2) :
            nextx, nexty = x + direction[item][0], y + direction[item][1]
            pathKey = str([x, y, nextx, nexty])
            pathKey2 = str([nextx, nexty, x, y])
            dicKey = str([nextx, nexty])

            if not dicKey in dic :
                path[pathKey] = True
                path[pathKey2] = True
                dic[dicKey] = True
            else :
                if not pathKey in path :
                    path[pathKey] = True
                    path[pathKey2] = True
                    answer += 1
            x, y = nextx, nexty

    return answer

딕셔너리의 키 값을 입력할때

dicKey = f'{nextx}{nexty}' 는 테스트 케이스는 되는데 제출만 하면 40점도 안나왔다. 아무리봐도 로직에는 문제가 없어서 dicKey = str([nextx, nexty]) 이렇게 바꿔봤더니 잘 동작한다.... 후 힘들었다