Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. from collections import deque
  2.  
  3. def get_new_state(index1, index2, current_state):
  4. if current_state[index1] == "0" or current_state[index2] == "0":
  5. current_state = list(current_state)
  6. current_state[index1], current_state[index2] = current_state[index2], current_state[index1]
  7. return "".join(current_state)
  8. return None
  9.  
  10. def sliding_puzzle(board):
  11. """
  12. :type board: List[List[int]]
  13. :rtype: int
  14. """
  15. min_step = 1 << 31
  16. # need to convert board to a string so that we can add it as a state in the set
  17. # construct the graph based on the positions of the next place it can swap
  18. graph = {0:[1, 3], 1:[0, 2, 4], 2:[1, 5], 3:[0, 4], 4:[1, 3, 5], 5:[2, 4]}
  19.  
  20. # convert init board to an initial state
  21. init_state = [] + board[0] + board[1]
  22. init_state = "".join(str(_) for _ in init_state)
  23.  
  24. visited = {init_state}
  25. queue = deque([[init_state, 0]])
  26. while queue:
  27. top = queue.popleft()
  28. current_state, step = top
  29.  
  30. # check results
  31. if current_state == "123450":
  32. min_step = min(min_step, step)
  33.  
  34. for index1 in graph:
  35. for index2 in graph[index1]:
  36. new_state = self.get_new_state(index1, index2, current_state)
  37. if new_state is not None and new_state not in visited:
  38. queue.append([new_state, step + 1])
  39. visited.add(new_state)
  40. if min_step == 1<< 31:
  41. return -1
  42. return min_step
  43.  
  44. #print(sliding_puzzle([[1,2,3],[4,0,5]]))
  45.  
  46. >>> 1
  47.  
  48. #Explanation: Swap the 0 and the 5 in one move.
  49.  
  50. #print(sliding_puzzle([[1,2,3],[5,4,0]]))
  51.  
  52. >>> -1
  53.  
  54. #Explanation: No number of moves will make the board solved.
  55.  
  56. #print(sliding_puzzle([[4,1,2],[5,0,3]]))
  57.  
  58. >>> 5
  59.  
  60. #Explanation: 5 is the smallest number of moves that solves the board.
  61.  
  62. #An example path -
  63. #After move 0: [[4,1,2],[5,0,3]]
  64. #After move 1: [[4,1,2],[0,5,3]]
  65. #After move 2: [[0,1,2],[4,5,3]]
  66. #After move 3: [[1,0,2],[4,5,3]]
  67. #After move 4: [[1,2,0],[4,5,3]]
  68. #After move 5: [[1,2,3],[4,5,0]]
  69.  
  70. #print(sliding_puzzle([[3,2,4],[1,5,0]]))
  71.  
  72. >>> 14
  73.  
  74. %timeit output.sliding_puzzle([[1,2,3],[4,0,5]])
  75. 3.24 ms ± 629 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
  76.  
  77. %timeit output.sliding_puzzle([[1,2,3],[5,4,0]])
  78. 3.17 ms ± 633 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
  79.  
  80. %timeit output.sliding_puzzle([[4,1,2],[5,0,3]])
  81. 3.32 ms ± 719 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
  82.  
  83. %timeit output.sliding_puzzle([[3,2,4],[1,5,0]])
  84. 2.75 ms ± 131 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement