본문 바로가기
알고리즘

[알고리즘] 특이한 자석 + 톱니바퀴

by keel_im 2021. 3. 10.
반응형

포인트

1. 구현을 얼마나 잘할 수 있는가? 나는 잘 할 수 있는가? 파이썬으로 구현을 해보았다. 

2. 조금 많이 비슷한 문제임을 알았다.

🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다. 

python

import sys

sys.stdin = open('input.txt')


def reverse_turn(dir: int) -> int:
	"""
	반대 방향을 나타내는 함수
	:param dir:
	:return:
	"""
	return -1 if dir == 1 else 1


def wheel(n: int, d: int) -> None:
	"""
	n 번 톱니바퀴를 돌려줍니다.
	:param n:
	:param d:
	"""
	if d == 1:
		data[n].insert(0, data[n].pop(-1))
	else:
		data[n].append(data[n].pop(0))


def turn(n: int, d: int):
	"""
	실질적인 동작을 하는 함수
	:param n: 
	:param d: 
	"""
	nd = reverse_turn(d)
	
	if n == 0:
		if data[0][2] != data[1][6]:
			if data[1][2] != data[2][6]:
				if data[2][2] != data[3][6]:
					wheel(0, d)
					wheel(1, nd)
					wheel(2, d)
					wheel(3, nd)
				else:
					wheel(0, d)
					wheel(1, nd)
					wheel(2, d)
			else:
				wheel(0, d)
				wheel(1, nd)
		else:
			wheel(0, d)
	elif n == 1:
		if data[0][2] != data[1][6]:
			wheel(0, nd)
		
		if data[1][2] != data[2][6]:
			if data[2][2] != data[3][6]:
				wheel(1, d)
				wheel(2, nd)
				wheel(3, d)
			else:
				wheel(1, d)
				wheel(2, nd)
		else:
			wheel(1, d)
	elif n == 2:
		if data[2][2] != data[3][6]:
			wheel(3, nd)
		
		if data[1][2] != data[2][6]:
			if data[0][2] != data[1][6]:
				wheel(2, d)
				wheel(1, nd)
				wheel(0, d)
			else:
				wheel(2, d)
				wheel(1, nd)
		else:
			wheel(2, d)
	elif n == 3:
		if data[2][2] != data[3][6]:
			if data[1][2] != data[2][6]:
				if data[0][2] != data[1][6]:
					wheel(3, d)
					wheel(2, nd)
					wheel(1, d)
					wheel(0, nd)
				else:
					wheel(3, d)
					wheel(2, nd)
					wheel(1, d)
			else:
				wheel(3, d)
				wheel(2, nd)
		else:
			wheel(3, d)


for test in range(1, int(input()) + 1):
	k = int(input())
	
	data = [list(map(int, input().split())) for _ in range(4)]
	
	for _ in range(k):
		a, b = map(int, input().split())
		a -= 1
		turn(a, b)
	
	result = 0
	if data[0][0] == 1:
		result += 1
	if data[1][0] == 1:
		result += 2
	if data[2][0] == 1:
		result += 4
	if data[3][0] == 1:
		result += 8
	
	print('#{} {}'.format(test, result))

python deque 추가 버전

import sys
from collections import deque

sys.stdin = open('input.txt')


def reverse_turn(dir: int):
    return -1 if dir == 1 else 1


def wheel(n: int, d: int):
    data[n].rotate(d)


def turn(n: int, d: int):
    nd = reverse_turn(d)

    if n == 0:
        if data[0][2] != data[1][6]:
            if data[1][2] != data[2][6]:
                if data[2][2] != data[3][6]:
                    wheel(0, d)
                    wheel(1, nd)
                    wheel(2, d)
                    wheel(3, nd)
                else:
                    wheel(0, d)
                    wheel(1, nd)
                    wheel(2, d)
            else:
                wheel(0, d)
                wheel(1, nd)
        else:
            wheel(0, d)
    elif n == 1:
        if data[0][2] != data[1][6]:
            wheel(0, nd)

        if data[1][2] != data[2][6]:
            if data[2][2] != data[3][6]:
                wheel(1, d)
                wheel(2, nd)
                wheel(3, d)
            else:
                wheel(1, d)
                wheel(2, nd)
        else:
            wheel(1, d)
    elif n == 2:
        if data[2][2] != data[3][6]:
            wheel(3, nd)

        if data[1][2] != data[2][6]:
            if data[0][2] != data[1][6]:
                wheel(2, d)
                wheel(1, nd)
                wheel(0, d)
            else:
                wheel(2, d)
                wheel(1, nd)
        else:
            wheel(2, d)
    elif n == 3:
        if data[2][2] != data[3][6]:
            if data[1][2] != data[2][6]:
                if data[0][2] != data[1][6]:
                    wheel(3, d)
                    wheel(2, nd)
                    wheel(1, d)
                    wheel(0, nd)
                else:
                    wheel(3, d)
                    wheel(2, nd)
                    wheel(1, d)
            else:
                wheel(3, d)
                wheel(2, nd)
        else:
            wheel(3, d)


for test in range(1, int(input()) + 1):
    k = int(input())

    data = [deque(map(int, input().split())) for _ in range(4)]

    for _ in range(k):
        a, b = map(int, input().split())
        a -= 1
        turn(a, b)

    result = 0
    if data[0][0] == 1:
        result += 1
    if data[1][0] == 1:
        result += 2
    if data[2][0] == 1:
        result += 4
    if data[3][0] == 1:
        result += 8

    print('#{} {}'.format(test, result))
반응형

'알고리즘' 카테고리의 다른 글

[알고리즘] 위장  (0) 2021.03.11
[알고리즘] 뱀  (0) 2021.03.11
[알고리즘] 가장 먼 노드  (0) 2021.03.09
[알고리즘] 홈 방범 서비스  (0) 2021.03.08
[알고리즘] 원자 소멸 시뮬레이션  (0) 2021.03.08

댓글