일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 1789번
- 경주로 건설
- SW Expert Academy
- 보석 쇼핑
- 파이썬
- 거울 설치
- python
- 감소하는 수
- SW ExpertAcademy
- 12865번
- 키패드 누르기
- 15686번
- 프로그래머스
- 베스트엘범
- 빛의 경로 사이클
- 14499번
- 스타트 택시
- 19238번
- 2020 카카오 인턴십
- 미세먼지 안녕!
- 9095번
- QueryDSL 기초
- 수식 최대화
- 1038번
- 17144번
- 백준 알고리즘
- 어른 상어
- 16234번
- HTML 기초
- 12869번
Archives
- Today
- Total
보물창고 블로그
백준 알고리즘 14499번 주사위 굴리기 풀이 With Python 본문
728x90
문제 링크: https://www.acmicpc.net/problem/14499
문제의 해결은 각 주사위가 동 서 남 북 방향으로 움직였을 때, 주사위에 적혀있는 숫자들을 바 저는 뀌면 된다.
저는 주사위를 담은 배열을 선언한 뒤에 주사위를 동 서 남 북으로 움직였을 때 숫자를 바꾸는 방식으로 문제를 해결하였습니다. 소스코드는 아래와 같습니다.
from collections import deque
def solution(map1, order, n, m, x, y, k):
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
stack = []
dice = [0 for _ in range(6)]
stack.append([x, y])
if map1[x][y] != 0:
dice[5] = map1[x][y]
for _ in range(k):
direction = order.popleft()
nx1, ny1 = stack.pop()
nx = nx1 + dx[direction - 1]
ny = ny1 + dy[direction - 1]
if -1 < nx < n and -1 < ny < m:
pass
else:
stack.append([nx1, ny1])
continue
if direction == 2:
temp = dice[0]
dice[0] = dice[4]
temp2 = dice[2]
dice[2] = temp
temp = dice[5]
dice[5] = temp2
dice[4] = temp
elif direction == 1:
temp = dice[0]
dice[0] = dice[2]
temp2 = dice[4]
dice[4] = temp
temp = dice[5]
dice[5] = temp2
dice[2] = temp
elif direction == 3:
temp = dice[0]
dice[0] = dice[3]
temp2 = dice[1]
dice[1] = temp
temp = dice[5]
dice[5] = temp2
dice[3] = temp
elif direction == 4:
temp = dice[0]
dice[0] = dice[1]
temp2 = dice[3]
dice[3] = temp
temp = dice[5]
dice[5] = temp2
dice[1] = temp
if map1[nx][ny] == 0:
map1[nx][ny] = dice[5]
else:
dice[5] = map1[nx][ny]
map1[nx][ny] = 0
print(dice[0])
stack.append([nx,ny])
n, m, x, y, k = map(int, input().split())
map1 = []
for _ in range(n):
map1.append(list(map(int, input().split())))
order = deque(map(int, input().split()))
solution(map1, order, n, m, x, y, k)
소스코드에 대한 질문이 있으시면 댓글남겨주시면 답변드리겠습니다.
'알고리즘 풀이 > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 16234번 인구 이동 풀이 With Python (0) | 2020.06.30 |
---|---|
백준 알고리즘 15686번 치킨 배달 풀이 With Python (0) | 2020.06.30 |
백준 알고리즘 19238번 스타트 택시 풀이 With Python (0) | 2020.06.30 |
백준 알고리즘 19237번 어른 상어 풀이 With Python (0) | 2020.06.30 |
백준 알고리즘 19236번 청소년 상어 풀이 With Python (1) | 2020.06.30 |
Comments