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 heretemp={}fora,binladders+snakes:temp[a]=bgraph={}foriinrange(1,100):ifinotintemp:graph[i]=[]forrollinrange(1,7):pos=i+rollifpos<=100:pos=temp.get(pos,pos)graph[i].append(pos)visited=set()queue=deque()queue.append((1,0))visited.add(1)whilelen(queue)>0:current,roll_count=queue.popleft()ifcurrent==100:returnroll_countforningraph[current]:ifnnotinvisited:queue.append((n,roll_count+1))visited.add(n)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 →