Advertisement
Fenny_Theo

Untitled

Jul 25th, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. class Task:
  2. def __init__(self,n,top,From,To,Middle):
  3. self.n = n
  4. self.top = top
  5. self.From = From
  6. self.To = To
  7. self.Middle = Middle
  8.  
  9. def printPros(self):
  10. print(self.From, "--->", self.To)
  11.  
  12. class ListStack:
  13. def __init__(self):
  14. self.q =list()
  15.  
  16. def push(self,element):
  17. self.q.append(element)
  18. return self
  19.  
  20. def pop(self):
  21. return self.q.pop()
  22.  
  23. def isEmpty(self):
  24. return len(self.q)==0
  25.  
  26. def hanoi(n):
  27. tasks = ListStack()
  28.  
  29. def push(n,i,f,t,m):
  30. t = Task(n,i,f,t,m)
  31. tasks.push(t)
  32.  
  33.  
  34. push(n,1,'A','C','B')#same order as From To Middle
  35. while not tasks.isEmpty():#remain to operate
  36. task = tasks.pop()
  37.  
  38. if task.n==1:#to the initial situation(base)
  39. task.printPros()
  40. else:
  41. push(task.n-1,1,task.Middle,task.To,task.From)#in middle
  42. push(1,task.n,task.From,task.To,task.To)# the botton one
  43. push(task.n-1,1,task.From,task.Middle,task.To)#in From
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement