Advertisement
dzocesrce

[VI] Ghost on Skates

Jan 22nd, 2025
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. class GhostOnSkates(Problem):
  2.     def __init__(self, initial, walls, n, goal=None):
  3.         super().__init__(initial, goal)
  4.         self.walls = walls
  5.         self.n = n
  6.  
  7.     def actions(self, state):
  8.         return self.successor(state).keys()
  9.  
  10.     def result(self, state, action):
  11.         return self.successor(state)[action]
  12.  
  13.     def goal_test(self, state):
  14.         return state == self.goal
  15.  
  16.     @staticmethod
  17.     def check_valid(state, walls, n):
  18.         if state in tuple(walls) or state[0]>n-1 or state[1]>n-1:
  19.             return False
  20.         return True
  21.  
  22.     def successor(self, state):
  23.         successors = dict()
  24.  
  25.         ghost_pos= state
  26.         holes= self.walls
  27.         n= self.n
  28.  
  29.         new_x= ghost_pos[0]
  30.         new_y= ghost_pos[1]+1
  31.         new_ghost_pos= []
  32.         new_ghost_pos.append(new_x)
  33.         new_ghost_pos.append(new_y)
  34.  
  35.         if self.check_valid(tuple(new_ghost_pos),holes,n):
  36.             successors["Gore 1"] = (tuple(new_ghost_pos))
  37.  
  38.         new_x= ghost_pos[0]
  39.         new_y= ghost_pos[1]+2
  40.         new_ghost_pos= []
  41.         new_ghost_pos.append(new_x)
  42.         new_ghost_pos.append(new_y)
  43.  
  44.         if self.check_valid(tuple(new_ghost_pos),holes,n):
  45.             successors["Gore 2"] = (tuple(new_ghost_pos))
  46.  
  47.         new_x= ghost_pos[0]
  48.         new_y= ghost_pos[1]+3
  49.         new_ghost_pos= []
  50.         new_ghost_pos.append(new_x)
  51.         new_ghost_pos.append(new_y)
  52.  
  53.         if self.check_valid(tuple(new_ghost_pos),holes,n):
  54.             successors["Gore 3"] = (tuple(new_ghost_pos))
  55.  
  56.         new_x= ghost_pos[0]+1
  57.         new_y= ghost_pos[1]
  58.         new_ghost_pos= []
  59.         new_ghost_pos.append(new_x)
  60.         new_ghost_pos.append(new_y)
  61.  
  62.         if self.check_valid(tuple(new_ghost_pos),holes,n):
  63.             successors["Desno 1"] = (tuple(new_ghost_pos))
  64.  
  65.  
  66.         new_x= ghost_pos[0]+2
  67.         new_y= ghost_pos[1]
  68.         new_ghost_pos= []
  69.         new_ghost_pos.append(new_x)
  70.         new_ghost_pos.append(new_y)
  71.  
  72.         if self.check_valid(tuple(new_ghost_pos),holes,n):
  73.             successors["Desno 2"] = (tuple(new_ghost_pos))
  74.  
  75.         new_x= ghost_pos[0]+3
  76.         new_y= ghost_pos[1]
  77.         new_ghost_pos= []
  78.         new_ghost_pos.append(new_x)
  79.         new_ghost_pos.append(new_y)
  80.  
  81.         if self.check_valid(tuple(new_ghost_pos),holes,n):
  82.             successors["Desno 3"] = (tuple(new_ghost_pos))
  83.  
  84.         return successors
  85.  
  86.     def h(self, node):
  87.         state= node.state
  88.         ghost_pos= state
  89.         goal_pos= self.goal
  90.         #print(node.state)
  91.         return (abs(ghost_pos[0]-goal_pos[0]) + abs(ghost_pos[1]-goal_pos[1]))/3
  92.  
  93.  
  94. if __name__ == '__main__':
  95.     n = int(input())
  96.     ghost_pos = (0, 0)
  97.     goal_pos = (n - 1, n - 1)
  98.  
  99.     num_holes = int(input())
  100.     holes = list()
  101.     for _ in range(num_holes):
  102.         holes.append(tuple(map(int, input().split(','))))
  103.  
  104.     problem = GhostOnSkates(ghost_pos, holes, n, goal_pos)
  105.  
  106.     answer= astar_search(problem)
  107.  
  108.     if answer is None:
  109.         print("None")
  110.     else:
  111.         print(answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement