-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.txt
75 lines (69 loc) · 1.84 KB
/
dfs.txt
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
import copy
blocks=int(input('Total no. of Blocks = '))
s=[['A','B'],['C'],[]]
g=[['C'],['A'],['B']]
# #--------------------------------------------
# print("\n INITIAL STATE\n")
# for i in range(blocks):
# a=[]
# print('Elements in Stack',i+1,sep="-", end=" = ")
# jaadu=int(input())
# for j in range(jaadu):
# print('Value for Element',j+1,sep="-", end=" = ")
# value=input()
# a.append(value)
# print("\n ------------ \n")
# s.append(a)
# #--------------------------------------------
# print("\n GOAL STATE\n")
# for i in range(blocks):
# a=[]
# print('Elements in Stack',i+1,sep="-", end=" = ")
# jaadu=int(input())
# for j in range(jaadu):
# print('Value for Element',j+1,sep="-", end=" = ")
# value=input()
# a.append(value)
# print("\n ------------ \n")
# g.append(a)
# #--------------------------------------------
print("\n =============== \n")
print("INITAIL STATE =",s)
print("GOAL STATE =",g)
print("\n =============== \n")
open=[s]
closed=[]
success=False
#--------------------------------------------
def findStates(current):
states=[]
for i in range(len(s)):
if(len(s[i])!=0):
for j in range(len(s)):
if j!=i:
s1=copy.deepcopy(s)
x=s1[i].pop(0)
s1[j].insert(0,x)
states.append(s1)
else:
continue
return states
if s==g:
print("Initial State is Goal State itself.")
else:
while success==False and len(open)!=0:
current=open.pop(len(open)-1)
closed.append(current)
if g==current:
success=True
else:
states=findStates(current)
print("\nSTATES\n",states)
for state in states:
if state not in open and state not in closed:
open.append(state)
#print("\nOPEN\n",open)
if success:
print('Found')
else:
print('Not Found')