Advertisement
Nenogzar

03. Kate's Way Out

Apr 9th, 2024
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.45 KB | None | 0 0
  1. rows = int(input())
  2.  
  3. matrix, moves, starting_row, starting_col = [], [], 0, 0
  4.  
  5. for row in range(rows):
  6.     matrix.append(list(input()))
  7.     if "k" in matrix[row]:
  8.         starting_row, starting_col = row, matrix[row].index("k")
  9.  
  10. cols = len(matrix[0])
  11.  
  12.  
  13. def find_way_out(row, col, step):
  14.     if not 0 <= row < rows or not 0 <= col < cols or matrix[row][col] in "#*":
  15.         return
  16.  
  17.     step += 1
  18.     if row == 0 or row == rows - 1 or col == 0 or col == cols - 1:
  19.         moves.append(step)
  20.  
  21.     matrix[row][col] = "*"
  22.     [find_way_out(row, col, step) for row, col in
  23.      ((row, col + 1), (row, col - 1), (row + 1, col), (row - 1, col))]
  24.     matrix[row][col] = " "
  25.     step -= 1
  26.  
  27.  
  28. find_way_out(starting_row, starting_col, 0)
  29. if moves:
  30.     print(f"Kate got out in {max(moves)} moves")
  31. else:
  32.     print("Kate cannot get out")
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. #
  40. #
  41. # def correct_lab_bounds(row, col):
  42. #     return row < 0 or col < 0 or row >= len(lab) or col >= len(lab[0])
  43. #
  44. #
  45. #
  46. # def check_wall(row, col):
  47. #     return lab[row][col] in "#v"
  48. #
  49. #
  50. #
  51. # def find_exit(row, col):
  52. #     return row == 0 or row == len(lab) - 1 or col == 0 or col == len(lab[0])
  53. #
  54. #
  55. #
  56. # def find_starting_point():
  57. #     for pos_row, row in enumerate(lab):
  58. #         for pos_col, col in enumerate(row):
  59. #             if col == "k":
  60. #                 return pos_row, pos_col
  61. #
  62. #
  63. # def find_the_lab_path(row, col, lab):
  64. #     if correct_lab_bounds(row, col) or check_wall(row, col):
  65. #         return
  66. #
  67. #     path_steps.append(1)
  68. #
  69. #     if find_exit(row, col):
  70. #         max_len_path.append(sum(path_steps))
  71. #
  72. #     lab[row][col] = "v"
  73. #     find_the_lab_path(row, col + 1, lab)  # check right
  74. #     find_the_lab_path(row, col - 1, lab)  # check left
  75. #     find_the_lab_path(row + 1, col, lab)  # check up
  76. #     find_the_lab_path(row - 1, col, lab)  # check down
  77. #     lab[row][col] = " "
  78. #
  79. #     path_steps.pop()
  80. #
  81. #
  82. # row = int(input())
  83. # lab = []
  84. # path_steps = []
  85. # max_len_path = []
  86. # for _ in range(row):
  87. #     lab.append(list(input()))
  88. # col = len(lab[0])
  89. # start_row, start_col = find_starting_point()
  90. #
  91. # find_the_lab_path(start_row, start_col, lab)
  92. #
  93. # if max_len_path:
  94. #     print(f"Kate got out in {max(max_len_path)} moves")
  95. # else:
  96. #     print("Kate cannot get out")
  97. #
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. # def find_position(maze):
  107. #     position = []
  108. #     for row in range(len(maze)):
  109. #         for el in maze[row]:
  110. #             if el == 'k':
  111. #                 position.append(row)
  112. #                 position.append(maze[row].find('k'))
  113. #                 return position
  114. #
  115. #
  116. # def next_free_spot(maze):
  117. #     free_spots = []
  118. #
  119. #     for row in range(len(maze)):
  120. #         for el in range(len(maze[row])):
  121. #             tmp = []
  122. #             if maze[row][el] == ' ':
  123. #                 tmp.append(row)
  124. #                 tmp.append(el)
  125. #                 free_spots.insert(0, tmp)
  126. #
  127. #     return free_spots
  128. #
  129. #
  130. # def find_path(position, next_free, maze):
  131. #     is_blocked = True
  132. #     step = 0
  133. #     moves = 0
  134. #
  135. #     while step < len(next_free):
  136. #         x1 = next_free[step][0]
  137. #         x2 = next_free[step][1]
  138. #         temp = []
  139. #         temp.append(x1)
  140. #         temp.append(x2)
  141. # # moving left
  142. #         if temp[0] == position[0] and position[1] - temp[1] == 1:
  143. #             position = temp
  144. #             moves += 1
  145. #             next_free.pop(step)
  146. #             step = 0
  147. # # moving right
  148. #         elif temp[0] == position[0] and temp[1] - position[1] == 1:
  149. #             position = temp
  150. #             moves += 1
  151. #             next_free.pop(step)
  152. #             step = 0
  153. # # moving down
  154. #         elif temp[0] - position[0] == 1 and position[1] == temp[1]:
  155. #             position = temp
  156. #             moves += 1
  157. #             next_free.pop(step)
  158. #             step = 0
  159. # # moving up
  160. #         elif position[0] - temp[0] == 1 and position[1] == temp[1]:
  161. #             position = temp
  162. #             moves += 1
  163. #             next_free.pop(step)
  164. #             step = 0
  165. #
  166. #
  167. #         else:
  168. #
  169. #             step += 1
  170. #
  171. #     if position[0] == 0 or position[0] == (len(maze) -1) or position[1] == 0 or position[1] == len(maze[0]):
  172. #         return f'Kate got out in {moves + 1} moves'
  173. #     return f'Kate cannot get out'
  174. #
  175. # m_rows = int(input())
  176. # maze = []
  177. # moves = 0
  178. # free_space = True
  179. # for row in range(m_rows):
  180. #     maze.append(input())
  181. # position = find_position(maze)
  182. # next_free = next_free_spot(maze)
  183. # movement = find_path(position, next_free, maze)
  184. # print(movement)
  185. #
  186.  
  187.  
  188.  
  189. #
  190. # import queue
  191. #
  192. # maze_rows = int(input())
  193. # maze = []
  194. # way_out = []
  195. # for _ in range(maze_rows):
  196. #     maze.append([x for x in input()])
  197. #
  198. #
  199. # def find_start_point(maze, point_start):
  200. #     for i, row in enumerate(maze):
  201. #         for j, value in enumerate(row):
  202. #             if point_start == value:
  203. #                 return i, j
  204. #
  205. #     return None
  206. #
  207. #
  208. # def find_end_point(maze, point_end):
  209. #     for i, row in enumerate(maze):
  210. #         for j, value in enumerate(row):
  211. #             if (i == 0 or i == maze_rows - 1) and point_end == value:
  212. #                 return i, j
  213. #
  214. #     return None
  215. #
  216. #
  217. # def find_path(maze):
  218. #     global way_out
  219. #     start = "k"
  220. #     end = find_end_point(maze, " ")
  221. #     start_pos = find_start_point(maze, start)
  222. #     q = queue.Queue()
  223. #     q.put((start_pos, [start_pos]))
  224. #     visited = set()
  225. #
  226. #     while not q.empty():
  227. #         current_pos, path = q.get()
  228. #         row, col = current_pos
  229. #         if current_pos == end:
  230. #             way_out = path
  231. #             return path
  232. #
  233. #         neighbors = find_neighbors(maze, row, col)
  234. #         for neighbor in neighbors:
  235. #             if neighbor in visited:
  236. #                 continue
  237. #
  238. #             r, c = neighbor
  239. #             if maze[r][c] == "#":
  240. #                 continue
  241. #
  242. #             new_path = path + [neighbor]
  243. #             q.put((neighbor, new_path))
  244. #             visited.add(neighbor)
  245. #
  246. #
  247. # def find_neighbors(maze, row, col):
  248. #     neighbors = []
  249. #     if row > 0:  # UP
  250. #         neighbors.append((row - 1, col))
  251. #     if row + 1 < len(maze):  # DOWN
  252. #         neighbors.append((row + 1, col))
  253. #     if col > 0:  # LEFT
  254. #         neighbors.append((row, col - 1))
  255. #     if col + 1 < len(maze[0]):  # RIGHT
  256. #         neighbors.append((row, col + 1))
  257. #
  258. #     return neighbors
  259. #
  260. #
  261. # find_path(maze)
  262. # if way_out:
  263. #     print(f"Kate got out in {len(way_out)} moves")
  264. # else:
  265. #     print("Kate cannot get out")
  266. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement