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.
defquickestWayUp(ladders,snakes):# Store snakes and ladders for lookupgraph={s[0]:s[1]forsinsnakes}graph.update({l[0]:l[1]forlinladders})visited=set([1])queue=[1]rolls={}whilequeue:idx=queue.pop(0)foriinrange(1,7):# If neighbour is a snake/ladder, jump to its end point# else, increment as isneighbour=graph.get(idx+i,idx+i)ifneighbournotinvisited:rolls[neighbour]=rolls.get(idx,0)+1visited.add(neighbour)queue.append(neighbour)ifneighbour==100:returnrolls[neighbour]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 →