광규니
광규니네
광규니
전체 방문자
오늘
어제
  • 분류 전체보기 (154)
    • 알고리즘 (100)
      • 알고리즘 개념 (2)
      • 문제 풀이 (96)
    • 주절주절 (19)
      • 자격증, 활동 후기 (4)
      • 전시회 후기 (3)
      • 이모저모 (2)
      • 회고 (3)
      • 뜨럼 (7)
    • 운영체제 (9)
    • 개발 지식 (9)
      • Apple Watch (4)
      • MySQL (2)
      • Eclipse (1)
      • XCode (1)
    • 네트워크 공부 (1)
    • 데이터베이스 공부 (5)
    • Java 공부 (7)
    • TMP (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 자바
  • 합주
  • BOJ
  • 백준
  • 파이썬
  • BFS
  • OS
  • 알고리즘
  • 티스토리챌린지
  • 다이나믹 프로그래밍
  • 프로그래머스
  • 컴퓨터 사이언스
  • 애플워치 앱 만들기
  • DP
  • 애플워치 앱
  • 구현
  • 개념
  • 운영체제
  • 오블완
  • 드린이

최근 댓글

최근 글

티스토리

250x250
hELLO · Designed By 정상우.
광규니
알고리즘/문제 풀이

[Python/파이썬 14891 백준] 톱니바퀴

알고리즘/문제 풀이

[Python/파이썬 14891 백준] 톱니바퀴

2021. 3. 25. 23:15
반응형

www.acmicpc.net/problem/14891

 

14891번: 톱니바퀴

첫째 줄에 1번 톱니바퀴의 상태, 둘째 줄에 2번 톱니바퀴의 상태, 셋째 줄에 3번 톱니바퀴의 상태, 넷째 줄에 4번 톱니바퀴의 상태가 주어진다. 상태는 8개의 정수로 이루어져 있고, 12시방향부터

www.acmicpc.net

풀이

구현 문제입니다

케이스가 별로 없어서 노가다로 했습니다.

주석 보면서 이해하세용

#구현
# 시물레이션
from collections import deque
import sys
input=sys.stdin.readline


first=deque(map(int,input().strip()))
second=deque(map(int,input().strip()))
third=deque(map(int,input().strip()))
fourth=deque(map(int,input().strip()))

n=int(input())
cnt=0

for i in range(n):
    num,direction=map(int,input().split())

    #첫번째
    if num==1:
        #시계 방향
        if direction==1:
            #1,2,3,4 정 반 정 반
            if first[2] != second[6] and second[2]!=third[6] and third[2] != fourth[6]:
                first.appendleft(first.pop())
                second.append(second.popleft())
                third.appendleft(third.pop())
                fourth.append(fourth.popleft())
            #1 2 3
            elif first[2] != second[6] and second[2]!=third[6]:
                first.appendleft(first.pop())
                second.append(second.popleft())
                third.appendleft(third.pop())
            # 1 2
            elif first[2] != second[6]:
                first.appendleft(first.pop())
                second.append(second.popleft())
            # 1
            else:
                first.appendleft(first.pop())
        #반시계
        else:
            # 1 2 3 4 반 정 반 정
            if first[2] != second[6] and second[2]!=third[6] and third[2] != fourth[6]:
                first.append(first.popleft())
                second.appendleft(second.pop())
                third.append(third.popleft())
                fourth.appendleft(fourth.pop())
            # 12 3
            elif first[2] != second[6] and second[2]!=third[6]:
                first.append(first.popleft())
                second.appendleft(second.pop())
                third.append(third.popleft())
            # 12
            elif first[2] != second[6]:
                first.append(first.popleft())
                second.appendleft(second.pop())
            # 1
            else:
                first.append(first.popleft())

    #두번째
    elif num==2:
        # 두번째 정방향
        if direction==1:
            # 1 2 3 4 반정반정
            if first[2] != second[6] and second[2]!=third[6] and third[2] != fourth[6]:
                first.append(first.popleft())
                second.appendleft(second.pop())
                third.append(third.popleft())
                fourth.appendleft(fourth.pop())
            # 1 2 3 반 정 반
            elif first[2] != second[6] and second[2]!=third[6] :
                first.append(first.popleft())
                second.appendleft(second.pop())
                third.append(third.popleft())
            # 2 34 정 반 정
            elif second[2]!=third[6] and third[2] != fourth[6]:
                second.appendleft(second.pop())
                third.append(third.popleft())
                fourth.appendleft(fourth.pop())
            # 1 2 반 정
            elif first[2] != second[6]:
                first.append(first.popleft())
                second.appendleft(second.pop())
            # 23 정 반
            elif second[2]!=third[6] :
                second.appendleft(second.pop())
                third.append(third.popleft())
            # 2 정
            else:
                second.appendleft(second.pop())
        #역방향
        else:
            #1,2,3,4 정 반 정 반
            if first[2] != second[6] and second[2]!=third[6] and third[2] != fourth[6]:
                first.appendleft(first.pop())
                second.append(second.popleft())
                third.appendleft(third.pop())
                fourth.append(fourth.popleft())
            #1 2 3
            elif first[2] != second[6] and second[2]!=third[6]:
                first.appendleft(first.pop())
                second.append(second.popleft())
                third.appendleft(third.pop())
            # 234
            elif second[2]!=third[6] and third[2] != fourth[6]:
                second.append(second.popleft())
                third.appendleft(third.pop())
                fourth.append(fourth.popleft())
            # 1 2
            elif first[2] != second[6]:
                first.appendleft(first.pop())
                second.append(second.popleft())
            # 23
            elif second[2]!=third[6]:
                second.append(second.popleft())
                third.appendleft(third.pop())  
            # 2
            else:
                second.append(second.popleft())
    #세번째
    elif num==3:
        if direction==1:
            #1,2,3,4 정 반 정 반
            if first[2] != second[6] and second[2]!=third[6] and third[2] != fourth[6]:
                first.appendleft(first.pop())
                second.append(second.popleft())
                third.appendleft(third.pop())
                fourth.append(fourth.popleft())
            # 2 3 4 반 정 반
            elif second[2]!=third[6] and third[2] != fourth[6]:
                second.append(second.popleft())
                third.appendleft(third.pop())
                fourth.append(fourth.popleft())
            # 123 정반정
            elif first[2] != second[6] and second[2]!=third[6] :
                first.appendleft(first.pop())
                second.append(second.popleft())
                third.appendleft(third.pop())
            # 23
            elif second[2]!=third[6] :
                second.append(second.popleft())
                third.appendleft(third.pop())
            # 3 4
            elif third[2] != fourth[6]:
                third.appendleft(third.pop())
                fourth.append(fourth.popleft())
            # 3
            else:
                third.appendleft(third.pop())
        else :
            # 1 2 3 4 반 정 반 정
            if first[2] != second[6] and second[2]!=third[6] and third[2] != fourth[6]:
                first.append(first.popleft())
                second.appendleft(second.pop())
                third.append(third.popleft())
                fourth.appendleft(fourth.pop())
            # 234
            elif second[2]!=third[6] and third[2] != fourth[6]:
                second.appendleft(second.pop())
                third.append(third.popleft())
                fourth.appendleft(fourth.pop())
            # 123
            elif first[2] != second[6] and second[2]!=third[6] :
                first.append(first.popleft())
                second.appendleft(second.pop())
                third.append(third.popleft())
            # 23
            elif second[2]!=third[6] :
                second.appendleft(second.pop())
                third.append(third.popleft())
            #34
            elif third[2] != fourth[6]:
                third.append(third.popleft())
                fourth.appendleft(fourth.pop())
            # 3
            else:
                third.append(third.popleft())

            
    #네번째
    else:
        # 두번째 정방향
        if direction==1:
            # 1 2 3 4 반정반정
            if first[2] != second[6] and second[2]!=third[6] and third[2] != fourth[6]:
                first.append(first.popleft())
                second.appendleft(second.pop())
                third.append(third.popleft())
                fourth.appendleft(fourth.pop())
            #  2 3 4 정반 정
            elif second[2]!=third[6] and third[2] != fourth[6]:
                second.appendleft(second.pop())
                third.append(third.popleft())
                fourth.appendleft(fourth.pop())
            # 34 반 정
            elif third[2] != fourth[6]:
                third.append(third.popleft())
                fourth.appendleft(fourth.pop())
            # 4 정
            else:
                fourth.appendleft(fourth.pop())
        #역방향
        else:
            #1,2,3,4 정 반 정 반
            if first[2] != second[6] and second[2]!=third[6] and third[2] != fourth[6]:
                first.appendleft(first.pop())
                second.append(second.popleft())
                third.appendleft(third.pop())
                fourth.append(fourth.popleft())
            #234
            elif second[2]!=third[6] and third[2] != fourth[6]:
                second.append(second.popleft())
                third.appendleft(third.pop())
                fourth.append(fourth.popleft())
            # 34
            elif third[2] != fourth[6]:
                third.appendleft(third.pop())
                fourth.append(fourth.popleft())
            # 4
            else:
                fourth.append(fourth.popleft())
if first[0]==1:
    cnt+=1
if second[0]==1:
    cnt+=2
if third[0]==1:
    cnt+=4
if fourth[0]==1:
    cnt+=8
print(cnt)
반응형
저작자표시 (새창열림)

'알고리즘 > 문제 풀이' 카테고리의 다른 글

[Python/파이썬 10819 백준] 차이를 최대로  (0) 2021.04.03
[Python/파이썬 15684 백준] 사다리 조작  (0) 2021.03.28
[Python/파이썬 14890 백준] 경사로  (0) 2021.03.24
[Python/파이썬 14888 백준] 연산자 끼워넣기  (0) 2021.03.23
[Python/파이썬 9465 백준] 스티커 (다이나믹 프로그래밍)  (0) 2021.03.15
  • 풀이
'알고리즘/문제 풀이' 카테고리의 다른 글
  • [Python/파이썬 10819 백준] 차이를 최대로
  • [Python/파이썬 15684 백준] 사다리 조작
  • [Python/파이썬 14890 백준] 경사로
  • [Python/파이썬 14888 백준] 연산자 끼워넣기
광규니
광규니
공부 및 일상 올리기~

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.