Advertisement
Mussab_Blue

Alphabet Path Finder

Aug 20th, 2022 (edited)
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. def find_moves_sequence(grid):
  2.     grid = [[*row] for row in grid]
  3.     w, h = len(grid[0])-1, len(grid)-1
  4.  
  5.     dirs = {
  6.         (0, 1):'R',
  7.         (1, 0):'D',
  8.         (0,-1):'L',
  9.         (-1,0):'U'
  10.     }
  11.  
  12.     def foo(x,y, _ascii):
  13.         if x > h or y > w: return None  
  14.         if _ascii > 90: return []
  15.  
  16.         for (i, j), direction in dirs.items():
  17.             if grid[x][y]==chr(_ascii) and (path:=foo(x+i, y+j, _ascii+1)) != None:
  18.                 return [((x,y), direction), *path]
  19.         return None
  20.  
  21.     path, directions = [], []
  22.     for coord, dirc in foo(0,0, 65):
  23.         path.append(coord); directions.append(dirc)
  24.  
  25.     for i, row in enumerate(grid):
  26.         print(row,'\t'.rjust(6), ["#" if (i, ii) in path else n for ii, n in enumerate(row)])  
  27.     print()
  28.     print('Moves Sequence:', ''.join(directions[:-1]))
  29.     print({grid[x][y]:(x,y) for (x,y) in path})
  30.  
  31. grid = ["ACRFGHOT",
  32.         "BCDEVIQR",
  33.         "CXDUKJPQ",
  34.         "DYASLOPQ",
  35.         "ETVHMNSR",
  36.         "FSZVVUTB",
  37.         "GDYXWUPV",
  38.         "HEWQSDEF"]
  39.  
  40. find_moves_sequence(grid)
  41.  
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement