아이공의 AI 공부 도전기

[Baekjoon] 14499번 : 주사위 굴리기 (Python, 구현, 시뮬레이션)

 

     

 

 

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

 

코드 링크

https://github.com/stellaluminary/Baekjoon

 

GitHub - stellaluminary/Baekjoon

Contribute to stellaluminary/Baekjoon development by creating an account on GitHub.

github.com

 

Python

 

방법 1 - 메모리 30840KB / 시간 76ms / 코드 길이 1553B

 

북쪽, 남쪽으로 움직일 때의 list와 동쪽, 서쪽으로 움직일 때의 list 두 개로 나눠 문제를 풀어냈다.

n과 m 범주를 벗어날 경우 continue하는 방식을 채택했으며 요구하는 구현을 위한 코드를 작성하였다.

다만 본 방식에 있어 맨 위는 horizontal[1]과 vertical[1]과 맨 아래인 horizontal[3]과 vertical[3]은 서로 값을 공유하도록 한다.

 

예제 1에 따른 action 방향에 따른 vertical, horizontal 전개 방식 결과는 다음과 같다.

action / vertical / horizontal

 

4 [0, 0, 0, 3] [0, 0, 0, 3]
4 [3, 0, 0, 5] [0, 0, 0, 5]
4 [5, 3, 0, 7] [0, 3, 0, 7]
1 [5, 0, 0, 8] [3, 0, 7, 8]
3 [0, 0, 8, 6] [3, 0, 7, 6]
3 [0, 8, 6, 4] [3, 8, 7, 4]
3 [8, 6, 4, 2] [3, 6, 7, 2]
2 [8, 3, 4, 7] [2, 3, 6, 7]

 

 

n,m,x,y,k = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(n)]
action = list(map(int, input().split()))

move = {1:(0,1), 2:(0,-1), 3:(-1,0), 4:(1,0)}
horizontal = [0]*4
vertical = [0]*4
for i in range(len(action)):
    if action[i] in [1,2]:
        nx, ny = x + move[action[i]][0], y + move[action[i]][1]
        if nx < 0 or ny < 0 or nx >= n or ny >= m:
            continue
        else:
            x, y = nx, ny

        horizontal[1], horizontal[3] = vertical[1], vertical[3]
        if action[i] == 1:
            horizontal = horizontal[1:] + [horizontal[0]]
        else:
            horizontal = [horizontal[-1]] + horizontal[:-1]

        if graph[x][y]:
            horizontal[-1] = graph[x][y]
            graph[x][y] = 0
        else:
            graph[x][y] = horizontal[-1]
        vertical[1], vertical[3] = horizontal[1], horizontal[3]
        print(horizontal[1])
    else:
        nx, ny = x + move[action[i]][0], y + move[action[i]][1]
        if nx < 0 or ny < 0 or nx >= n or ny >= m:
            continue
        else:
            x, y = nx, ny

        vertical[1], vertical[3] = horizontal[1], horizontal[3]
        if action[i] == 3:
            vertical = vertical[1:] + [vertical[0]]
        else:
            vertical = [vertical[-1]] + vertical[:-1]

        if graph[x][y]:
            vertical[-1] = graph[x][y]
            graph[x][y] = 0
        else:
            graph[x][y] = vertical[-1]
        horizontal[1], horizontal[3] = vertical[1], vertical[3]
        print(vertical[1])

 

방법 2 - 메모리 30840KB / 시간 72ms / 코드 길이 898B

 

방법 1과 달리 주사위 전체에 대한 방향 전환의 값 변환을 고려한 코드를 구성한다.

이를 통해 더 짧고 간결하게 구성할 수 있다.

 

 

n,m,x,y,k = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(n)]
action = list(map(int, input().split()))
dice = [0]*6

def turn(dir):
    if dir == 1:
        dice[0],dice[2],dice[3],dice[5] = dice[3],dice[0],dice[5],dice[2]
    elif dir == 2:
        dice[0],dice[2],dice[3],dice[5] = dice[2],dice[5],dice[0],dice[3]
    elif dir == 3:
        dice[0],dice[1],dice[4],dice[5] = dice[4],dice[0],dice[5],dice[1]
    elif dir == 4:
        dice[0],dice[1],dice[4],dice[5] = dice[1],dice[5],dice[0],dice[4]

move = [(0,0),(0,1),(0,-1),(-1,0),(1,0)]

for i in range(k):
    nx, ny = x + move[action[i]][0], y + move[action[i]][1]
    if 0 <= nx < n and 0 <= ny < m:
        x, y = nx, ny
        turn(action[i])
        if graph[x][y]:
            dice[5] = graph[x][y]
            graph[x][y] = 0
        else:
            graph[x][y] = dice[5]
        print(dice[0])

 

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading