-
Notifications
You must be signed in to change notification settings - Fork 1
/
8-puzle-A*.py
69 lines (62 loc) · 1.99 KB
/
8-puzle-A*.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
62
63
64
65
66
67
68
69
from queue import PriorityQueue
def calculateH(current, goal):
h = 0
for i in range(3):
for j in range(3):
if current[i][j] != goal[i][j]:
h += 1
return h
def generate_next_moves(current):
res = []
for i in range(3):
for j in range(3):
if current[i][j] == 0:
if i > 0:
temp = current.copy()
temp[i][j] = temp[i-1][j]
temp[i-1][j] = 0
res.append(temp)
if i < 2:
temp = current.copy()
temp[i][j] = temp[i+1][j]
temp[i+1][j] = 0
res.append(temp)
if j > 0:
temp = current.copy()
temp[i][j] = temp[i][j-1]
temp[i][j-1] = 0
res.append(temp)
if j < 2:
temp = current.copy()
temp[i][j] = temp[i][j+1]
temp[i][j+1] = 0
res.append(temp)
return res
def solve(start, goal, moves):
q = PriorityQueue()
q.put((calculateH(start, goal), start))
visited = set()
while not q.empty():
current = q.get()
if current[1] == goal:
print("Solved in " + str(moves[0]) + " moves!")
return
else:
visited.add(tuple(map(tuple, current[1])))
moves[0] += 1
if moves[0] > 1000:
print("Could not solve in 1000 moves.")
return
print("Move: " + str(moves[0]))
print(current[1])
print()
next_moves = generate_next_moves(current[1])
for move in next_moves:
if tuple(map(tuple, move)) not in visited:
q.put((calculateH(move, goal), move))
def main():
start = [[1,2,3],[4,6,8],[7,5,0]]
goal = [[1,2,3],[4,5,6],[7,8,0]]
moves = [0]
solve(start, goal, moves)
main()