@@ -9,6 +9,20 @@ def edges(graph, coord):
9
9
return (n for n in ((r - 1 , c ), (r + 1 , c ), (r , c - 1 ), (r , c + 1 ))
10
10
if n in graph and graph [coord ] - graph [n ] <= 1 )
11
11
12
+ def shortest_path_to_height (graph , start , height ):
13
+ to_explore = deque ([(start , 0 )])
14
+ explored = set ()
15
+ while to_explore :
16
+ here , steps = to_explore .popleft ()
17
+ if graph [here ] == height :
18
+ return steps
19
+ for neighbor in edges (graph , here ):
20
+ if neighbor not in explored :
21
+ explored .add (neighbor )
22
+ to_explore .append ((neighbor , steps + 1 ))
23
+
24
+ raise ValueError ('No paths exist!' )
25
+
12
26
def main ():
13
27
graph = {}
14
28
end = None
@@ -25,19 +39,5 @@ def main():
25
39
steps = shortest_path_to_height (graph , end , ord ('a' ))
26
40
print (steps )
27
41
28
- def shortest_path_to_height (graph , start , height ):
29
- to_explore = deque ([(start , 0 )])
30
- explored = set ()
31
- while to_explore :
32
- here , steps = to_explore .popleft ()
33
- if graph [here ] == height :
34
- return steps
35
- for neighbor in edges (graph , here ):
36
- if neighbor not in explored :
37
- explored .add (neighbor )
38
- to_explore .append ((neighbor , steps + 1 ))
39
-
40
- raise ValueError ('No paths exist!' )
41
-
42
42
if __name__ == '__main__' :
43
43
main ()
0 commit comments