Advertisement
kastielspb

Checkio| Open Labyrinth

Nov 28th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. # Open Labyrinth
  2. from collections import deque
  3.  
  4. class DepthLabyrinth:
  5.     moves = {
  6.         'N': [-1,0],
  7.         'S': [1,0],
  8.         'W': [0,-1],
  9.         'E': [0,1]
  10.     }
  11.     histories = []
  12.     def __init__(self, graph, start, end):
  13.         self.graph = graph
  14.         self.start = start
  15.         self.end = end
  16.         self.find_path(self.start, [self.start])
  17.  
  18.     @property
  19.     def history(self):
  20.         return min(self.histories, key=len)
  21.  
  22.     def find_path(self, curr, path):
  23.         for next in self.get_next_steps(curr, path):
  24.             _path = path.copy()
  25.             _path.append(next)
  26.             if next == self.end:
  27.                 self.histories.append(_path)
  28.             self.find_path(next, _path)
  29.  
  30.  
  31.     def get_next_steps(self, curr, path):
  32.         for move in self.moves.values():
  33.             next = [curr[0] + move[0], curr[1] + move[1]]
  34.             if next not in path and self.is_may_step(next):
  35.                 # print(f'path: {path}')
  36.                 yield next
  37.  
  38.     def is_may_step(self, step):
  39.         val = self.graph[step[0]][step[1]]
  40.         return True if val == 0 else False
  41.  
  42.     def get_path(self):
  43.         return ''.join(self.read_history())
  44.  
  45.     def read_history(self):
  46.         directions = []
  47.         m_keys = list(self.moves.keys())
  48.         m_vals = list(self.moves.values())
  49.  
  50.         for i in range(len(self.history) - 1):
  51.             step = [
  52.                 self.history[i+1][0] - self.history[i][0],
  53.                 self.history[i+1][1] - self.history[i][1],
  54.             ]
  55.             directions.append(
  56.                 m_keys[
  57.                     m_vals.index(step)
  58.                 ]
  59.             )
  60.         return directions
  61.  
  62. class BestFirstSearchLabyrinth:
  63.     moves = {
  64.         'N': [-1,0],
  65.         'S': [1,0],
  66.         'W': [0,-1],
  67.         'E': [0,1]
  68.     }
  69.     history = []
  70.     def __init__(self, graph, start, end):
  71.         self.graph = graph
  72.         self.start = start
  73.         self.end = end
  74.         self.turn = deque([[self.start, [self.start]]])
  75.         self.find_path()
  76.  
  77.     def find_path(self):
  78.         while len(self.turn):
  79.             curr, path = self.turn.popleft()
  80.             for next in self.get_next_steps(curr, path):
  81.                 _path = path.copy()
  82.                 _path.append(next)
  83.                 if next == self.end:
  84.                     self.history = _path
  85.                     return
  86.                 self.turn.append([next, _path])
  87.  
  88.  
  89.     def get_next_steps(self, curr, path):
  90.         for move in self.moves.values():
  91.             next = [curr[0] + move[0], curr[1] + move[1]]
  92.             if next not in path and self.is_may_step(next):
  93.                 yield next
  94.  
  95.     def is_may_step(self, step):
  96.         val = self.graph[step[0]][step[1]]
  97.         return True if val == 0 else False
  98.  
  99.     def get_path(self):
  100.         return ''.join(self.read_history())
  101.  
  102.     def read_history(self):
  103.         directions = []
  104.         m_keys = list(self.moves.keys())
  105.         m_vals = list(self.moves.values())
  106.  
  107.         for i in range(len(self.history) - 1):
  108.             step = [
  109.                 self.history[i+1][0] - self.history[i][0],
  110.                 self.history[i+1][1] - self.history[i][1],
  111.             ]
  112.             directions.append(
  113.                 m_keys[
  114.                     m_vals.index(step)
  115.                 ]
  116.             )
  117.         return directions
  118.  
  119. for i, LabClass in enumerate(
  120.     [
  121.         DepthLabyrinth,
  122.         BestFirstSearchLabyrinth
  123.     ],
  124.     start=1
  125. ):
  126.     labyrinth = LabClass(
  127.         [
  128.             [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  129.             [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  130.             [1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
  131.             [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  132.             [1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1],
  133.             [1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
  134.             [1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1],
  135.             [1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1],
  136.             [1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1],
  137.             [1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1],
  138.             [1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1],
  139.             [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  140.         ],
  141.         [1, 1],
  142.         [10, 10]
  143.     )
  144.     answer = labyrinth.get_path()
  145.     print(f'answer{i}: "{answer}"')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement