본문 바로가기

Python(알고리즘,문제풀이)/BOJ (Bronze IV)

백준 28295번 / 체육은 코딩과목 입니다 - 파이썬

728x90

📚문제

출처 : 백준 28295번(https://www.acmicpc.net/problem/28295)

 

📝풀이

#28295 체육은 코딩과목 입니다
di = 'N'
for _ in range(10):
    order = int(input())
    if di == 'N' and order ==1:
        di = 'E'
    elif di =='N' and order ==2:
        di = 'S'
    elif di =='N' and order ==3:
        di = 'W'
        
    elif di == 'E' and order ==1:
        di = 'S'
    elif di =='E' and order ==2:
        di = 'W'
    elif di =='E' and order ==3:
        di = 'N'
        
    elif di == 'W' and order ==1:
        di = 'N'
    elif di =='W' and order ==2:
        di = 'E'
    elif di =='W' and order ==3:
        di = 'S'
        
    elif di == 'S' and order ==1:
        di = 'W'
    elif di =='S' and order ==2:
        di = 'N'
    elif di =='S' and order ==3:
        di = 'E'
print(di)

방법이 너무 생각 안나서 일단 정답은 맞춰보자는 심정으로 노가다...

풀고 구글링 해보니 파이썬으로 푼 방법은 2가지가 있는데 

1가지 방법은 좌표평면 ? 을 이용한것 같은데 신박하긴하나

초기 설정값 등이 이해가 잘 되지 않아서 

나머지 풀이 참고

▼참고한 링크 출처

direction = ["N", "E", "S", "W"]
result = "N"

for i in range(10):
    command = int(input())
    if command == 1:
        result = direction[(direction.index(result) + 1) % 4]
    elif command == 2:
        result = direction[(direction.index(result) + 2) % 4]
    elif command == 3:
        result = direction[(direction.index(result) + 3) % 4]

print(result)

찬찬히 뜯어보면 이해가 되는데 어떻게 이런 코드를 구현해낼지 잘 모르겠다

우선 초기값이 N이기 때문에

command(우향우,뒤로돌아,좌향좌)에 따라서 리스트 순서를 배치한 것 같다

(%4는 direction내 index에 +2 나 +3등을 하게되면 index_range를 벗어나기 때문인 것 같다)

 

나중에 다시 풀어보거나 

새로운 알고리즘이나 자료구조에 대해 알게되서 

다른 풀이법이 생각난다면

다시 풀어봐야겠다

728x90