주의할 점 : y와 x가 반대로 되어있다. 예제 케이스에서는 x == y 인 경우만 보여주기 때문에 예제케이스에서는 맞더라도 실제는 틀릴 수 있음
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
import sys
from typing import List
from collections import deque
input = sys.stdin.readline
N, M, y, x, K = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(N)]
cmds = list(map(int, input().split()))
dice = [deque([0, 0, 0, 0]), deque([0, 0, 0, 0])] # [0][1], [1][1]은 공유 - 윗면 / [0][3], [1][3] 공유 - 아랫면
bottom = [(1, 3), (0, 3)] # 바닥면에 해당하는 index
top = [(1, 1), (0, 1)] # 윗면에 해당하는 index
DELTAS = {1: (0, 1), 2: (0, -1), 3: (-1, 0), 4: (1, 0)}
def rotate_dice(dice: List[deque], cmd: int):
if cmd == 1:
dice[0].rotate(1)
dice[top[0][0]][top[0][1]] = dice[top[1][0]][top[1][1]]
dice[bottom[0][0]][bottom[0][1]] = dice[bottom[1][0]][bottom[1][1]]
elif cmd == 2:
dice[0].rotate(-1)
dice[top[0][0]][top[0][1]] = dice[top[1][0]][top[1][1]]
dice[bottom[0][0]][bottom[0][1]] = dice[bottom[1][0]][bottom[1][1]]
elif cmd == 3:
dice[1].rotate(-1)
dice[top[1][0]][top[1][1]] = dice[top[0][0]][top[0][1]]
dice[bottom[1][0]][bottom[1][1]] = dice[bottom[0][0]][bottom[0][1]]
elif cmd == 4:
dice[1].rotate(1)
dice[top[1][0]][top[1][1]] = dice[top[0][0]][top[0][1]]
dice[bottom[1][0]][bottom[1][1]] = dice[bottom[0][0]][bottom[0][1]]
def solution(y, x):
for cmd in cmds:
ny = y + DELTAS[cmd][0]
nx = x + DELTAS[cmd][1]
if ny < N and ny >= 0 and nx < M and nx >= 0:
# 이동 & 주사위 굴리기
rotate_dice(dice, cmd)
y, x = ny, nx
# 복사하기
if board[y][x] == 0:
board[y][x] = dice[bottom[0][0]][bottom[0][1]]
else:
dice[bottom[0][0]][bottom[0][1]] = board[y][x]
dice[bottom[1][0]][bottom[1][1]] = board[y][x]
board[y][x] = 0
print(dice[top[0][0]][top[0][1]])
solution(y, x)
|
cs |
반응형
'알고리즘 > 삼성 기출 문제풀이' 카테고리의 다른 글
[시뮬레이션] BOJ 16234 인구이동 (0) | 2023.10.08 |
---|---|
[시뮬레이션] BOJ 15683 감시 (0) | 2023.10.08 |
[시뮬레이션] BOJ 14891 톱니바퀴 (0) | 2023.10.08 |
[시뮬레이션] BOJ 20056 마법사 상어와 파이어볼 (0) | 2023.10.07 |
[시뮬레이션] 20055 컨베이어 벨트 위의 로봇 (0) | 2023.10.07 |