CLOCKSYNC.py

Loopy·2021년 8월 4일
0

algorlsm

목록 보기
2/2

문제는 다음에 있다.
알고스팟

책에 나오는 의사코드대로 풀기로 했다.

N = int(input())
arr = [-1] * 10
success = -1
linked = [
    [0, 1, 2],
    [3, 7, 9, 11],
    [4, 10, 14, 15],
    [0, 4, 5, 6, 7],
    [6, 7, 8, 10, 12],
    [0, 2, 14, 15],
    [3, 14, 15],
    [4, 5, 7, 14, 15],
    [1, 2, 3, 4, 5],
    [3, 4, 5, 9, 13]
]

def areAligned():
    global arr
    for i in arr:
        if i != 12:
            return False
    return True

def push(swtch):
    global linked, arr
    for i in linked[swtch]:
        if arr[i] == 12: arr[i] = 3
        else: arr[i] += 3

def solve(swtch):
    if(swtch == 10): return 0 if areAligned() else 9999;
    ret = 9999
    for i in range(4):
        ret = min(ret, i + solve(swtch+1))
        push(swtch)
    return ret

def init():
    global arr
    arr = list(map(int, input().split()))

for i in range(N):
    init()
    print(solve(0))

알고스팟 들어가서 제출을 하면 timeout으로 틀림 처리가 된다.
그래도 문제 풀이 방식은 맞다고 생각이 된다.

고칠점

global을 너무 자주 쓰는 것 같다. 다른 언어(특히 javascript)에서 많이 써서 그런거 같다. 이걸 고칠 방법에 대해서 고민을 해야겠다.

profile
With Rust?

0개의 댓글