We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
from collections import deque
def quickestWayUp(ladders, snakes):
graph = {}
for x, y in (ladders + snakes):
graph[x] = y
visited = [False]*(101)
q = deque([(1, 0)])
visited[1] = True
while q:
node, roll = q.popleft()
if node == 100:
return roll
visited[node] = True
for i in range(1, 7):
nodeNext = node + i
if nodeNext <= 100 and visited[nodeNext] == False:
if nodeNext in graph:
q.append(( graph[nodeNext], roll + 1 ))
else:
q.append(( nodeNext, roll + 1 ))
return -1
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Snakes and Ladders: The Quickest Way Up
You are viewing a single comment's thread. Return to all comments →
from collections import deque def quickestWayUp(ladders, snakes):