Snakes and Ladders: The Quickest Way Up

  • + 0 comments
    from collections import deque
    def quickestWayUp(ladders, snakes):
        shortcuts={start:end for start, end in ladders+snakes}
        q=deque([(1,0)])
        visited=set([1])
        while q:
            position, count=q.popleft()
            if position==100:
                return count 
            for i in range(1, 7): 
                new_position=shortcuts.get(position+i,position+i)
                if new_position not in visited:
                    visited.add(new_position)
                    q.append((new_position, count+1))
        return -1