Advertisement
Guest User

Untitled

a guest
Feb 14th, 2021
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. def all_false(x):
  2.     for check in x:
  3.         if check:
  4.             return False
  5.     return True
  6.  
  7.  
  8. def char_replace(string, x, y):
  9.     temp = list(string[x])
  10.     temp[y] = "#"
  11.     temp = "".join(temp)
  12.     maze[x] = temp
  13.  
  14.  
  15. def kate(x):
  16.     for row in x:
  17.         if "k" in row:
  18.             return x.index(row), row.index("k")
  19.  
  20.  
  21. def way_out_path(x):
  22.     way_out_map = []
  23.     for a, row in enumerate(x):
  24.         for b, column in enumerate(row):
  25.             if column == " ":
  26.                 way_out_map.append([a, b])
  27.     return way_out_map
  28.  
  29.  
  30. def way_out(x, y, way_out_map):
  31.     move = 0
  32.     while True:
  33.         found_a_way = [[x, y+1] in way_out_map, [x, y-1] in way_out_map, [x+1, y] in way_out_map, [x-1, y] in way_out_map]
  34.         found_a_way_value = [[x, y+1], [x, y-1], [x+1, y], [x-1, y]]
  35.  
  36.         if x == maze_rows-1 or y == len(maze[0])-1 or y == 0:
  37.             return f"Kate got out in {move+1} moves"
  38.  
  39.         if not all_false(found_a_way):
  40.             x, y = found_a_way_value[found_a_way.index(True)]
  41.             char_replace(maze, x, y)
  42.             way_out_map.remove(found_a_way_value[found_a_way.index(True)])
  43.             move += 1
  44.  
  45.         else:
  46.             return "Kate cannot get out"
  47.  
  48.  
  49. maze_rows = int(input())
  50. maze = [input() for x in range(maze_rows)]
  51. kate_row, kate_column = kate(maze)
  52. map_to_freedom = way_out_path(maze)
  53. print(way_out(kate_row, kate_column, map_to_freedom))
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement