Previously, I had published a rather inefficient recursive solution to the problem of getting the fewest number of moves through a snake and ladder panel.

I have found a much faster solution with the Dijkstra algorithm and believe that it is correct.

Each box on the board is linked to a box between 1 and 6 that is larger than the box with a weight of 1 (equal to a roll of 1 to 6 on a die). All snakes and ladders connect squares weighing 1. The goal was to get the lowest total cost of the path between 1 and 100 (here 0 and 99, since I used list indexes).

This is the first time I implement the Dijkstra algorithm and the first time I use it `namedtuples`

, I'm not sure if using namedtuples was appropriate, but it made it clearer in my mind.

I think I have massively overly complicated code parts, especially within the `to the`

Loop under the condition `if edge.start == next_item:`

, I seem to use much too much list comprehension, and I know the solution will be slower than it could be. Could someone help me find better ways to access the variables in my queue of named tuples?

```
"" Calculate the shortest way across a snake and ladder tableau with Dijkstra's shortest path. ""
from collections import namedtuple
Edge = namedtuple ("Edge", ("Start", "End", "Cost"))
Stack = namedtuple ("stack", ("start", "pathcost", "totalcost")
Class graph:
Class generates graph and calculates shortest path
def __init __ (self, edges):
"" Generate edges in the graph ""
self.edges = [Edge(*edge) for edge in edges]
def dijkstra_path (self, start, end):
Function that calculates the shortest path
if start> = end or start < 0 or end > 99:
Return -1
Queue = sorted (
(
Stack (edge.end, edge.cost, edge.cost)
for edge in self.edges
if edge.start == start
)
Key = lambda x: x[2].
)
during the queue:
next_item, _, current_total = queue.pop (0)
if next_item == end:
Return current_total
for edge in self.edges:
if edge.start == next_item:
if edge.end in [item.start for item in queue]:
current_cost = [
item.totalcost for item in queue if item.start == edge.end
][0]
if not current_cost <edge.cost + current_total:
Queue = [item for item in queue if item.start != edge.end]
queue.append (
Stack (edge.end, edge.cost, edge.cost + current_total)
)
otherwise:
queue.append (
Stack (edge.end, edge.cost, edge.cost + current_total)
)
Queue = sorted (queue, key = lambda x: x[2])
def build_graph ():
chessboard
list_board = [[i, i + j, 1] for i in the range (100) for j in the range (1, 7)]# Ladders
list_board.append ([1, 37, 1])
list_board.append ([19, 59, 1])
list_board.append ([28, 63, 1])
list_board.append ([55, 99, 1])
# Snakes
list_board.append ([91, 13, 1])
list_board.append ([86, 11, 1])
list_board.append ([53, 2, 1])
list_board.append ([41, 13, 1])
return list_board
if __name__ == "__main__":
GRAPH = graph (build_graph ())
FROM = 0
TO = 100
NUMBER_STEPS = GRAPH.dijkstra_path (FROM, TO)
if not NUMBER_STEPS == -1:
print (f "Can finish the game in at least {NUMBER_STEPS} dice")
otherwise:
print ("Error, make sure the starting point is between 0 and 99 and less than the endpoint",
"what must be the same or equal to 99")
```