forked from duaraghav8/N-Puzzle-through-A-Star
-
Notifications
You must be signed in to change notification settings - Fork 0
/
n_puzzle.py
87 lines (71 loc) · 2.6 KB
/
n_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from heapq import heapify, heappush, heappop;
def hamming (dim, grid, target):
counter = 0;
for i in range (dim):
for j in range (dim):
if (not (grid [i] [j] == target [i] [j] or grid [i] [j] == 0)):
counter += 1;
return (counter);
def manhattan (dim, grid, target):
result = 0;
for i in range (dim):
for j in range (dim):
if (target [i] [j] == 0):
continue;
for l in range (dim):
for m in range (dim):
if (target [i] [j] == grid [l] [m]):
result += (abs (m - j) + abs (l - i));
break;
return (result);
def getNextStates (dim, current):
nextStates = [];
empty = None;
for i in range (dim):
try:
empty = current [i].index (0);
except Exception as e:
continue;
empty = (i, empty);
break;
if (empty [1] < (dim - 1)):
a = [ i.copy () for i in current ];
a [empty [0]] [empty [1]], a [empty [0]] [empty [1] + 1] = a [empty [0]] [empty [1] + 1], a [empty [0]] [empty [1]];
nextStates.append (('RIGHT', a, (empty [0], empty [1] + 1)));
if (empty [1] > 0):
b = [ i.copy () for i in current ];
b [empty [0]] [empty [1]], b [empty [0]] [empty [1] - 1] = b [empty [0]] [empty [1] - 1], b [empty [0]] [empty [1]];
nextStates.append (('LEFT', b, (empty [0], empty [1] - 1)));
if (empty [0] > 0):
c = [ i.copy () for i in current ];
c [empty [0]] [empty [1]], c [empty [0] - 1] [empty [1]] = c [empty [0] - 1] [empty [1]], c [empty [0]] [empty [1]];
nextStates.append (('UP', c, (empty [0] - 1, empty [1])));
if (empty [0] < (dim - 1)):
d = [ i.copy () for i in current ];
d [empty [0]] [empty [1]], d [empty [0] + 1] [empty [1]] = d [empty [0] + 1] [empty [1]], d [empty [0]] [empty [1]];
nextStates.append (('DOWN', d, (empty [0] + 1, empty [1])));
return (nextStates);
def getSequenceInfo (dim, grid):
target = [ [j for j in range (i, i + dim)] for i in range (0, (dim * (dim - 1)) + 1, dim) ];
current = (manhattan (dim, grid, target), 0, [], grid)
stateTree = [current];
heapify (stateTree);
while (not current [-1] == target):
current = heappop (stateTree);
# print (current);
for state in getNextStates (dim, current [-1]):
heappush (stateTree, (manhattan (dim, state [1], target) + current [1] + 1, current [1] + 1, current [2] + [state [0]], state [1]));
return (current [1], current [2]);
if (__name__ == '__main__'):
dim = int (input ());
row = numCount = 0;
grid = [[] for i in range (dim)];
for i in range (dim ** 2):
grid [row].append (int (input ()));
numCount += 1;
if (numCount % dim == 0):
row += 1;
numCount = 0;
seqCount, sequence = getSequenceInfo (dim, grid);
print (seqCount);
print ('\n'.join (sequence));