forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 0
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())