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):# Write your code heregraph1=dict()foru,vinladders:graph1[u]=vgraph2=dict()foru,vinsnakes:graph2[u]=vq=deque()visit=set()q.append([1,0])while(q):square,moves=q.popleft()foriinrange(1,7):next_square=square+iifgraph1.get(next_square):next_square=graph1.get(next_square)elifgraph2.get(next_square):next_square=graph2.get(next_square)ifnext_square==100:returnmoves+1ifnext_squarenotinvisit:visit.add(next_square)q.append([next_square,moves+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 →
Python3 solution: