[白俊#14499]サイコロを投げる


1.問題の説明



2.解説


サイコロを投げるのをシミュレートして、濃いサムスンの味を放つ問題.サイコロを投げるときは、各面がどこに移動するかを知ってからコードに変換すればいいです.
  • サイコロをスクロールすると、地図上の数字が0でない場合は、サイコロの底面に数字をコピーし、地図上の値が0になります.
  • サイコロをスクロールすると、地図上の数字が0の場合、サイコロの底面の数字を地図にコピーします.
  • 地図範囲を超えようとする命令を無視しても出力しない.つまり、サイコロは投げません.
  • この条件に注意して、特に太く書いた条件で、サイコロを投げてください.ではサイコロはどうやって転がりますか?
  • 東(右):左->上、右->下、底->左
  • 西(左):上->左->下、下->右->上
  • 北:上->背面->底->正面->正面->正面
  • 南側:正面->正面->底->背面->正面
  • 頭の中で想像すれば理解できるここで東西に転がるときは前後が変わらず、南北に転がるときは左右が変わらない.それでは今から和音を編みましょう

    3.コード

    from sys import stdin
    input = stdin.readline
    
    N, M, x, y, K = map(int, input().split())
    graph = [list(map(int, input().split())) for _ in range(N)]
    
    # 0: 위, 1: 아래, 2: 앞, 3: 뒤, 4: 왼, 5: 오
    dice = [0, 0, 0, 0, 0, 0]
    
    order_list = list(map(int, input().split()))
    
    for order in order_list:
        if order == 1:  # 동: 위->오, 오->아래, 아래->왼, 왼->위
            if y+1 >= M:
                continue
            else:
                up = dice[0]
                down = dice[1]
                left = dice[4]
                right = dice[5]
    
                dice[0] = left
                dice[1] = right
                dice[4] = down
                dice[5] = up
    
                y += 1
    
        elif order == 2:  # 서: 오른쪽->위, 위->왼쪽, 왼쪽->아래, 아래->오른쪽
            if y-1 < 0:
                continue
            else:
                up = dice[0]
                down = dice[1]
                left = dice[4]
                right = dice[5]
    
                dice[0] = right
                dice[1] = left
                dice[4] = up
                dice[5] = down
    
                y -= 1
    
        elif order == 3:  # 북 - 앞->위, 위->뒤, 뒤->아래, 아래->앞
            if x-1 < 0:
                continue
            else:
                up = dice[0]
                down = dice[1]
                front = dice[2]
                rear = dice[3]
    
                dice[0] = front
                dice[1] = rear
                dice[2] = down
                dice[3] = up
    
                x -= 1
    
        elif order == 4:  # 남 - 위->앞, 앞->아래, 아래->뒤, 뒤->위
            if x+1 >= N:
                continue
            else:
                up = dice[0]
                down = dice[1]
                front = dice[2]
                rear = dice[3]
    
                dice[0] = rear
                dice[1] = front
                dice[2] = up
                dice[3] = down
    
                x += 1
    
        if graph[x][y] != 0:
            dice[1] = graph[x][y]
            graph[x][y] = 0
        else:
            graph[x][y] = dice[1]
    
        print(dice[0])