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())
# 위->아래: 북->남 / 왼->오: 서->동
map_data = [list(map(int, input().split())) for _ in range(n)]
orders = list(map(int, input().split()))
dice = [0]*6
top = 0 # 윗면
up = 1 # 북쪽
right = 2 # 동쪽
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
for o in orders: # 명령
nx = x+dx[o-1]
ny = y+dy[o-1]
if nx < 0 or nx >= n or ny < 0 or ny >= m:
continue
x, y = nx, ny
if o == 1: # 동
if map_data[x][y] == 0:
map_data[x][y] = dice[right]
else:
dice[right] = map_data[x][y]
map_data[x][y] = 0
top, right = 5-right, top
elif o == 2: # 서
if map_data[x][y] == 0:
map_data[x][y] = dice[5-right]
else:
dice[5-right] = map_data[x][y]
map_data[x][y] = 0
top, right = right, 5-top
elif o == 3: # 북
if map_data[x][y] == 0:
map_data[x][y] = dice[up]
else:
dice[up] = map_data[x][y]
map_data[x][y] = 0
top, up = 5-up, top
else: # 남
if map_data[x][y] == 0:
map_data[x][y] = dice[5-up]
else:
dice[5-up] = map_data[x][y]
map_data[x][y] = 0
top, up = up, 5-top
print(dice[top])
'문제풀이 > 구현' 카테고리의 다른 글
[Python/파이썬] 백준 18311번 왕복 (0) | 2024.04.15 |
---|---|
[Python/파이썬] 백준 21277번 짠돌이 호석 (0) | 2024.04.06 |
[Python/파이썬] 백준 17281번 ⚾ (0) | 2024.03.30 |
[Python/파이썬] 백준 1652번 누울 자리를 찾아라 (0) | 2024.03.12 |
[Python/파이썬] 백준 6986번 절사평균 (0) | 2024.03.02 |