Skip to content

Commit 5c4c18d

Browse files
committed
[BOJ] 뱀 / 골드 4 / 100분 실패
https://www.acmicpc.net/problem/3190
1 parent dea19b3 commit 5c4c18d

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from collections import deque
2+
3+
n = int(input())
4+
k = int(input())
5+
6+
graph = [[0] * n for _ in range(n)]
7+
dx = [0, 1, 0, -1]
8+
dy = [1, 0, -1, 0]
9+
10+
for i in range(k):
11+
a, b = map(int, input().split())
12+
graph[a - 1][b - 1] = 2
13+
14+
l = int(input())
15+
dirDict = dict()
16+
queue = deque()
17+
queue.append((0, 0))
18+
19+
for i in range(l):
20+
x, c = input().split()
21+
dirDict[int(x)] = c
22+
23+
x, y = 0, 0
24+
graph[x][y] = 1
25+
cnt = 0
26+
direction = 0
27+
28+
def turn(alpha):
29+
global direction
30+
if alpha == 'L':
31+
direction = (direction - 1) % 4
32+
else:
33+
direction = (direction + 1) % 4
34+
35+
36+
while True:
37+
cnt += 1
38+
x += dx[direction]
39+
y += dy[direction]
40+
41+
if x < 0 or x >= n or y < 0 or y >= n:
42+
break
43+
44+
if graph[x][y] == 2:
45+
graph[x][y] = 1
46+
queue.append((x, y))
47+
if cnt in dirDict:
48+
turn(dirDict[cnt])
49+
50+
elif graph[x][y] == 0:
51+
graph[x][y] = 1
52+
queue.append((x, y))
53+
tx, ty = queue.popleft()
54+
graph[tx][ty] = 0
55+
if cnt in dirDict:
56+
turn(dirDict[cnt])
57+
58+
else:
59+
break
60+
61+
print(cnt)

0 commit comments

Comments
 (0)