/
npuzzle.py
executable file
·61 lines (48 loc) · 1.48 KB
/
npuzzle.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
#!/bin/python3
import sys
sys.path.insert(0, 'src/')
import core
import solve
def inversions(puzzle):
total = 0
z = 0
for i in range(len(puzzle)):
if puzzle[i] == 0:
z += 1
if (z == 2):
return -1
continue
count = 0
for j in range(i, len(puzzle)):
if puzzle[i] > puzzle[j] and puzzle[j] != 0:
count += 1
total += count
return total
def isSolvable(snailPuzzle, size, endDic):
start = [c for row in snailPuzzle for c in row]
endSnailPuzzle = [[0 for _ in range(size)] for _ in range(size)]
for i, pt in endDic.items():
x, y = pt
endSnailPuzzle[x][y] = i
end = [c for row in endSnailPuzzle for c in row]
startInversions = inversions(start)
if startInversions == -1:
return False
endInversions = inversions(end)
if size % 2 == 0:
startInversions += start.index(0) // size
endInversions += end.index(0) // size
return startInversions % 2 == endInversions % 2
def main():
(size, start, end, heuristic, force) = core.init()
if not isSolvable(start, size, end):
print("This puzzle is not solvable")
sys.exit()
nSelectedStates, nMaxStates, solution = solve.solve(size, start, end, heuristic, force)
nStates, solution = solution
for state in solution:
core.display(state)
print("Total number of states ever selected in open set: {:d}".format(nSelectedStates))
print("Maximum number of states ever represented in memory at the same time: {:d}".format(nMaxStates))
print("Number of moves required to solve the puzzle: {:d}".format(nStates))
main()