-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathn_puzzle.py
207 lines (184 loc) · 5.3 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#################################################################
# AUTHOR: ZEYNEP OZALP #
#################################################################
import copy
class Node:
def __init__(self, tiles=[], parent=None, g=0, h=0, f=0):
self.tiles = tiles
self.g = g
self.h = h
self.f = f
self.parent = parent
def isGoal(self):
return self.tiles == goal.tiles
def findBlank(self): #find the blank in a node's tiles
tiles = self.tiles
for i in range(n):
for j in range(n):
if tiles[i][j] == '_':
return i, j
def hgf(self): # calculate the manhattan distance
sum = 0
tiles = self.tiles
if self==start:
for i in range(n):
for j in range(n):
if tiles[i][j] == '_':
continue
else:
x, y = findGoal(tiles[i][j])
sum += abs(x - i) + abs(y - j)
self.h = sum
self.g = 0
self.f = sum
else:
for i in range(n):
for j in range(n):
if tiles[i][j] == '_':
continue
else:
x, y = findGoal(tiles[i][j])
sum += abs(x - i) + abs(y - j)
self.h = sum
self.g = self.parent.g + 1
self.f = self.g + sum
def genChildren(self):
tiles = self.tiles
x, y = self.findBlank()
newTiles = []
if (x + 1) < n: #moving blank down / moving a tile up
new = copy.deepcopy(tiles)
new[x][y] = new[x+1][y]
new[x+1][y] = '_'
newTiles.append(new)
if (x - 1) > -1: #moving blank up / moving a tile down
new = copy.deepcopy(tiles)
new[x][y] = new[x-1][y]
new[x-1][y] = '_'
newTiles.append(new)
if (y + 1) < n: # moving blank right / moving a tile left
new = copy.deepcopy(tiles)
new[x][y]=new[x][y+1]
new[x][y+1]='_'
newTiles.append(new)
if (y - 1) > -1: # moving blank left / moving a tile right
new = copy.deepcopy(tiles)
new[x][y] = new[x][y - 1]
new[x][y-1] = '_'
newTiles.append(new)
ret = []
for i in newTiles: #create children nodes
child = Node(i, self)
child.hgf()
ret.append(child)
return ret
def isExp(self, exp):
tiles = self.tiles
for i in exp:
if (tiles == i.tiles) and (i.f<=self.f):
return i
return None
start = Node()
goal = Node()
method = ""
m = 0
n = 0
def getInput():
global method, m, n, goal, start
method = raw_input()
m = int (raw_input())
n = int (raw_input())
tiles = []
for i in range(n):
tiles.append(raw_input().split(" "))
start.tiles = tiles
tiles = []
for i in range(n):
tiles.append(raw_input().split(" "))
goal.tiles = tiles
start.hgf()
def findOpt(lst):
opt = lst[0]
index = 0
for i in range(len(lst)): # type: Node
if lst[i].f < opt.f:
opt = lst[i]
index = i
return opt, index
def findGoal(str): #find the correct place of a tile in goal
global goal
tiles = goal.tiles
for i in range(n):
for j in range(n):
if tiles[i][j] == str:
return i, j
def Astar():
front = [start]
exp = []
while(1):
if len(front)==0:
return None
x, xIndex = findOpt(front)
del front[xIndex]
if(x.isGoal() and x.f<=m):
return x
if(x.isExp(exp) == None and x.f<=m):
exp.append(x)
successors = x.genChildren()
for i in successors:
front.append(i)
return None
def IDAstar():
fmax = start.f
while(1):
if(fmax > m):
return None
node, newfmax = limitedSearch(start, fmax)
if(node!=None):
return node
if(newfmax!=None):
fmax = newfmax
return None
def limitedSearch(node, limit):
if node.f > limit:
return None, node.f
if node.isGoal():
return node, limit
successors = node.genChildren()
min = float('inf')
for succ in successors:
newNode, newLimit = limitedSearch(succ, limit)
if newNode!=None:
return newNode, None
if newLimit < min:
min = newLimit
return None, min
def printOutput(sol):
global goal, n
print "SUCCESS\n"
path = []
while(sol!=None):
path.insert(0, sol)
sol=sol.parent
for i in range(len(path)):
state = path[i]
for j in range(n):
for k in range(n):
if(k<n-1):
print (state.tiles)[j][k],
else:
print (state.tiles)[j][k]
print "\n",
def solve():
global method, m, n, goal, start
getInput()
solution = None
if method == "A*":
solution = Astar()
else:
solution = IDAstar()
if(solution==None):
print "FAILURE"
else:
printOutput(solution)
solve()