:link:문제 링크

[31909 FOCUS]

:question:문제 설명

0~7번 위치에있는 키가 존재하고 명령이 주어질때마다 각 키의 위치를 바꿀때 특정 키의 위치를 찾는 문제

:pencil2:코드

import sys
input = sys.stdin.readline
from collections import defaultdict


N = int(input())
dict = defaultdict(int)
for i in range(8):
    dict[i] = i
arr = list(map(int, input().split()))
num = int(input())
for x in arr:
    tmp = list(bin(x)[2:][::-1])
    if tmp.count("1") != 2:
        continue
    res = []
    for i in range(len(tmp)):
        if tmp[i] == "1":
            res.append(i)
    i, j = res[0], res[1]
    dict[i], dict[j] = dict[j], dict[i]

print(dict[num])

:memo:풀이

주어지는 숫자를 2진수로 변환하고 2진수에서 1의 등장 횟수가 2인 경우에만 KEY의 위치를 바꿔주었다. 생각보다 어렵지는 않은 문제!

나중에 로봇이 오작동하면 이런 알고리즘이 쓰일 수도 있겠다는 생각이 들었다..!

댓글남기기