-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvolcano.py
More file actions
42 lines (38 loc) · 890 Bytes
/
Copy pathvolcano.py
File metadata and controls
42 lines (38 loc) · 890 Bytes
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
from collections import deque
def volcano():
n, m = map(long, raw_input().strip().split())
volcano = {}
for i in xrange(m):
r, s = map(long, raw_input().strip().split())
volcano[(r,s)] = True
q = deque()
prev = {}
start = (1, 1)
end = (n, n)
if start == end:
return 0
q.append(start)
flag = False
while len(q) > 0:
p = q.popleft()
if p == end:
flag = True
break
n1 = (p[0] + 1, p[1])
n2 = (p[0], p[1] + 1)
if n1 not in volcano:
prev[n1] = p
q.append(n1)
if n2 not in volcano:
prev[n2] = p
q.append(n2)
if flag:
time = 1
cur = prev[end]
while cur != start:
time += 1
cur = prev[cur]
return time
else:
return -1
print volcano()