# -*- coding: utf-8 -*-# State类,记录当前状态classState:def__init__(self, monkey=-1, box=0,banana=1, monbox=-1):
self.monkey = monkey # -1:Monkey at A 0: Monkey at B 1:Monkey at C
self.box = box # -1:box at A 0:box at B 1:box at C
self.banana = banana # Banana at C,Banana=1
self.monbox = monbox # -1: monkey not on the box 1: monkey on the box# 复制前一个状态情况给当前状态,注意不是将对象直接复制,而是创建新的对象复制原状态的数据defcopyState(state_i):
state_iplus1 = State()
state_iplus1.monkey = state_i.monkey
state_iplus1.box = state_i.box
state_iplus1.banana = state_i.banana
state_iplus1.monbox = state_i.monbox
return state_iplus1
# function monkeygoto,it makes the monkey goto the other placedefmonkeygoto(b,i):
a=b
if(a==-1):
States[i+1]=copyState(States[i])
States[i+1].monkey=-1
routesave.insert(i,"Monkey go to A")elif(a==0):
States[i+1]=copyState(States[i])
States[i+1].monkey=0
routesave.insert(i,"Monkey go to B")elif(a==1):
States[i+1]=copyState(States[i])
States[i+1].monkey=1
routesave.insert(i,"Monkey go to C")else:print("parameter is wrong")# function movebox,the monkey move the box to the other placedefmovebox(a,i):
B = a
if(B==-1):
routesave.insert(i,"Monkey move box to A")
States[i+1]=copyState(States[i])
States[i+1].monkey=-1
States[i+1].box=-1elif(B==0):
routesave.insert(i,"Monkey move box to B")
States[i+1]=copyState(States[i])
States[i+1].monkey=0
States[i+1].box=0elif(B==1):
routesave.insert(i,"Monkey move box to C")
States[i+1]=copyState(States[i])
States[i+1].monkey=1
States[i+1].box=1else:print("parameter is wrong")# function climbonto,the monkey climb onto the boxdefclimbonto(i):
routesave.insert(i,"Monkey climb onto the box")
States[i+1]=copyState(States[i])
States[i+1].monbox=-1# function climbdown,monkey climb down from the boxdefclimbdown(i):
routesave.insert(i,"Monkey climb down from the box")
States[i+1]=copyState(States[i])
States[i+1].monbox=-1# function reach,if the monkey,box,and banana are at the same place,the monkey reach bananadefreach(i):
routesave.insert(i,"Monkey reach the banana")# output the solution to the problemdefshowSolution(i):print("Result to problem:")for c inrange(i+1):print("Step %d : %s \n"%(c+1,routesave[c]))# perform next stepdefnextStep(i):#print("nextstep"+str(i))#print States[i].box
j=0if(i>=150):print("%s \n","steplength reached 150,have problem ")if(States[i].monbox ==1and States[i].monkey == States[0].banana and States[i].banana == States[0].banana and States[i].box == States[0].banana):print("sss")
showSolution(i)returnfor c inrange(i):if(States[c].monkey==States[i].monkey and States[c].box==States[i].box and States[c].banana==States[i].banana and States[c].monbox==States[i].monbox ):
showSolution(i)return
j=i+1if(States[i].box == States[i].monkey and States[i].box == States[i].banana):#print("1")if(States[i].monbox==-1):
climbonto(i)
reach(i +1)
nextStep(j)else:
reach(i +1)
nextStep(j)if(States[i].box == States[i].monkey and States[i].box != States[i].banana):#print("2")if(States[i].monbox ==-1):
movebox(States[i].banana, i)
nextStep(j)else:
climbdown(i)
nextStep(j)if(States[i].box != States[i].monkey and States[i].box == States[i].banana):#print("3")
monkeygoto(States[i].box, i)
nextStep(j)if(States[i].box != States[i].monkey and States[i].box != States