[백준] 14499 : 주사위 굴리기 - Python

Chooooo·2024년 3월 25일
0

알고리즘/백준

목록 보기
144/182

코드

import sys

sys.stdin = open("input.txt", "rt")

n, m, x, y, k = map(int, input().split())
# ! 보드 크기 n,m 좌표 x,y 명령의 개수 k
g = [list(map(int, input().split())) for _ in range(n)]
move = list(map(int, input().split()))  # ! 이동 방향

right, left, up, down = 1,2,3,4
dx = [0, 0, 0, -1, 1]
dy = [0, 1, -1, 0, 0]
# ! 동 서 북 남 : 1 2 3 4

dice = [0] * 7  # ! 1번 인덱스부터 사용
def move_left():
    global dice
    new_dice = [0] * 7
    new_dice[4] = dice[1]
    new_dice[2] = dice[2]
    new_dice[1] = dice[3]
    new_dice[6] = dice[4]
    new_dice[5] = dice[5]
    new_dice[3] = dice[6]
    return new_dice
def move_right():
    global dice
    new_dice = [0] * 7
    new_dice[3] = dice[1]
    new_dice[2] = dice[2]
    new_dice[6] = dice[3]
    new_dice[1] = dice[4]
    new_dice[5] = dice[5]
    new_dice[4] = dice[6]
    return new_dice
def move_up():
    global dice
    new_dice = [0] * 7
    new_dice[2] = dice[1]
    new_dice[6] = dice[2]
    new_dice[3] = dice[3]
    new_dice[4] = dice[4]
    new_dice[1] = dice[5]
    new_dice[5] = dice[6]
    return new_dice
def move_down():
    global dice
    new_dice = [0] * 7
    new_dice[5] = dice[1]
    new_dice[1] = dice[2]
    new_dice[3] = dice[3]
    new_dice[4] = dice[4]
    new_dice[6] = dice[5]
    new_dice[2] = dice[6]
    return new_dice

for dir in move: # ! 움직임
    if not (0 <= x + dx[dir] < n and 0<= y + dy[dir] < m):
        continue
    # ! step1 : 해당 좌표로 이동
    x += dx[dir]
    y += dy[dir]
    # ! step2 :  주사위 좌표 변화
    if dir == up:
        dice = move_up()
    elif dir == down:
        dice = move_down()
    elif dir == left:
        dice = move_left()
    elif dir == right:
        dice = move_right()
    # ! step3 : 보드판 좌표 확인
    if g[x][y] == 0: # ! 보드가 0이면 -> 주사위 바닥면에 쓰인 수 -> 칸으로 복사
    # ! 주사위 바닥면은 dice[6] 좌표
        g[x][y] = dice[6]
    else: # ! 주사위 값이 0이 아니면 쓰여 있는 수가 주사위 바닥면으로 복사, 보드 0
        dice[6] = g[x][y]
        g[x][y] = 0

    print(dice[1])

코멘트

어떻게 풀 수 있을까 ?
주사위가 동서북남으로 움직일 때 어떻게 변하는지 코드로 구현해야 한다.
문제의 조건에 따라 주사위의 위치에 보드의 값이 0이면 주사위의 하단 값을 보드로 복사한다.
주사위의 위치에 보드의 값이 0이 아니라면 주사위의 하단 값을 보드의 값으로 복사한다.

주사위가 움직일 때 위치가 어떻게 변하는지 체크했어야 했다.

삼성 코테, 즉 빡구현 문제를 풀 때는 직접 문제를 돌려가면서 어떻게 될지를 직접 (코드로) 있는 그대로 작성하려고 노력하자.

profile
back-end, 지속 성장 가능한 개발자를 향하여

0개의 댓글