๊ฐœ๋ฐœ/๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

baekjoon. ์นด๋“œ2 [python] [Silver IV]

ttoance 2023. 3. 23. 22:06

๋ฌธ์ œ๋งํฌ :https://www.acmicpc.net/problem/2164

 

2164๋ฒˆ: ์นด๋“œ2

N์žฅ์˜ ์นด๋“œ๊ฐ€ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ์นด๋“œ๋Š” ์ฐจ๋ก€๋กœ 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™์–ด ์žˆ์œผ๋ฉฐ, 1๋ฒˆ ์นด๋“œ๊ฐ€ ์ œ์ผ ์œ„์—, N๋ฒˆ ์นด๋“œ๊ฐ€ ์ œ์ผ ์•„๋ž˜์ธ ์ƒํƒœ๋กœ ์ˆœ์„œ๋Œ€๋กœ ์นด๋“œ๊ฐ€ ๋†“์—ฌ ์žˆ๋‹ค. ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋™์ž‘์„ ์นด๋“œ๊ฐ€

www.acmicpc.net

 

from collections import deque


N = int(input()) # ๋ณด๋“œ์˜ ํฌ๊ธฐ 
K = int(input()) # ์‚ฌ๊ณผ์˜ ๊ฐœ์ˆ˜ 

def printAll(board):
    for i in range(N):
        for j in range(N):
            print(board[i][j], end = ' ')
        print("")
    print("")

# ์‚ฌ๊ณผ ๊ฐฏ์ˆ˜ 
# ์ด์ฒ˜๋Ÿผ ํ•˜๋ฉด ํ•œ ํ–‰๋งŒ ๋ฐ”๊พธ์–ด๋„ ๋‹ค๋ฅธ ํ–‰์ด ์˜ํ–ฅ ๋ฐ›์Œ board = [[0] * N] * N
board =  [[0] * N for _ in range(N)]
for _ in range(K):
    appleX, appleY = map(int, input().split())
    board[appleX - 1][appleY - 1] = 'A'

# debug - printAll(board)

L = int(input()) # ๋ฐฉํ–ฅ ๋ฒˆํ™˜ ํšŸ์ˆ˜ 
runs = {}
for _ in range(L):
    X, C = input().split()
    # runs[X] = C;
    runs[int(X)] = C;


# right, down, left, up
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]



directions = {'right': 0, 'down': 1, 'left': 2, 'up': 3}
currentDirection = 'right'
currentX = 0
currentY = 0
board[currentX][currentY] = 'S'


def nextDirection(currentDirection, next):
    # print('next - ', currentDirection, ' : ', next)

    if (currentDirection == 'right' and next == 'L'):
        return 'up'
    elif (currentDirection == 'right' and next == 'D'):
        return 'down'
    elif (currentDirection == 'left' and next == 'L'):
        return 'down'
    elif (currentDirection == 'left' and next == 'D'):
        return 'up'
    elif (currentDirection == 'up' and next == 'L'):
        return 'left'
    elif (currentDirection == 'up' and next == 'D'):
        return 'right'
    elif (currentDirection == 'down' and next == 'L'):
        return 'right'
    elif (currentDirection == 'down' and next == 'D'):
        return 'left'
    else:
        return ''
    
time = 0
# ๋ฐฐ์—ด๋กœ ์ €์žฅ snake = deque([currentX, currentY])
snake = deque([[currentX, currentY]])
while True:
    # printAll(board)

    # print(runs.keys())
    # print('time - ', time, ' : currentDirection', currentDirection)

    nextX = currentX + dx[directions[currentDirection]]
    nextY = currentY + dy[directions[currentDirection]]

    # print(nextX, " : ", nextY)

    if (nextX < 0 or nextX >= N or nextY < 0 or nextY >= N):
        break;
    if (board[nextX][nextY] == 'S'):
        break;

    # ๋‹ค์Œ์ด ์‚ฌ๊ณผ๊ฐ€ ์•„๋‹ˆ๋ฉด, ํ์—์„œ ํ•˜๋‚˜ ์‚ญ์ œํ•˜๊ณ  ์ƒˆ๋กœ์šด ์ขŒํ‘œ๋ฅผ ๋„ฃ๋Š”๋‹ค. 
    if (board[nextX][nextY] == 0):
        delX, delY = snake.popleft()
        # print('del', delX, ' : ', delY)
        board[delX][delY] = 0 

    snake.append([nextX, nextY])
    board[nextX][nextY] = 'S'

    currentX = nextX
    currentY = nextY
    time = time + 1

    if (time in runs.keys()):
        currentDirection = nextDirection(currentDirection, runs[time])




print(time + 1)
๋ฐ˜์‘ํ˜•