Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Task:
- def __init__(self,n,top,From,To,Middle):
- self.n = n
- self.top = top
- self.From = From
- self.To = To
- self.Middle = Middle
- def printPros(self):
- print(self.From, "--->", self.To)
- class ListStack:
- def __init__(self):
- self.q =list()
- def push(self,element):
- self.q.append(element)
- return self
- def pop(self):
- return self.q.pop()
- def isEmpty(self):
- return len(self.q)==0
- def hanoi(n):
- tasks = ListStack()
- def push(n,i,f,t,m):
- t = Task(n,i,f,t,m)
- tasks.push(t)
- push(n,1,'A','C','B')#same order as From To Middle
- while not tasks.isEmpty():#remain to operate
- task = tasks.pop()
- if task.n==1:#to the initial situation(base)
- task.printPros()
- else:
- push(task.n-1,1,task.Middle,task.To,task.From)#in middle
- push(1,task.n,task.From,task.To,task.To)# the botton one
- push(task.n-1,1,task.From,task.Middle,task.To)#in From
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement