구현 언어 : python3
"굴러다니는 주사위를 어떻게 배열로 표현할 것인가" 가 핵심인 문제입니다.
배열로 표현하기 때문에 사실상 전개도를 최신화해서 가져가는 것이고, 저는 인덱스를 옮기는 방식으로 표현했습니다.
(동) 방향으로 굴릴땐 인덱스 번호가 항상 아래와 같이 섞입니다. 나머지 3방향에 대해서도 마찬가지 방식을 적용할 수 있습니다.
이걸 코드로 표현하면 다음과 같습니다.
def move_net(i, net) : # 코드에서는 윗면을 생략하여 5면만 표현했습니다. (윗면=7-아랫면)
if i==1: # 동
nnet = [net[1],7-net[0],net[0],net[3],net[4]]
elif i==2: # 서
nnet = [net[2],net[0],7-net[0],net[3],net[4]]
elif i==3: # 북
nnet = [net[3],net[1],net[2],7-net[0],net[0]]
elif i==4: # 남
nnet = [net[4],net[1],net[2],net[0],7-net[0]]
return nnet
전체적인 구현 코드
def solution(input) :
# 지도 크기 N x M, (y,x) , 0부터 시작
# input read
N, M, y, x, K = list(map(int,input().split()))
mmap = []
for _ in range(N) :
mmap.append(list(map(int,input().split())))
dice_move = []
dice_move = list(map(int,input().split()))
# 반복 사용 구현
def move_net(i, net) :
if i==1:
nnet = [net[1],7-net[0],net[0],net[3],net[4]]
elif i==2:
nnet = [net[2],net[0],7-net[0],net[3],net[4]]
elif i==3:
nnet = [net[3],net[1],net[2],7-net[0],net[0]]
elif i==4:
nnet = [net[4],net[1],net[2],net[0],7-net[0]]
return nnet
roll = { # 동서북남
1:(0,1),
2:(0,-1),
3:(-1,0),
4:(1,0)
}
c_net = [6,3,4,2,5] # 현재 전개도
dice = [0]*7 # 주사위면 숫자 (인덱스 0은 사용하지 않음)
c_loc = (y,x) # 현위치
for i in dice_move :
# 지도 밖이 아니라면 이동
nc_loc = (c_loc[0]+roll[i][0],c_loc[1]+roll[i][1])
if nc_loc[0] >= 0 and nc_loc[0] < N and nc_loc[1] >= 0 and nc_loc[1] < M :
c_loc = nc_loc # 주사위 현재 위치 이동
c_net = move_net(i, c_net) # 전개도 최신화
bottom = c_net[0]
top = 7-bottom
if mmap[c_loc[0]][c_loc[1]] == 0 : # 바닥이 0일 때
mmap[c_loc[0]][c_loc[1]] = dice[bottom]
else :
dice[bottom] = mmap[c_loc[0]][c_loc[1]]
mmap[c_loc[0]][c_loc[1]] = 0
print(dice[top])
# solution(input)
'Algorithem > 백준 PS with code' 카테고리의 다른 글
백준 #14501 - 퇴사 : DP (0) | 2023.05.15 |
---|---|
백준 #16235 - 나무 재테크 : 구현, 시간 (0) | 2023.05.11 |
백준 #10844 - 쉬운 계단 수 : 점화식 (0) | 2023.01.09 |
백준 #1208 부분수열의 합(2) (0) | 2023.01.09 |
백준 #1647 도시분할계획 : 크루스칼 알고리즘과 유니온파인드 (0) | 2023.01.09 |