Advertisement
Guest User

OdProfIvan

a guest
Jun 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. from utils import *
  2. from uninformed_search import *
  3.  
  4.  
  5.  
  6. class HanoiTowers(Problem):
  7.  
  8.     def __init__(self,initial,goal,n):
  9.         Problem.__init__(self,initial,goal)
  10.         self.initial=initial
  11.         self.goal=goal
  12.         self.n=n
  13.  
  14.     def goal_test(self, state):
  15.         return state == self.goal
  16.  
  17.     def successor(self, state):
  18.         rez = dict()
  19.         ppp = 0
  20.  
  21.  
  22.         for i in range(0,len(state)):
  23.             #print("NOVA so state: "+str(state))
  24.             pillar = state[i]
  25.             if len(pillar)>0:
  26.                 for j in range(0,len(state)):
  27.                     destPil=state[j]
  28.                     if i==j:
  29.                         ppp = 0
  30.                     else:
  31.                         plocka = pillar[len(pillar) - 1]
  32.  
  33.                         novPoceten = pillar[:len(pillar)-1]
  34.                         novKraenTMP = list(destPil)
  35.                         novKraenTMP.append(plocka)
  36.                         novKraen = tuple(novKraenTMP)
  37.  
  38.                         novStateTMP = list()
  39.                         for k in range(0,len(state)):
  40.                             if k==i:
  41.                                 novStateTMP.append(novPoceten)
  42.                             elif k==j:
  43.                                 novStateTMP.append(novKraen)
  44.                             else:
  45.                                 novStateTMP.append(state[k])
  46.  
  47.                         novState = tuple(novStateTMP)
  48.                         # print(novState)
  49.  
  50.                         if self.validityCheck(novState):
  51.                             #print(novState)
  52.                             akcija = "MOVE TOP BLOCK FROM PILLAR "+str(i+1)+" TO PILLAR "+str(j+1)
  53.                             #print(akcija)
  54.                             rez[akcija]=novState
  55.                         else:
  56.                             #print("ne moze ova")
  57.                             ppp=0
  58.  
  59.         return rez
  60.  
  61.  
  62.     def validityCheck(self,state):
  63.  
  64.         for pillar in state:       # bi trebalo da vrti 3 pati
  65.             for i in range(0,len(pillar)-1):
  66.                 if pillar[i]<=pillar[i+1]:
  67.                     return False
  68.         return True
  69.  
  70.  
  71.  
  72. if __name__=="__main__":
  73.  
  74.     # inState = ((3,2,1),(),())
  75.     # glState = ((),(),(3,2,1))
  76.  
  77.     s = input()
  78.     initial_towers = tuple([tuple(map(int, x.split(','))) if x != '' else () for x in s.split(';')])
  79.     s = input()
  80.     goal_towers = tuple([tuple(map(int, x.split(','))) if x != '' else () for x in s.split(';')])
  81.  
  82.     #problem = HanoiTowers(inState,glState,3)
  83.  
  84.     #problem.successor(inState)
  85.  
  86.     n=0
  87.     for tow in initial_towers:
  88.         if len(tow)>0:
  89.             n=len(tow)
  90.  
  91.     problem = HanoiTowers(initial_towers,goal_towers,n)
  92.  
  93.     ans = breadth_first_graph_search(problem)
  94.     #print(ans.solve())
  95.     print("Number of steps required: "+len(ans))
  96.     print("Solution: ")
  97.     print(ans.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement