카테고리 없음
[백준 14499] 주사위굴리기
백수진
2024. 4. 8. 21:39
import copy
# 1. 초기화
n,m,current_x, current_y, move_count = map(int, input().split())
board = []
for i in range(n):
board.append(list(map(int, input().split())))
move_list = list(map(int, input().split()))
# print(board)
# print(move_list)
# dice_number 딕셔너리
dice_number = {"1":0,"2":0,"3":0,"4":0,"5":0,"6":0}
# dice 면에 dice_number key를 저장 / dice -> [2,1,5,6,4,3]
dice = [2,1,5,6,4,3]
# move_list의 값 d에 따라서 이동할 때 사용할 diff값과 주사위 면의 값과 value를 변경할 배열
direct = [(0,0), (0,1), (0,-1), (-1,0),(1,0)] # 동 서 북 남 -> 맨 처음은 숫자가 1부터 시작하니까 임의값
dice_change = [[0,0,0,0,0,0], [0,4,2,5,3,1],[0,5,2,4,1,3],[1,2,3,0,4,5],[3,0,1,2,4,5]] #-> 맨 처음은 숫자가 1부터 시작하니까 임의값
# dice[index] -> dice2[dice_change[d][index]]로 저장. ex) 맨 처음에 동쪽이동이라면 3은(index 5) -> dice2[1]로 이동
for d in move_list:
# 1. direction으로 이동한 위치
diff_x, diff_y = direct[d]
# 이동가능한지 체크
if not( (0 <= current_x + diff_x <= n - 1) and ( 0 <= current_y + diff_y <= m-1)):
# print("이동불가능")
continue
next_x = current_x + diff_x
next_y = current_y + diff_y
# 2. direction으로 이동했을 때의 주사위 면에 적힌 숫자를 update.(차례대로, 뒤, 밑, 앞, 위, 서, 동)을 기록
dice2 = copy.deepcopy(dice)
dice_change_temp = dice_change[d]
for i in range(6):
dice2[dice_change_temp[i]] = dice[i]
# 밑 dice2[1]
# 위 dice2[3]
print(dice_number[str(dice2[3])])
# 복사하는 과정
if board[next_x][next_y] != 0:
bottom_number = dice2[1]
dice_number[str(bottom_number)] = board[next_x][next_y]
board[next_x][next_y] = 0
else:
bottom_number = dice2[1]
board[next_x][next_y] = dice_number[str(bottom_number)]
dice_number[bottom_number] = 0
current_x, current_y = next_x, next_y
dice = dice2