본문 바로가기
python/백준

백준 Python 14499

by 설화님 2024. 8. 10.
import sys
input = sys.stdin.readline.rstrip

# 주사위 각 면의 초기화 (1~6번 면)
dice = [0] * 7

# 지도 크기 및 시작 위치, 명령 수 입력
n, m, x, y, k = map(int, input().split())

# 지도 입력
graph = []
for i in range(n):
    lst = list(map(int, input().split()))
    graph.append(lst)

# 이동 방향 정의 (동서북남 순)
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]

# 현재 주사위의 위치
position = [x, y]

# 명령어 리스트
moves = list(map(int, input().split()))

# 주사위 이동 함수들
def move_east():
    dice[1], dice[3], dice[4], dice[6] = dice[4], dice[1], dice[6], dice[3]
def move_west():
    dice[1], dice[3], dice[4], dice[6] = dice[3], dice[6], dice[1], dice[4]
def move_north():
    dice[1], dice[2], dice[5], dice[6] = dice[2], dice[6], dice[1], dice[5]
def move_south():
    dice[1], dice[2], dice[5], dice[6] = dice[5], dice[1], dice[6], dice[2]

# 이동 처리
def moving(direction):
    # 새로운 위치 계산
    nx = position[0] + dx[direction - 1]
    ny = position[1] + dy[direction - 1]
    
    # 지도 범위를 벗어나지 않을 경우에만 이동
    if 0 <= nx < n and 0 <= ny < m:
        # 방향에 따라 주사위 이동
        if direction == 1:
            move_east()
        elif direction == 2:
            move_west()
        elif direction == 3:
            move_north()
        elif direction == 4:
            move_south()
        
        # 이동 후 위치 업데이트
        position[0], position[1] = nx, ny
        
        # 지도와 주사위의 상호작용
        if graph[nx][ny] == 0:
            graph[nx][ny] = dice[6]
        else:
            dice[6] = graph[nx][ny]
            graph[nx][ny] = 0
        
        # 주사위 윗면 출력
        print(dice[1])

# 명령어 수행
for move in moves:
    moving(move)

 
 





문제 푸는 포인트 요약

  • 주사위의 각 면이 이동할 때 어떻게 변화하는지를 정확히 이해해야 합니다.
  • 이동 명령에 따라 지도를 벗어나지 않도록 체크하고, 벗어날 경우에는 명령을 무시합니다.
  • 주사위의 위치를 업데이트하고, 지도와 상호작용하는 부분을 잘 구현해야 합니다.
  • 주사위 이동에 따라 맵과 주사위의 숫자를 어떻게 변환할지를 코드로 정확히 구현해야 합니다.