Advertisement
WadeRollins2710

Depth-First Search for Matrix Outcomes

Sep 9th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. grid = [[0, 1, 0],
  2.         [1, 0, 1],
  3.         [1, 0, 0]]
  4.  
  5.  
  6. def new_grid(grid):
  7.     new_grid = []
  8.     for line in grid:
  9.         new_grid.append(list(line))
  10.     return new_grid
  11.  
  12.  
  13. reverse_dct = {0: 1, 1: 0}
  14.  
  15. possible_grid = [[new_grid(grid), ""]]
  16. possible_move = {"x1": [[0,0], [0, 1], [0,2]], "x2": [[1,0], [1, 1], [1,2]], "x3": [[2,0], [2, 1], [2,2]], "x4": [[0,0], [1, 0], [2, 0]], "x5": [[0,1], [1, 1], [2, 1]], "x6": [[0, 2], [1, 2], [2, 2]]}
  17.  
  18.  
  19. def print_grid(grid):
  20.     for line in grid:
  21.         print(line)
  22.     print()
  23.  
  24.  
  25. def transform(grid, move):
  26.     # print(move)
  27.     temp_grid = new_grid(grid)
  28.     global possible_move, reverse_dct
  29.     for x, y in possible_move[move]:
  30.         temp_grid[x][y] = reverse_dct[temp_grid[x][y]]
  31.     # print_grid(temp_grid)
  32.     return new_grid(temp_grid)
  33.  
  34.  
  35. print_grid(grid)
  36. index = 0
  37. while index < len(possible_grid):
  38.     # print(len(possible_grid))
  39.     current_grid = new_grid(possible_grid[index][0])
  40.     for i in range(1, 7):
  41.         # print("current")
  42.         # print_grid(current_grid)
  43.         _new_grid = new_grid(transform(new_grid(current_grid), "x" + str(i)))
  44.         # print_grid(_new_grid)
  45.         repeated = False
  46.         for j in range(len(possible_grid)):
  47.             if _new_grid == possible_grid[j][0]:
  48.                 # print("this grid")
  49.                 # print_grid(_new_grid)
  50.                 # print("Got repeated")
  51.                 # print_grid(possible_grid[i][0])
  52.                 repeated = True
  53.                 break
  54.         if not repeated:
  55.             # print("Found new grid")
  56.             possible_grid.append([new_grid(_new_grid), possible_grid[index][1] + "->x" + str(i)])
  57.             # print(len(possible_grid))
  58.     index += 1
  59.  
  60. for item in possible_grid:
  61.     print(item[1])
  62.     print_grid(item[0])
  63. print("Done computing")
  64. print(len(possible_grid))
  65. while True:
  66.     command = input()
  67.     if command == "exit":
  68.         break
  69.     arr = list(map(int, command.split()))
  70.     input_grid = [[arr[0], arr[1], arr[2]], [arr[3], arr[4], arr[5]], [arr[6], arr[7], arr[8]]]
  71.     for i in range(len(possible_grid)):
  72.         if input_grid == possible_grid[i][0]:
  73.             print(possible_grid[i][1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement