본문 바로가기
백준 삼성

[python] 14499 - 주사위 굴리기

by 다데기 2022. 8. 26.

https://www.acmicpc.net/problem/14499

 

14499번: 주사위 굴리기

첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x, y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지

www.acmicpc.net

 

 

정답률 43.425%

난이도 골4

주사위 굴리는 걸 어떻게 구현할지 고민

 


코드 설명

1. 입력 받기

2. 좌표 변경과 주사위 회전

3. 숫자 복사 및 결과 출력

 

 

 

※ 주의할 점

  • 방향을 1, 2, 3, 4로 주기 때문에 dx, dy 쓸거면 주의
  • 밖으로 나가면 주사위 안굴리게 해야 시간 절약 가능
  • 주사위 전개도 그려놓고 상하좌우 굴렸을 때 이동 확인
  • 좌표 업데이트 까먹지 않기 (x, y = nx, ny)
def move(op):
    # 2. 좌표 변경과 주사위 회전
    global x, y
    if op == 1:
        nx, ny = x, y + 1
        if nx == -1 or nx == N or ny == -1 or ny == M:
            return
        dice[1], dice[5], dice[3], dice[4] = dice[4], dice[1], dice[5], dice[3]

    elif op == 2:
        nx, ny = x, y - 1
        if nx == -1 or nx == N or ny == -1 or ny == M:
            return
        dice[4], dice[1], dice[5], dice[3] = dice[1], dice[5], dice[3], dice[4]

    elif op == 3:
        nx, ny = x - 1, y
        if nx == -1 or nx == N or ny == -1 or ny == M:
            return
        dice[0], dice[1], dice[2], dice[3] = dice[1], dice[2], dice[3], dice[0]

    else:
        nx, ny = x + 1, y
        if nx == -1 or nx == N or ny == -1 or ny == M:
            return
        dice[1], dice[2], dice[3], dice[0] = dice[0], dice[1], dice[2], dice[3]

    # 3. 숫자 복사 및 결과 출력
    if board[nx][ny]:
        dice[3] = board[nx][ny]
        board[nx][ny] = 0
    else:
        board[nx][ny] = dice[3]

    print(dice[1])
    x, y = nx, ny


# 1. 입력 받기
N, M, x, y, K = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(N)]
order = list(map(int, input().split()))
dice = [0, 0, 0, 0, 0, 0]

for op in order:
    move(op)

 

 


코드 결과