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)

문제 푸는 포인트 요약
- 주사위의 각 면이 이동할 때 어떻게 변화하는지를 정확히 이해해야 합니다.
- 이동 명령에 따라 지도를 벗어나지 않도록 체크하고, 벗어날 경우에는 명령을 무시합니다.
- 주사위의 위치를 업데이트하고, 지도와 상호작용하는 부분을 잘 구현해야 합니다.
- 주사위 이동에 따라 맵과 주사위의 숫자를 어떻게 변환할지를 코드로 정확히 구현해야 합니다.
'python > 백준' 카테고리의 다른 글
python 백준 14501 (0) | 2024.08.26 |
---|---|
백준 python 13460 (0) | 2024.07.07 |
백준 14502 Bfs 2차원 (0) | 2024.06.07 |
[python] [백준] [단계별로풀어보기] [문자열] 정답 코드보기 (1) | 2024.01.19 |
[python] [백준] [단계별로 풀어보기] [반복문] 정답. (0) | 2024.01.17 |