-
Notifications
You must be signed in to change notification settings - Fork 24
/
773 - Sliding Puzzle.py
61 lines (59 loc) · 1.92 KB
/
773 - Sliding Puzzle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from heapq import heappush, heappop
class Solution(object):
def slidingPuzzle(self, board):
"""
:type board: List[List[int]]
:rtype: int
"""
# Encode the board as a 1-D array
start = []
for i in range(len(board)):
for j in range(len(board[i])):
start.append(board[i][j])
goal = [1,2,3,4,5,0]
heap = []
heappush(heap, (self.calculateHeuristic(start),start))
adjacent_squares = {
0: [1,3],
1: [0,2,4],
2: [1,5],
3: [0,4],
4: [1,3,5],
5: [2,4]
}
visited = set()
while len(heap) > 0:
state = heappop(heap)
board_state = state[1]
# Check if we've hit the goal state
if self.calculateHeuristic(board_state) == 0:
return state[0]
visited.add(tuple(board_state))
g = state[0] - self.calculateHeuristic(board_state) + 1
# Generate neighbours
index_of_zero = board_state.index(0)
for square in adjacent_squares[index_of_zero]:
new_board_state = board_state[:]
new_board_state[index_of_zero] = new_board_state[square]
new_board_state[square] = 0
if tuple(new_board_state) in visited:
continue
h = self.calculateHeuristic(new_board_state)
heappush(heap, (g + h, new_board_state))
return -1
# Calculates the heuristic function, ex. the # of mismatched squares
def calculateHeuristic(self, board):
h = 0
if board[0] != 1:
h += 1
if board[1] != 2:
h += 1
if board[2] != 3:
h += 1
if board[3] != 4:
h += 1
if board[4] != 5:
h += 1
if board[5] != 0:
h += 1
return h