From d9b17b866ef3acd8b10d7b638bc0c65f7bd2c226 Mon Sep 17 00:00:00 2001 From: Subhan Khan <48542811+Subhankhan41@users.noreply.github.com> Date: Fri, 12 Apr 2024 13:47:38 +1000 Subject: [PATCH] solution.py --- 12_Route_Planner/solution.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/12_Route_Planner/solution.py b/12_Route_Planner/solution.py index 642044d..154a7af 100644 --- a/12_Route_Planner/solution.py +++ b/12_Route_Planner/solution.py @@ -1,12 +1,30 @@ def route_exists(from_row, from_column, to_row, to_column, map_matrix): - pass + rows = len(map_matrix) + cols = len(map_matrix[0]) + stack = [(from_row, from_column)] + visited = set() + + while stack: + current = stack.pop() + if current == (to_row, to_column): + return True + visited.add(current) + row, col = current + neighbors = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)] + for neighbor in neighbors: + n_row, n_col = neighbor + if 0 <= n_row < rows and 0 <= n_col < cols and map_matrix[n_row][n_col] and neighbor not in visited: + stack.append(neighbor) + + return False if __name__ == '__main__': map_matrix = [ [True, False, False], [True, True, False], [False, True, True] - ]; + ] print(route_exists(0, 0, 2, 2, map_matrix)) +