728x90
반응형
문제 출처 :
https://www.acmicpc.net/problem/14499
문제 풀이 :
- 이런 문제의 경우 문제의 상황을 3D 공간상으로 시뮬레이션 돌려놓고(머리로) 풀면 더욱 좋은 문제이다.
- 신경 쓴 부분은 각각의 회전에 따라 밑면과 윗면을 어떻게 구할 것인가 이다. 본인의 경우 도면에서 항상 위와 아래면을 맡는 위치를 찾았고 그렇게 되면 회전한 후에 주사위의 도면만 알면 문제를 해결할 수 있다.
즉, 회전시에 도면이 어떻게 변하는지를 알면 해결 할 수 있다. - 밑의 그림을 살펴보자
- 나는 아예 주사위의 옆으로 훑는 면과 앞에서 뒤로 훑는면을 따로 만들어서 관리하였다.오른쪽으로 회전하는 경우만 보자면
[4,2,3,5] -> [5,4,2,3] 오른쪽으로 한칸씩 밀렸다.
[1,2,6,5] -> [1,4,6,3] 위의 list에서 2번째 4번째는 같고 1번째 3번째는 기존 list의 값과 같다.
이런식으로 모든 회전에 따라서 변하는 부분을 체크하고 코딩하여서 해결 했다.
from collections import deque
n,m,x,y,k = map(int,input().split())
maps = [list(map(int,input().split())) for _ in range(n)]
commands = list(map(int,input().split()))
dx = [0,0,-1,1]
dy = [1,-1,0,0]
#init_dice
dice = {1:0,2:0,3:0,4:0,5:0,6:0}
dice_row =deque([4,1,3,6])
dice_col =deque([2,1,5,6])
#1 : right 2: left 3:up 4:down
#algorithm
'''
1. 우선 Dice를 이동시킨다.
1-1) 이동시킬 위치가 범위를 벗어날 경우 실행하지않는다.
2. 이동시킨 dice의 회전규칙을 작동시킨다.
3. 바닥면을 확인한 후 작동
4. 윗면 출력
'''
while(commands):
cur_d = commands.pop(0) -1
nx = x+dx[cur_d]
ny = y+dy[cur_d]
#print(x,y,nx,ny)
if(nx>=n or nx<0 or ny>=m or ny<0):
continue
if(cur_d==0):
#right
dice_row.rotate(1)
dice_col[1]=dice_row[1]
dice_col[3]=dice_row[3]
elif (cur_d == 1):
#left
dice_row.rotate(-1)
dice_col[1] = dice_row[1]
dice_col[3] = dice_row[3]
elif (cur_d == 2):
# up
dice_col.rotate(-1)
dice_row[1] = dice_col[1]
dice_row[3] = dice_col[3]
elif (cur_d == 3):
# down
dice_col.rotate(1)
dice_row[1] = dice_col[1]
dice_row[3] = dice_col[3]
if(maps[nx][ny]==0):
maps[nx][ny]=dice[dice_row[3]]
else:
dice[dice_row[3]]=maps[nx][ny]
maps[nx][ny]=0
#print(dice_row)
#print(dice_col)
#print(dice)
print(dice[dice_row[1]])
x=nx
y=ny
반응형
'Computer Science > Algorithm' 카테고리의 다른 글
[프로그래머스] 메뉴 리뉴얼 (Level2) (Python) (1) | 2021.07.03 |
---|---|
[프로그래머스] 신규 아이디 추천 (Level 1) (Python) (0) | 2021.07.03 |
[백준] 13458 - 시험 감독 (Python) (0) | 2021.07.03 |
[백준] 3190 - 뱀 (Python) (0) | 2021.07.03 |
[백준] 21609 - 상어 중학교 (Python) (0) | 2021.05.10 |