Advertisement
dzocesrce

[VI] Stars

Jan 21st, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.57 KB | None | 0 0
  1. def k_up_left(k_x,k_y,b_x,b_y):
  2.     if k_x-1>=0 and k_y+2<8 and (k_x-1!=b_x or k_y+2!=b_y):
  3.         return k_x-1, k_y+2
  4.     return k_x,k_y
  5. def k_up_right(k_x,k_y,b_x,b_y):
  6.     if k_x+1<8 and k_y+2<8 and (k_x+1!=b_x or k_y+2!=b_y):
  7.         return k_x+1, k_y+2
  8.     return k_x,k_y
  9. def k_right_down(k_x,k_y,b_x,b_y):
  10.     if k_x+2<8 and k_y-1>=0 and (k_x+2!=b_x or k_y-1!=b_y):
  11.         return k_x+2, k_y-1
  12.     return k_x,k_y
  13. def k_right_up(k_x,k_y,b_x,b_y):
  14.     if k_x+2<8 and k_y+1<8 and (k_x+2!=b_x or k_y+1!=b_y):
  15.         return k_x+2, k_y+1
  16.     return k_x,k_y
  17. def k_left_down(k_x,k_y,b_x,b_y):
  18.     if k_x-2>=0 and k_y-1>=0 and (k_x-2!=b_x or k_y-1!=b_y):
  19.         return k_x-2, k_y-1
  20.     return k_x,k_y
  21.  
  22. def k_left_up(k_x,k_y,b_x,b_y):
  23.     if k_x-2>=0 and k_y+1<8 and (k_x-2!=b_x or k_y+1!=b_y):
  24.         return k_x-2, k_y+1
  25.     return k_x,k_y
  26.  
  27. def k_down_left(k_x,k_y,b_x,b_y):
  28.     if k_x-1>0 and k_y-2>=0 and (k_x-1!=b_x or k_y-2!=b_y):
  29.         return k_x-1, k_y-2
  30.     return k_x,k_y
  31.  
  32. def k_down_right(k_x,k_y,b_x,b_y):
  33.     if k_x+1<8 and k_y-2>=0 and (k_x+1!=b_x or k_y-2!=b_y):
  34.         return k_x+1, k_y-2
  35.     return k_x,k_y
  36. def b_down_left(k_x,k_y,b_x,b_y):
  37.     if b_x-1>=0 and k_y-1>=0 and (k_x!=b_x-1 or k_y!=b_y-1):
  38.         return b_x-1,b_y-1
  39.     return b_x,b_y
  40.  
  41. def b_down_right(k_x,k_y,b_x,b_y):
  42.     if b_x+1<8 and k_y-1>=0 and (k_x!=b_x+1 or k_y!=b_y-1):
  43.         return b_x+1,b_y-1
  44.     return b_x,b_y
  45. def b_up_left(k_x,k_y,b_x,b_y):
  46.     if b_x-1>=0 and k_y+1<8 and (k_x!=b_x-1 or k_y!=b_y+1):
  47.         return b_x-1,b_y+1
  48.     return b_x,b_y
  49. def b_up_right(k_x,k_y,b_x,b_y):
  50.     if b_x+1<8 and k_y+1<8 and (k_x!=b_x+1 or k_y!=b_y+1):
  51.         return b_x+1,b_y+1
  52.     return b_x,b_y
  53. class Stars(Problem):
  54.     def __init__(self, initial, house=None):
  55.         super().__init__(initial, house)
  56.  
  57.     def goal_test(self, state):
  58.         stars= state[4]
  59.         return len(stars)==0
  60.  
  61.     def successor(self, state):
  62.         succ = {}
  63.         k_x= state[0]
  64.         k_y= state[1]
  65.         b_x= state[2]
  66.         b_y= state[3]
  67.         stars= state[4]
  68.  
  69.         new_k_x, new_k_y = k_up_right(k_x, k_y, b_x, b_y)
  70.         if new_k_x != k_x:
  71.             succ["Konj Gore Desno"] = (
  72.             new_k_x, new_k_y, b_x, b_y, tuple([s for s in stars if (s[0], s[1]) != (new_k_x, new_k_y)]))
  73.  
  74.         new_k_x, new_k_y = k_up_left(k_x, k_y, b_x, b_y)
  75.         if new_k_x != k_x:
  76.             succ["Konj Gore Levo"] = (
  77.             new_k_x, new_k_y, b_x, b_y, tuple([s for s in stars if (s[0], s[1]) != (new_k_x, new_k_y)]))
  78.  
  79.         new_k_x, new_k_y = k_right_up(k_x, k_y, b_x, b_y)
  80.         if new_k_x != k_x:
  81.             succ["Konj Desno Gore"] = (
  82.             new_k_x, new_k_y, b_x, b_y, tuple([s for s in stars if (s[0], s[1]) != (new_k_x, new_k_y)]))
  83.  
  84.         new_k_x, new_k_y = k_right_down(k_x, k_y, b_x, b_y)
  85.         if new_k_x != k_x:
  86.             succ["Konj Desno Dolu"] = (
  87.             new_k_x, new_k_y, b_x, b_y, tuple([s for s in stars if (s[0], s[1]) != (new_k_x, new_k_y)]))
  88.  
  89.         new_k_x, new_k_y = k_left_up(k_x, k_y, b_x, b_y)
  90.         if new_k_x != k_x:
  91.             succ["Konj Levo Gore"] = (
  92.             new_k_x, new_k_y, b_x, b_y, tuple([s for s in stars if (s[0], s[1]) != (new_k_x, new_k_y)]))
  93.  
  94.         new_k_x, new_k_y = k_left_down(k_x, k_y, b_x, b_y)
  95.         if new_k_x != k_x:
  96.             succ["Konj Levo Dolu"] = (
  97.             new_k_x, new_k_y, b_x, b_y, tuple([s for s in stars if (s[0], s[1]) != (new_k_x, new_k_y)]))
  98.  
  99.         new_k_x, new_k_y = k_down_left(k_x, k_y, b_x, b_y)
  100.         if new_k_x != k_x:
  101.             succ["Konj Dolu Levo"] = (
  102.             new_k_x, new_k_y, b_x, b_y, tuple([s for s in stars if (s[0], s[1]) != (new_k_x, new_k_y)]))
  103.  
  104.         new_k_x, new_k_y = k_down_right(k_x, k_y, b_x, b_y)
  105.         if new_k_x != k_x:
  106.             succ["Konj Dolu Desno"] = (
  107.             new_k_x, new_k_y, b_x, b_y, tuple([s for s in stars if (s[0], s[1]) != (new_k_x, new_k_y)]))
  108.  
  109.         new_b_x,new_b_y= b_up_left(k_x,k_y,b_x,b_y)
  110.         if new_b_x!=b_x:
  111.             succ["Lovec Gore Levo"] = (k_x,k_y,new_b_x,new_b_y,tuple([s for s in stars if (s[0],s[1])!=(new_b_x,new_b_y)]))
  112.  
  113.         new_b_x,new_b_y= b_up_right(k_x,k_y,b_x,b_y)
  114.         if new_b_x!=b_x:
  115.             succ["Lovec Gore Desno"] = (k_x,k_y,new_b_x,new_b_y,tuple([s for s in stars if (s[0],s[1])!=(new_b_x,new_b_y)]))
  116.  
  117.         new_b_x,new_b_y= b_down_left(k_x,k_y,b_x,b_y)
  118.         if new_b_x!=b_x:
  119.             succ["Lovec Dolu Levo"] = (k_x,k_y,new_b_x,new_b_y,tuple([s for s in stars if (s[0],s[1])!=(new_b_x,new_b_y)]))
  120.  
  121.         new_b_x, new_b_y = b_down_right(k_x, k_y, b_x, b_y)
  122.         if new_b_x != b_x:
  123.             succ["Lovec Dolu Desno"] = (k_x, k_y,new_b_x,new_b_y, tuple([s for s in stars if (s[0], s[1]) != (new_b_x, new_b_y)]))
  124.  
  125.         return succ
  126.     def actions(self, state):
  127.         return self.successor(state).keys()
  128.  
  129.     def result(self, state, action):
  130.         return self.successor(state)[action]
  131.  
  132.  
  133. if __name__ == '__main__':
  134.  
  135.     k_x,k_y = input().split(',')
  136.     k_x=int(k_x)
  137.     k_y=int(k_y)
  138.     b_x,b_y = input().split(',')
  139.     b_x=int(b_x)
  140.     b_y=int(b_y)
  141.     stars= []
  142.     n= int(input())
  143.     for i in range(0,n):
  144.         s_x,s_y = input().split(',')
  145.         s_x = int(s_x)
  146.         s_y = int(s_y)
  147.         s = []
  148.         s.append(s_x)
  149.         s.append(s_y)
  150.         stars.append(tuple(s))
  151.     stars=tuple(stars)
  152.     galaxies = ((1,1),(3,3),(5,7))
  153.     stars_problem = Stars((k_x,k_y,b_x,b_y,stars))
  154.  
  155.     answer= breadth_first_graph_search(stars_problem)
  156.  
  157.     if answer is None:
  158.         print("None")
  159.     else:
  160.         print(answer.solution())
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement