forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path5.py
More file actions
62 lines (55 loc) Β· 1.95 KB
/
5.py
File metadata and controls
62 lines (55 loc) Β· 1.95 KB
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
n = int(input())
k = int(input())
data = [[0] * (n + 1) for _ in range(n + 1)] # λ§΅ μ 보
info = [] # λ°©ν₯ νμ μ 보
# λ§΅ μ 보(μ¬κ³Ό μλ κ³³μ 1λ‘ νμ)
for _ in range(k):
a, b = map(int, input().split())
data[a][b] = 1
# λ°©ν₯ νμ μ 보 μ
λ ₯
l = int(input())
for _ in range(l):
x, c = input().split()
info.append((int(x), c))
# μ²μμλ μ€λ₯Έμͺ½μ λ³΄κ³ μμΌλ―λ‘(λ, λ¨, μ, λΆ)
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
def turn(direction, c):
if c == "L":
direction = (direction - 1) % 4
else:
direction = (direction + 1) % 4
return direction
def simulate():
x, y = 1, 1 # λ±μ 머리 μμΉ
data[x][y] = 2 # λ±μ΄ μ‘΄μ¬νλ μμΉλ 2λ‘ νμ
direction = 0 # μ²μμλ λμͺ½μ λ³΄κ³ μμ
time = 0 # μμν λ€μ μ§λ 'μ΄' μκ°
index = 0 # λ€μμ νμ ν μ 보
q = [(x, y)] # λ±μ΄ μ°¨μ§νκ³ μλ μμΉ μ 보(κΌ¬λ¦¬κ° μμͺ½)
while True:
nx = x + dx[direction]
ny = y + dy[direction]
# λ§΅ λ²μ μμ μκ³ , λ±μ λͺΈν΅μ΄ μλ μμΉλΌλ©΄
if 1 <= nx and nx <= n and 1 <= ny and ny <= n and data[nx][ny] != 2:
# μ¬κ³Όκ° μλ€λ©΄ μ΄λ νμ 꼬리 μ κ±°
if data[nx][ny] == 0:
data[nx][ny] = 2
q.append((nx, ny))
px, py = q.pop(0)
data[px][py] = 0
# μ¬κ³Όκ° μλ€λ©΄ μ΄λ νμ 꼬리 κ·Έλλ‘ λκΈ°
if data[nx][ny] == 1:
data[nx][ny] = 2
q.append((nx, ny))
# λ²½μ΄λ λ±μ λͺΈν΅κ³Ό λΆλͺνλ€λ©΄
else:
time += 1
break
x, y = nx, ny # λ€μ μμΉλ‘ 머리λ₯Ό μ΄λ
time += 1
if index < l and time == info[index][0]: # νμ ν μκ°μΈ κ²½μ° νμ
direction = turn(direction, info[index][1])
index += 1
return time
print(simulate())