728x90
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
n,m,x,y,k = map(int,input().split())
ground =[]
dx=[0,0,-1,1]
dy=[1,-1,0,0]
dice=[0,0,0,0,0,0]
def turn(dir):
a,b,c,d,e,f=dice[0],dice[1],dice[2],dice[3],dice[4],dice[5]
if dir ==1:
dice[0], dice[1], dice[2], dice[3], dice[4], dice[5]= d,b,a,f,e,c
elif dir==2:
dice[0], dice[1], dice[2], dice[3], dice[4], dice[5]= c,b,f,a,e,d
elif dir==3:
dice[0], dice[1], dice[2], dice[3], dice[4], dice[5]= e,a,c,d,f,b
elif dir==4:
dice[0], dice[1], dice[2], dice[3], dice[4], dice[5]= b,f,c,d,a,e
for i in range(n):
ground.append(list(map(int,input().split())))
command = list(map(int,input().split()))
nx,ny=x,y
for i in command:
nx+=dx[i-1]
ny+=dy[i-1]
if nx<0 or nx>=n or ny<0 or ny>=m:
nx-=dx[i-1]
ny-=dy[i-1]
continue
turn(i)
if ground[nx][ny]==0:
ground[nx][ny]=dice[-1]
else:
dice[-1]=ground[nx][ny]
ground[nx][ny]=0
print(dice[0])
이 문제의 핵심은 동, 서, 북, 남으로 이동한 경우 주사위의 각 면이 어떻게 변화하는 지를 파악하는 것입니다.
turn 함수를 정의해 명령별 주사위 면의 변화를 나타냈습니다.
문제에서 동쪽은 1, 서쪽은 2, 북쪽은 3, 남쪽은 4로 주어지기 때문에 움직임을 나타내는 dx와 dy 설정을 동,서,북,남의 이동에 맞게 설정했습니다.
명령별 주사위 면의 변화를 파악했다면 단순 구현만으로 코드를 완성할 수 있습니다.
728x90
반응형
'알고리즘' 카테고리의 다른 글
[프로그래머스/파이썬(Python3)]_소수 만들기 (0) | 2022.05.14 |
---|---|
[BOJ/Python3(파이썬)] 백준 5598번 : 카이사르 암호 (0) | 2022.05.14 |
[BOJ/Python3(파이썬)] 백준 16935번: 배열 돌리기 3 (0) | 2022.05.12 |
[BOJ/Python3(파이썬)] 백준 2745번: 진법 변환 (0) | 2022.01.17 |
[정렬] 백준 10825번 : 국영수(BOJ, Python, 파이썬) (0) | 2022.01.12 |