Advertisement
dzocesrce

[VI] Squares

Jan 21st, 2025 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. class Squares(Problem):
  2.     def __init__(self, initial, house):
  3.         super().__init__(initial, house)
  4.  
  5.     def goal_test(self, state):
  6.         return state == self.goal
  7.  
  8.     @staticmethod
  9.     def check_valid(state):
  10.         #print(state)
  11.         for x in state:
  12.             if x < 0 or x > 4:
  13.                 return False
  14.         for y in state:
  15.             if y < 0 or y > 4:
  16.                 return False
  17.         return True
  18.  
  19.     def successor(self, state):
  20.         succ = {}
  21.         s1=state[0]
  22.         s2=state[1]
  23.         s3=state[2]
  24.         s4=state[3]
  25.         s5=state[4]
  26.  
  27.         if self.check_valid((s1[0],s1[1]+1)):
  28.             succ["Pomesti kvadratche 1 gore"] = ((s1[0],s1[1]+1),s2,s3,s4,s5)
  29.         if self.check_valid((s1[0],s1[1]-1)):
  30.             succ["Pomesti kvadratche 1 dolu"] = ((s1[0],s1[1]-1),s2,s3,s4,s5)
  31.         if self.check_valid((s1[0]-1,s1[1])):
  32.             succ["Pomesti kvadratche 1 levo"] = ((s1[0]-1,s1[1]),s2,s3,s4,s5)
  33.         if self.check_valid((s1[0]+1,s1[1])):
  34.             succ["Pomesti kvadratche 1 desno"] = ((s1[0]+1,s1[1]),s2,s3,s4,s5)
  35.  
  36.         if self.check_valid((s2[0],s2[1]+1)):
  37.             succ["Pomesti kvadratche 2 gore"] = (s1,(s2[0],s2[1]+1),s3,s4,s5)
  38.         if self.check_valid((s2[0],s2[1]-1)):
  39.             succ["Pomesti kvadratche 2 dolu"] = (s1,(s2[0],s2[1]-1),s3,s4,s5)
  40.         if self.check_valid((s2[0]-1,s2[1])):
  41.             succ["Pomesti kvadratche 2 levo"] = (s1,(s2[0]-1,s2[1]),s3,s4,s5)
  42.         if self.check_valid((s2[0]+1,s2[1])):
  43.             succ["Pomesti kvadratche 2 desno"] = (s1,(s2[0]+1,s2[1]),s3,s4,s5)
  44.  
  45.         if self.check_valid((s3[0],s3[1]+1)):
  46.             succ["Pomesti kvadratche 3 gore"] = (s1,s2,(s3[0],s3[1]+1),s4,s5)
  47.         if self.check_valid((s3[0],s3[1]-1)):
  48.             succ["Pomesti kvadratche 3 dolu"] = (s1,s2,(s3[0],s3[1]-1),s4,s5)
  49.         if self.check_valid((s3[0]-1,s3[1])):
  50.             succ["Pomesti kvadratche 3 levo"] = (s1,s2,(s3[0]+1,s3[1]),s4,s5)
  51.         if self.check_valid((s3[0]+1,s3[1])):
  52.             succ["Pomesti kvadratche 3 desno"] = (s1,s2,(s3[0]-1,s3[1]),s4,s5)
  53.  
  54.         if self.check_valid((s4[0],s4[1]+1)):
  55.             succ["Pomesti kvadratche 4 gore"] = (s1,s2,s3,(s4[0],s4[1]+1),s5)
  56.         if self.check_valid((s4[0],s4[1]-1)):
  57.             succ["Pomesti kvadratche 4 dolu"] = (s1,s2,s3,(s4[0],s4[1]-1),s5)
  58.         if self.check_valid((s4[0]-1,s4[1])):
  59.             succ["Pomesti kvadratche 4 levo"] = (s1,s2,s3,(s4[0]-1,s4[1]),s5)
  60.         if self.check_valid((s4[0]+1,s4[1])):
  61.             succ["Pomesti kvadratche 4 desno"] = (s1,s2,s3,(s4[0]+1,s4[1]),s5)
  62.  
  63.         if self.check_valid((s5[0],s5[1]+1)):
  64.             succ["Pomesti kvadratche 5 gore"] = (s1,s2,s3,s4,(s5[0],s5[1]+1))
  65.         if self.check_valid((s5[0],s5[1]-1)):
  66.             succ["Pomesti kvadratche 5 dolu"] = (s1,s2,s3,s4,(s5[0],s5[1]-1))
  67.         if self.check_valid((s5[0]-1,s5[1])):
  68.             succ["Pomesti kvadratche 5 levo"] = (s1,s2,s3,s4,(s5[0]-1,s5[1]))
  69.         if self.check_valid((s5[0]+1,s5[1])):
  70.             succ["Pomesti kvadratche 5 desno"] = (s1,s2,s3,s4,(s5[0]+1,s5[1]))
  71.  
  72.         return succ
  73.     def actions(self, state):
  74.         return self.successor(state).keys()
  75.  
  76.     def result(self, state, action):
  77.         return self.successor(state)[action]
  78.  
  79.  
  80. if __name__ == '__main__':
  81.     # ((x1, y1), (x2, y2), (x3, y3), (x4, y4), (x5, y5))
  82.     initial_state = tuple()
  83.     for _ in range(5):
  84.         initial_state += (tuple(map(int, input().split(','))), )
  85.  
  86.     goal_state = ((0, 4), (1, 3), (2, 2), (3, 1), (4, 0))
  87.  
  88.     squares = Squares(initial_state, goal_state)
  89.  
  90.     answer = breadth_first_graph_search(squares)
  91.  
  92.     if answer is None:
  93.         print ("None")
  94.     else:
  95.         print (answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement