-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq2.py
184 lines (173 loc) · 3.63 KB
/
q2.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
"""
assume
heuristic: h(s)=number of 'w's after first b + number of 'b's before last w
in the previous code, now instead of alternative choosing of what to swap, i'll swap with nearest 'w' and 'b' and depending on h(s) s will be assigned
if h(s)=0 the stop
wwbwbb_
"""
import copy
print("White block=\'w\'\nBlack block=\'b\'\nEmpty block=\'_\'")
print("Enter the start configuration:")
s=input()
print("------------------------------")
cw=0
cb=0
ce=0
pe=0
l=0
for k in s:
if k=='w':
cw+=1
if k=='b':
cb+=1
if k=='_':
ce+=1
pe=l
l+=1
if cw!=3 or cb!=3 or ce!=1:
print("Wrong input!!")
exit()
def sorted(s):
if s=="_wwwbbb" or s=="w_wwbbb" or s=="ww_wbbb" or s=="www_bbb" or s=="wwwb_bb" or s=="wwwbb_b" or s=="wwwbbb_":
return True
else:
return False
def h(s):
cB=0
cW=0
nB=0
nW=0
for i in s:
if i=='w':
cW+=1
if cB!=0:
nW+=1
if i=='b':
cB+=1
if cW<3:
nB+=1
return nB+nW
visited=[]
stack=[]
s=list(s)
#print(h(s))
#print(h("w_wbwbb"))
#print(h("ww_bwbb"))
a=[]
stack.append([s,pe,0,a,0]) #string,index of _, cost, path list
#print(stack[0])
found=False
#while not found and sorted("".join(s)):
def insertIt(s,i,c,path,ch):
j=0
cAndH=c+h(s)+ch
if len(stack)==0:
stack.append([s,i,c,path,cAndH])
return
if s not in visited:
while cAndH>stack[j][4]:
j+=1
if j==len(stack):
stack.append([s,i,c,path,cAndH])
return
stack.insert(j,[s,i,c,path,cAndH])
finalCost=0
finalPath=[]
found=False
if sorted("".join(s)):
finalCost=0
finalPath=["".join(s)]
found=True
while not found and len(stack)!=0:
node=stack.pop(0)
#print(node)
ss=copy.deepcopy(node[0])
i=node[1]
cost=node[2]
visited.append(ss)
a=max(i-3,0)
b=min(i+4,7)
#print(a,b)
ch=node[4]
for j in range(a,b):
path=copy.deepcopy(node[3])
c=abs(j-i)
if c==0:
continue
ss=copy.deepcopy(node[0])
if c==1 or c==2:
c=1
if c==3:
c=2
temp=ss[i]
#print(temp)
ss[i]=ss[j]
ss[j]=temp
c+=cost
path.append(ss)
if sorted("".join(ss)):
finalCost=c
finalPath=copy.deepcopy(path)
found=True
break
insertIt(ss,j,c,path,ch)
print("Cost=",finalCost)
j=1
for i in finalPath:
print("".join(i),j)
j+=1
"""
print(h(s))
k=0
cost=0
i=pe
while True:
foundOne=False
s=list(s)
s1=s
s2=s
ss=s[i+1:]
k=1
temp=i
c1=0
c2=0
for j in range(len(ss)):
if ss[j]=='w':
s1[temp]='w'
s1[temp+1+j]='_'
temp=temp+1+j
if j==0:
c1+=1
else:
c1+=j
foundOne=True
break
ss=s[:i]
k=0
t2=i
l=len(ss)
for j in range(len(ss)):
if ss[l-j-1]=='b':
s2[t2]='b'
s2[t2-j-1]='_'
t2=t2-j-1
if j==0:
c2+=1
else:
c2+=j
foundOne=True
break
if h(s1)>h(s2):
i=t2
s=s2
if h(s2)==0:
break
else:
i=temp
s=s1
if h(s1)==0:
break
s="".join(s)
print(s)
print(h(s))
print("Cost:",cost)"""