You are viewing a single comment's thread. Return to all comments →

My Python3 solution using BFS:

def get_graph(ladders, snakes): graph = defaultdict(dict) for start, finish in ladders: graph[start][finish] = 0 for start, finish in snakes: graph[start][finish] = 0 return graph def quickestWayUp(ladders, snakes): graph = get_graph(ladders, snakes) queue = deque() queue.append(1) costs = dict() visited = set([1]) while queue: current_node = queue.popleft() neighbours = graph.get(current_node, {current_node + i: 1 for i in range(1, 7)}) for neighbour in neighbours: if neighbour not in visited: costs[neighbour] = costs.get(current_node, 0) + neighbours[neighbour] visited.add(neighbour) queue.append(neighbour) if neighbour == 100: return costs[neighbour] return -1

## Snakes and Ladders: The Quickest Way Up

You are viewing a single comment's thread. Return to all comments →

My Python3 solution using BFS: