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
import sys
n, m, x, y, k= map(int, sys.stdin.readline().split())
graph = []
dice = [ [0],
[0,0,0],
[0],
[0]]
def rol_dice(a):
if a == 1:
tmp = dice[3][0]
dice[3][0] = dice[1][2]
dice[1][2] = dice[1][1]
dice[1][1] = dice[1][0]
dice[1][0] = tmp
elif a== 2:
tmp = dice[3][0]
dice[3][0] = dice[1][0]
dice[1][0] = dice[1][1]
dice[1][1] = dice[1][2]
dice[1][2] = tmp
elif a == 3:
tmp = dice[3][0]
dice[3][0] = dice[0][0]
dice[0][0] = dice[1][1]
dice[1][1] = dice[2][0]
dice[2][0] = tmp
elif a == 4:
tmp = dice[3][0]
dice[3][0] = dice[2][0]
dice[2][0] = dice[1][1]
dice[1][1] = dice[0][0]
dice[0][0] = tmp
dx = [0, 0, 0, -1, 1]
dy = [0, 1, -1, 0, 0]
for i in range(n):
graph.append(list(map(int,sys.stdin.readline().split())))
ans = []
go_list = list(map(int,sys.stdin.readline().split()))
for i in range(k):
go =go_list[i]
if go == 1:
nx = x + dx[1]
ny = y + dy[1]
if nx < 0 or ny < 0 or n <= nx or m <= ny:
continue
rol_dice(1)
if graph[nx][ny] == 0 :
graph[nx][ny] = dice[3][0]
elif graph[nx][ny] != 0:
dice[3][0] = graph[nx][ny]
graph[nx][ny] = 0
x,y = nx,ny
elif go == 2:
nx = x + dx[2]
ny = y + dy[2]
if nx < 0 or ny < 0 or n <= nx or m <= ny:
continue
rol_dice(2)
if graph[nx][ny] == 0:
graph[nx][ny] = dice[3][0]
elif graph[nx][ny] != 0:
dice[3][0] = graph[nx][ny]
graph[nx][ny] = 0
x, y = nx, ny
elif go == 3:
nx = x + dx[3]
ny = y + dy[3]
if nx < 0 or ny < 0 or n <= nx or m <= ny:
continue
rol_dice(3)
if graph[nx][ny] == 0:
graph[nx][ny] = dice[3][0]
elif graph[nx][ny] != 0:
dice[3][0] = graph[nx][ny]
graph[nx][ny] = 0
x, y = nx, ny
elif go == 4:
nx = x + dx[4]
ny = y + dy[4]
if nx < 0 or ny < 0 or n <= nx or m <= ny:
continue
rol_dice(4)
if graph[nx][ny] == 0:
graph[nx][ny] = dice[3][0]
elif graph[nx][ny] != 0:
dice[3][0] = graph[nx][ny]
graph[nx][ny] = 0
x, y = nx, ny
ans.append(dice[1][1])
for i in range(len(ans)):
print(ans[i])
로직만 생각하고 풀면 되는데 너무 오래 걸리기는 한듯?
함수를 만드려고 했지만 그냥 입력하는게 편할거 같아 만들었다.
반응형
'알고리즘 공부 > 구현' 카테고리의 다른 글
[백준] 17140번 이차원 배열과 연산- 파이썬 (0) | 2022.04.06 |
---|---|
[백준] 14890번 경사로- 파이썬 (0) | 2022.04.03 |
[백준] 17144번 미세먼지 안녕!- 파이썬 (0) | 2022.03.31 |
[백준] 15685번 드래곤 커브- 파이썬 (0) | 2022.03.30 |
[백준] 14891번 톱니바퀴- 파이썬 (0) | 2022.03.27 |