Skip to content

Commit 4cdf80e

Browse files
committed
116차 2번 다시 문제풀이
1 parent a379461 commit 4cdf80e

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
import sys
2+
from collections import *
23

34
input = sys.stdin.readline
45

56
n, t = map(int, input().split())
6-
hold = []
7+
holds = set()
8+
y_dict = defaultdict(list)
9+
710
for _ in range(n):
8-
hold.append(list(map(int, input().split())))
11+
x, y = map(int, input().split())
12+
holds.add((x, y))
13+
y_dict[y].append(x)
14+
15+
visited = set()
16+
17+
q = deque([(0, 0, 0)])
18+
while q:
19+
x, y, cnt = q.popleft()
20+
if y == t:
21+
print(cnt)
22+
break
23+
24+
for ny in range(y - 2, y + 3):
25+
if ny not in y_dict:
26+
continue
27+
for nx in y_dict[ny]:
28+
if abs(nx - x) <= 2 and (nx, ny) in holds and (nx, ny) not in visited:
29+
visited.add((nx, ny))
30+
q.append((nx, ny, cnt + 1))
931

10-
hold.sort() # [[0, 2], [1, 2], [3, 2], [4, 1], [6, 3]]
32+
else:
33+
print(-1)

0 commit comments

Comments
 (0)