-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEight-puzzle-BFS.py
134 lines (114 loc) · 3.49 KB
/
Eight-puzzle-BFS.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
import time
col=3
class Node:
def __init__(self, p):
self.children=[]
self.parent=None
self.puzzle=p[:]
self.x=0
def printPuzzle(self):
print()
m=0
for i in range(col):
for j in range(col):
print(self.puzzle[m],end=" ")
m+=1
print()
def movetoright(self):
if ((self.x)%col < (col-1)):
pc=self.puzzle[:]
pc[self.x],pc[self.x+1]=pc[self.x+1],pc[self.x]
child=Node(pc)
self.children.append(child)
child.parent=self
def movetoleft(self):
if ((self.x) % col > 0):
pc = self.puzzle[:]
pc[(self.x)], pc[(self.x) - 1] = pc[(self.x) - 1], pc[(self.x)]
child = Node(pc)
self.children.append(child)
child.parent = self
def moveup(self):
if ((self.x) -3 > 0):
pc = self.puzzle[:]
pc[(self.x)], pc[(self.x) -3] = pc[(self.x) -3], pc[(self.x)]
child = Node(pc)
self.children.append(child)
child.parent = self
def movedown(self):
if ((self.x) +3 <9):
pc = self.puzzle[:]
pc[(self.x)], pc[(self.x) + 3] = pc[(self.x) + 3], pc[(self.x)]
child = Node(pc)
self.children.append(child)
child.parent = self
def goaltest(self):
isGoal=True
for i in range(len(self.puzzle)):
if i!=self.puzzle[i]:
isGoal=False
return isGoal
return isGoal
def expandNode(self):
for i in range(len(self.puzzle)):
if self.puzzle[i]==0:
self.x=i
self.movetoright()
self.movedown()
self.movetoleft()
self.moveup()
def isunsolvable(self):
count=0
for i in range(8):
for j in range(i,9):
if self.puzzle[i] > self.puzzle[j] and self.puzzle[j]!=0:
count+=1
if count%2==1:
return True
else:
return False
class search:
def __init__(self):
pass
def breadthFirstSearch(self,root):
openlist=[]
visited=set()
openlist.append(root)
visited.add(tuple(root.puzzle))
while(True):
currentNode=openlist.pop(0)
if currentNode.goaltest():
pathtosolution= search.pathtrace(currentNode)
print(len(visited))
return pathtosolution
currentNode.expandNode()
for i in range(len(currentNode.children)):
currentchild=currentNode.children[i]
if (not (tuple(currentchild.puzzle) in visited)) :
openlist.append(currentchild)
visited.add(tuple(currentchild.puzzle))
def pathtrace(n):
current=n
path=[]
#print("Tracing Path")
path.append(current)
while current.parent!=None:
current=current.parent
path.append(current)
return path
if __name__== "__main__":
puzzle=[int(x) for x in input().split()]
root=Node(puzzle)
if root.isunsolvable():
print("No solution Found")
else:
s=search()
print("Finding Solution")
start=time.time()
solution=s.breadthFirstSearch(root)
end=time.time()
solution.reverse()
for i in range(len(solution)):
solution[i].printPuzzle()
print("No of steps required to solve the puzzle",len(solution)-1)
print("Time Taken",end-start)