Advertisement
dzocesrce

[VI] Labyrinth

Apr 2nd, 2024
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. #lab 2 informed search task 2
  2.  
  3. from searching_framework import *
  4.  
  5.  
  6. class Labirynth(Problem):
  7.  
  8.     def __init__(self, initial, goal):
  9.         super().__init__(initial, goal)
  10.  
  11.     def goal_test(self, state):
  12.         g = self.goal
  13.         return state[0][0] == g[0] and state[0][1] == g[1]
  14.  
  15.     def successor(self, state):
  16.         successors = dict()
  17.  
  18.         man_x = state[0][0]
  19.         man_y = state[0][1]
  20.  
  21.         grid_size = state[1]
  22.         obsticles = state[2]
  23.  
  24.         if man_y < grid_size - 1 and (man_x, man_y + 1) not in obsticles:
  25.             successors['Gore'] = ((man_x, man_y + 1), grid_size, obsticles)
  26.  
  27.         if man_x > 0 and (man_x - 1, man_y) not in obsticles:
  28.             successors['Levo'] = ((man_x - 1, man_y), grid_size, obsticles)
  29.  
  30.         if man_y > 0 and (man_x, man_y - 1) not in obsticles:
  31.             successors['Dolu'] = ((man_x, man_y - 1), grid_size, obsticles)
  32.  
  33.         if man_x < grid_size - 2 and (man_x + 1, man_y) not in obsticles and (man_x + 2, man_y) not in obsticles:
  34.             successors['Desno 2'] = ((man_x + 2, man_y), grid_size, obsticles)
  35.  
  36.         if man_x < grid_size - 3 and (man_x + 1, man_y) not in obsticles and (man_x + 2, man_y) not in obsticles and (
  37.         man_x + 3, man_y) not in obsticles:
  38.             successors['Desno 3'] = ((man_x + 3, man_y), grid_size, obsticles)
  39.  
  40.  
  41.         return successors
  42.  
  43.     def actions(self, state):
  44.         return self.successor(state).keys()
  45.  
  46.     def h(self, node):
  47.         man_x = node.state[0][0]
  48.         man_y = node.state[0][1]
  49.         house_x = self.goal[0]
  50.         house_y = self.goal[1]
  51.  
  52.         return abs(house_y - man_y)/4 + abs(house_x-man_x)/4
  53.  
  54.     def result(self, state, action):
  55.         possible = self.successor(state)
  56.         return possible[action]
  57.  
  58.  
  59. if __name__ == '__main__':
  60.  
  61.     n = int(input())
  62.     number_of_blocks = int(input())
  63.     obsticles = []
  64.     i = 0
  65.     while (number_of_blocks > i):
  66.         temp = input().split(',')
  67.         obsticles.append((int(temp[0]), int(temp[1])))
  68.         i += 1
  69.     man_coordinates = input().split(',')
  70.     man_x = int(man_coordinates[0])
  71.     man_y = int(man_coordinates[1])
  72.     man = (man_x, man_y)
  73.     house_coordinates = input().split(',')
  74.     house_x = int(house_coordinates[0])
  75.     house_y = int(house_coordinates[1])
  76.     house = (house_x, house_y)
  77.  
  78.     labirynth = Labirynth((man, n, tuple(obsticles)), house)
  79.  
  80.     answer = astar_search(labirynth)
  81.     if answer is None:
  82.         print("NONE")
  83.     else:
  84.         print(answer.solution())
  85.  
  86.  
  87.  
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement