Algorithm Problem/Python

[python] 백준 - 14499. 주사위 굴리기(삼성 SW 역량 테스트 기출 문제)

deo2kim 2020. 5. 19. 23:53
반응형

문제 해결

1. 회전방향에 따라 주사위의 숫자위치 변화를 잘 이해해야 한다.

 (1) 먼저 지도 위에서 주사위를 방향에 맞게 움직여주고

 (2) 움직임에 맞게 주사위의 면에 써진 숫자들의 위치를 변경해준다.

 (3) 바뀐 숫자들과 지도에 써진 숫자를 비교하여 바꿔준다.

 

- > 회전시킨 주사위 면의 변화를 잘 몰라서 그림판에 그려가며 하드코딩 했다.........(어쩔 수 없었다)

소스코드

def move(i, j, direc):
    global x, y
    if direc == 1:  # 동
        if j + 1 < m:
            y = j + 1
    elif direc == 2:  # 서
        if j - 1 >= 0:
            y = j - 1
    elif direc == 3:  # 북
        if i - 1 >= 0:
            x = i - 1
    elif direc == 4:  # 남
        if i + 1 < n:
            x = i + 1
    if i != x or j != y:
        return True
    return False


def rotation(direc):
    if direc == 1:  # 동
        dice[1][0], dice[1][1], dice[1][2], dice[3][1] = dice[3][1], dice[1][0], dice[1][1], dice[1][2]

    elif direc == 2:  # 서
        dice[1][0], dice[1][1], dice[1][2], dice[3][1] = dice[1][1], dice[1][2], dice[3][1], dice[1][0]

    elif direc == 3:  # 북
        dice[0][1], dice[1][1], dice[2][1], dice[3][1] = dice[1][1], dice[2][1], dice[3][1], dice[0][1]

    elif direc == 4:  # 남
        dice[0][1], dice[1][1], dice[2][1], dice[3][1] = dice[3][1], dice[0][1], dice[1][1], dice[2][1]

def change_number():
    # print('xy', x, y)
    if field[x][y] == 0:
        field[x][y] = dice[3][1]
    else:
        dice[3][1] = field[x][y]
        field[x][y] = 0

    print(dice[1][1])


n, m, x, y, k = map(int, input().split())  # 세로 가로 엑스 와이 명령갯수
field = [list(map(int, input().split())) for _ in range(n)]

orders = list(map(int, input().split()))

# 1, 2, 3, 4 : 동 서 북 남
dice = [[0]*3 for _ in range(4)]

for order in range(len(orders)):
    if move(x, y, orders[order]):
        rotation(orders[order])
        change_number()

 

 

출처 : BACKJOON ONLINE JUDGE

문제 : 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

 

반응형