이 포스팅은 Algorithm Problem Solving 시리즈 113 편 중 43 번째 글 입니다.
목차
골드5 : 구현 문제이다.
풀이
문제 똑바로 읽자. 조건하나 안봐서 틀렸네.
Code
import sys
from pprint import pprint
input = sys.stdin.readline
action_dict = {4: "south", 3: "north", 2: "west", 1: "east"}
n, m, y, x, k = map(int, input().split())
total_map = [list(map(int, input().split())) for _ in range(n)]
actions = [action_dict[num] for num in list(map(int, input().split()))]
dice = {"top": 0, "right": 0, "left": 0, "rear": 0, "front": 0, "bottom": 0}
# dice = {"top": 1, "right": 3, "left": 4, "rear": 2, "front": 5, "bottom": 6}
direction = {"south": [1, 0], "north": [-1, 0], "east": [0, 1], "west": [0, -1]}
dice_move_dict = {
"east": [
["top", "left"],
["right", "top"],
["left", "bottom"],
["bottom", "right"],
["rear", "rear"],
["front", "front"],
],
"west": [
["top", "right"],
["right", "bottom"],
["left", "top"],
["bottom", "left"],
["rear", "rear"],
["front", "front"],
],
"south": [
["top", "rear"],
["right", "right"],
["left", "left"],
["bottom", "front"],
["rear", "bottom"],
["front", "top"],
],
"north": [
["top", "front"],
["right", "right"],
["left", "left"],
["bottom", "rear"],
["rear", "top"],
["front", "bottom"],
],
}
def move_dice(dice, action):
copy_dice = {name: 0 for name in dice}
for a, b in dice_move_dict[action]:
copy_dice[a] = dice[b]
return copy_dice
def copy_bottom(y, x, total_map, dice):
if total_map[y][x] == 0:
total_map[y][x] = dice["bottom"]
else:
dice["bottom"] = total_map[y][x]
total_map[y][x] = 0
return dice
def isPossible(y, x, action):
dy, dx = direction[action]
ny, nx = y + dy, x + dx
if 0 <= ny < n and 0 <= nx < m:
return True
return False
cy, cx = y, x
for action in actions:
if isPossible(cy, cx, action):
dy, dx = direction[action]
cy, cx = cy + dy, cx + dx
dice = move_dice(dice, action)
dice = copy_bottom(cy, cx, total_map, dice)
# print(action)
# print(dice)
# pprint(total_map)
print(dice["top"])
# 3 3 1 1 4
# 0 0 0
# 0 0 0
# 0 0 0
# 1 2 3 4