여름의 서재
[백준] 14499_주사위 굴리기 (Python) 본문
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
💡 풀이법
1. dice의 각 6개 방향의 번호를 길이가 6인 리스트로 표현한다. (나는 0번 인덱스의 번호가 지도와 닿아있는 방향)
2. 동, 서, 남, 북으로 돌릴 때, 각 방향의 번호들을 바꿔준다.
3. 주사위를 놓는 곳이 0이라면 0번 인덱스의 숫자를 지도의 닿아있는 부분에 넣고, 반대로 0번 인덱스가 0이라면 지도의 숫자를 주사위에 넣는다.
N, M, x, y, K = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(N)]
move = list(map(int, input().split()))
dxy = [(0, 1), (0, -1), (-1, 0), (1, 0)]
dice = [0, 0, 0, 0, 0, 0]
for m in move:
dx, dy = dxy[m-1]
x += dx
y += dy
if not 0 <= x < N or not 0 <= y < M:
x -= dx
y -= dy
continue
if m == 1:
temp, dice[0] = dice[0], dice[2]
temp, dice[3] = dice[3], temp
temp, dice[5] = dice[5], temp
dice[2] = temp
elif m == 2:
temp, dice[2] = dice[2], dice[0]
temp, dice[5] = dice[5], temp
temp, dice[3] = dice[3], temp
dice[0] = temp
elif m == 3:
temp, dice[4] = dice[4], dice[0]
temp, dice[5] = dice[5], temp
temp, dice[1] = dice[1], temp
dice[0] = temp
else:
temp, dice[0] = dice[0], dice[4]
temp, dice[1] = dice[1], temp
temp, dice[5] = dice[5], temp
dice[4] = temp
if not board[x][y]:
board[x][y] = dice[0]
else:
dice[0] = board[x][y]
board[x][y] = 0
print(dice[5])
'알고리즘 > BOJ' 카테고리의 다른 글
[백준] 21610_마법사 상어와 비바라기 (Python) (0) | 2022.03.06 |
---|---|
[백준] 13460_구슬 탈출 2 (Python) (0) | 2022.03.01 |
[백준] 21608_상어 초등학교 (Python) (0) | 2022.02.28 |
[백준] 20055_컨베이어 벨트 위의 로봇 (Python) (0) | 2022.02.28 |
[백준] 2096_내려가기 (dp 이용) (0) | 2021.12.07 |
Comments