Programming/CodingTest

[코드트리] 삼성 SW 역량테스트 | 2016년 하반기 1번 | 정육면체 굴리기

콩순이컴퓨터 2025. 6. 1. 23:55

https://www.codetree.ai/ko/frequent-problems/samsung-sw/problems/cube-rounding/description

 

코딩테스트 기출 문제 설명: 정육면체 굴리기 | 코드트리

코딩테스트 기출 문제 정육면체 굴리기의 상세 설명입니다. 문제 요구사항을 정확히 파악하고 효율적인 알고리즘을 설계해보세요.

www.codetree.ai

 

  • 문제유형 : 시뮬레이션
  • 난이도 : L12
  • 정답률 : 63%

 

풀이 방법

 

 

코드

n, m, x, y, k = map(int, input().split())
board = []
for _ in range(n):
    board.append(list(map(int, input().split())))
direction = list(map(int, input().split())) # 1:동, 2:서, 3:북, 4:남 

dice = [0, 0, 0, 0, 0, 0] #동, 서, 남, 북, 위, 아래

def dice_change(dice, d):
    if d == 1: #동
        dice[0], dice[1], dice[4], dice[5] = dice[4], dice[5], dice[1], dice[0]
    elif d==2: #서
        dice[0], dice[1], dice[4], dice[5] = dice[5], dice[4], dice[0], dice[1]
    elif d==3: #북
        dice[2], dice[3], dice[4], dice[5] = dice[5], dice[4], dice[2], dice[3]
    elif d==4: #남
        dice[2], dice[3], dice[4], dice[5] = dice[4], dice[5], dice[3], dice[2]
    return dice 

def board_move(r, c, d):
    dr = [0, 0, -1, 1] #동서북남
    dc = [1, -1, 0, 0]
    next_r = r + dr[d-1]
    next_c = c + dc[d-1]
    if 0 <= next_r < row_len and 0 <= next_c < col_len:
        return next_r, next_c, True 
    else:
        return next_r, next_c, False 

row_len = n 
col_len = m 
cur_r, cur_c = x, y
for d in direction:
    next_r, next_c, move = board_move(cur_r, cur_c, d) #이동된 방향
    if move == True :
        dice = dice_change(dice, d)
        print(dice[4])
        if board[next_r][next_c] == 0:
            board[next_r][next_c] = dice[5]
        else:
            dice[5] = board[next_r][next_c]
            board[next_r][next_c] = 0 
        cur_r, cur_c = next_r, next_c