Python Forum
Shortest paths to win snake and ladder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shortest paths to win snake and ladder
#4
Thanks, ThomasL for thinking fellow people as that low.
FYI, I did do my search (research) before posting here to discuss with my fellow python users/teachers/developers their point of views and most of the answer where either implementation of snake and ladder game itself or no: of least steps to win (some example with 1 shortest path)
But, I wanted all shortest paths if you read my first post (Question)

After looking into Administrator post, I sat down and came with below code. Also, I understood we should not ignore snakes as suggested above since a snake can be between 2 big ladders which will shorten the steps needed. Ofcourse, we can still optimize it.

top = 100
snakes = {16: 6, 48: 26, 49: 11, 56: 53, 62: 19,
64: 60, 87: 24, 93: 73, 95: 75, 98: 78}
ladders = {2: 38, 4: 14, 9: 31, 21: 42,
28: 84, 36: 44, 51: 67, 71: 91, 80: 100}
jump = {**snakes, **ladders}

steps = 0
paths = dict()
reached_top = False
all_short_paths = list()
reached_bottom = False

while not reached_top:
    steps += 1
    if steps == 1:
        paths[1] = [[0,1,2,3,4,5,6]]
        for index,val in enumerate(paths[1][0]):
            if val in jump.keys():
                paths[1][0][index] = jump[val]
    else:
        paths[steps] = list()
        for lists in paths[steps - 1]:
            for val in lists[1:]:
                paths[steps].append([val, val+1, val+2, val+3, val+4, val+5, val+6])

        for index1,lists in enumerate(paths[steps]):
            for index2,val in enumerate(lists):
                if val in jump.keys():
                    paths[steps][index1][index2] = jump[val]
                    val = jump[val]
                if val == top:
                    reached_top = True
                    if paths[steps][index1][0]:
                        all_short_paths.append([100,paths[steps][index1][0]])

all_short_paths = [list(x) for x in set(tuple(x) for x in all_short_paths)]
steps_down = 1
while not reached_bottom:
    new_all_short_paths = list()
    for lists1 in all_short_paths:
        for lists2 in paths[steps - steps_down]:
            if lists1[-1] in lists2:
                new_all_short_paths.append(lists1 + [lists2[0]])
    all_short_paths = [list(x) for x in set(tuple(x) for x in new_all_short_paths)]
    steps_down += 1
    if steps - steps_down == 1:
        reached_bottom = True

print(f"Shortest steps to win is {steps}.\nNo: of Shortest paths are,\n {len(all_short_paths)}")
print(all_short_paths)
Reply


Messages In This Thread
Shortest paths to win snake and ladder - by sandaab - Jun-28-2019, 10:17 PM
RE: Shortest paths to win snake and ladder - by sandaab - Jun-29-2019, 05:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Good class design - with a Snake game as an example bear 1 1,883 Jan-24-2024, 08:36 AM
Last Post: annakenna
  pdf2image, poppler and paths jehoshua 18 15,171 Jun-14-2022, 06:38 AM
Last Post: jehoshua
  Windows paths issue otalado 3 1,527 May-29-2022, 09:11 AM
Last Post: snippsat
  Help for the shortest way to install a suitable version of Python, Numpy, and OpenCV. Ezzat 2 2,335 Dec-23-2021, 12:34 PM
Last Post: snippsat
  automatically get absolute paths oclmedyb 1 2,146 Mar-11-2021, 04:31 PM
Last Post: deanhystad
  Mouvement Snake (The game) Catif 0 1,344 Nov-27-2020, 08:28 PM
Last Post: Catif
  chkFile with absolute paths JarredAwesome 7 3,057 Sep-21-2020, 03:51 AM
Last Post: bowlofred
  Paths millpond 12 5,337 Jul-30-2020, 01:16 PM
Last Post: snippsat
  Problems with windows paths delphinis 6 5,331 Jul-21-2020, 06:11 PM
Last Post: Gribouillis
  How to handle paths with spaces in the name? zBernie 1 6,794 Nov-22-2018, 04:04 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020