๋ฌธ์ ๋งํฌ :https://www.acmicpc.net/problem/2164
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)
๋ฐ์ํ
'๐ค ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[codeforce] D. Non-zero Segments (0) | 2023.05.21 |
---|---|
baekjoon. ํธ๋ฆฌ ์ํ [Silver I] [python] (0) | 2023.03.25 |
baekjoon. ํฌ๊ฒ ๋ง๋ค๊ธฐ (2812) [python][Gold III] (0) | 2023.02.22 |
baekjoon. ํ2 (18258) [python][Silver IV] (0) | 2023.02.20 |
baekjoon. ์ ๋ง๋๊ธฐ (10799) [python][Silver II] (0) | 2023.01.29 |