[백준] 뱀

[백준] 뱀

https://www.acmicpc.net/problem/3190



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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import sys
input = sys.stdin.readline

N, K, board, snake, L, curve, cur_direct = [None] * 7

direct_move = {'L': (0, -1), 'R': (0, 1), 'U': (-1, 0), 'D': (1, 0)}
change_dir = {'L': {'L': 'D', 'R': 'U', 'U': 'L', 'D': 'R'}, 'D': {'L': 'U', 'R': 'D', 'U': 'R', 'D': 'L'}}


def init():
global N, K, board, snake, L, curve, cur_direct
N = int(input())
K = int(input())

board = [[-1] + [0] * N + [-1] for _ in range(N + 2)]
board[0] = board[-1] = [-1] * (N + 2)
board[1][1] = 1

for i in range(K):
row, col = list(map(lambda x: int(x), input().split(' ')))
board[row][col] = 2 # apple

snake = [(1, 1)]
cur_direct = 'R'
L = int(input())
curve = []

for i in range(L):
X, C = input()[:-1].split(' ')
curve.append((int(X), C))


def move():
global cur_direct

move_dist = direct_move[cur_direct]
next_head = (snake[0][0] + move_dist[0], snake[0][1] + move_dist[1])
if board[next_head[0]][next_head[1]] == 1 or board[next_head[0]][next_head[1]] == -1:
return False

if board[next_head[0]][next_head[1]] == 0: # not apple
tail = snake.pop()
board[tail[0]][tail[1]] = 0

snake.insert(0, next_head)
board[next_head[0]][next_head[1]] = 1

return True


def solution():
global cur_direct

time = 0
while True:
if curve:
X, C = curve.pop(0)
while X > time:
time += 1
if not move():
print(time)
return
cur_direct = change_dir[C][cur_direct]
time += 1
if not move():
print(time)
return
else:
time += 1
if not move():
print(time)
return


init()
solution()

Author

Jinki Kim

Posted on

2021-10-21

Updated on

2021-10-21

Licensed under

댓글