[백준] 14499번 주사위 굴리기- 파이썬

2022. 4. 1. 11:40·알고리즘 공부/구현

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
'알고리즘 공부/구현' 카테고리의 다른 글
  • [백준] 17140번 이차원 배열과 연산- 파이썬
  • [백준] 14890번 경사로- 파이썬
  • [백준] 17144번 미세먼지 안녕!- 파이썬
  • [백준] 15685번 드래곤 커브- 파이썬
코딩 코딩 코오딩
코딩 코딩 코오딩
  • 코딩 코딩 코오딩
    코딩하는 누누
    코딩 코딩 코오딩
  • 전체
    오늘
    어제
    • 분류 전체보기 (491)
      • 생산성 (2)
        • 인텔리제이 (2)
      • 프로젝트 기록 (14)
        • git (2)
        • spring (3)
        • TestCode (2)
        • spring security (3)
        • 기타 (2)
        • MySQL (0)
        • Cloud (2)
      • 회고 (4)
      • Spring (6)
      • JPA (0)
      • DB (4)
        • MySql (2)
        • Redis (1)
      • Java (7)
        • JSP (1)
      • 잡담 (1)
      • CS (30)
        • 컴퓨팅 사고 (0)
        • 배열 (4)
        • 알고리즘 (8)
        • 메모리 (7)
        • 자료구조 (9)
        • 암호학 (2)
      • opencv (14)
      • AI (56)
        • 머신러닝 (2)
        • 딥러닝 (7)
        • tensorflow (3)
        • 머신러닝(딥러닝) 정리 (21)
        • 강화학습 (7)
        • 논문 읽기 (1)
        • 잡동사니 (1)
        • python AI (13)
        • 선형대수 (1)
        • 확률론 (0)
      • 알고리즘 공부 (177)
        • 그래프 이론 (0)
        • 다익스트라 (4)
        • 위상정렬 (3)
        • 신장트리-크루스칼 알고리즘 (4)
        • 플로이드 워셜 (3)
        • 이진탐색 (9)
        • 백트래킹 (11)
        • 부르드포스 (9)
        • 다이나믹 프로그래밍 (20)
        • BFS & DFS (24)
        • 그리디 (6)
        • 구현 (15)
        • 정렬 (3)
        • 기타 (62)
        • 수학? (1)
      • 코딩 (173)
        • 파이썬(python) (15)
        • c언어 (13)
        • 프로그래머스 lv1 (46)
        • 프로그래머스 lv2 (41)
        • 백준 - c++ (49)
        • Softeer (9)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    n진법 변환
    백준
    삽입 정렬
    코딩문제
    C언어 기초
    캘리브레이션
    다이나믹 프로그래밍
    코딩기초
    자료구조
    BFS
    큐
    에라토슽네스의 체
    코딩기초스킬
    스택
    c언어
    코딩
    선택정렬
    인접행렬
    DFS
    그리디
    순차 탐색
    알고리즘
    if문
    코딩테스트
    소수찾기
    왜곡보정
    프로그래머스
    인접리스트
    정렬
    이미지처리
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
코딩 코딩 코오딩
[백준] 14499번 주사위 굴리기- 파이썬
상단으로

티스토리툴바