From 6b4c90b41a0c949d7d6e1a4fc67f35a9b6746dc8 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Mon, 25 Sep 2023 01:35:36 +0500 Subject: [PATCH 01/18] [UPDATE] #9066 Rate_in_Maze.py --- backtracking/rat_in_maze.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 7bde886dd558..74069331e96d 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -4,13 +4,15 @@ def solve_maze(maze: list[list[int]]) -> bool: """ This method solves the "rat in maze" problem. - In this problem we have some n by n matrix, a start point and an end point. - We want to go from the start to the end. In this matrix zeroes represent walls - and ones paths we can use. Parameters : maze(2D matrix) : maze Returns: Return: True if the maze has a solution or False if it does not. + Description: + This method navigates through a maze represented as an n by n matrix, starting from a specified source cell + (default: top-left corner) and aiming to reach a destination cell (default: bottom-right corner). + The maze consists of walls (0s) and open paths (1s). + By providing custom row and column values, the source and destination cells can be adjusted. >>> maze = [[0, 1, 0, 1, 1], ... [0, 0, 0, 0, 0], ... [1, 0, 1, 0, 1], @@ -24,6 +26,9 @@ def solve_maze(maze: list[list[int]]) -> bool: [0, 0, 0, 0, 1] True + Note: + In the output maze, the ones (1s) represent one of the possible paths from the source to the destination. + >>> maze = [[0, 1, 0, 1, 1], ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 1], @@ -59,10 +64,14 @@ def solve_maze(maze: list[list[int]]) -> bool: No solution exists! False """ - size = len(maze) # We need to create solution object to save path. + size = len(maze) + source_row=0 + source_column=0 + destination_row=size-1 + destination_column=size-1 solutions = [[0 for _ in range(size)] for _ in range(size)] - solved = run_maze(maze, 0, 0, solutions) + solved = run_maze(maze, source_row, source_column,destination_row,destination_column, solutions) if solved: print("\n".join(str(row) for row in solutions)) else: @@ -70,7 +79,7 @@ def solve_maze(maze: list[list[int]]) -> bool: return solved -def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]]) -> bool: +def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,destination_column:int, solutions: list[list[int]]) -> bool: """ This method is recursive starting from (i, j) and going in one of four directions: up, down, left, right. @@ -84,7 +93,7 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]]) """ size = len(maze) # Final check point. - if i == j == (size - 1): + if i == destination_row and j == destination_column: solutions[i][j] = 1 return True @@ -100,10 +109,10 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]]) # check for directions if ( - run_maze(maze, i + 1, j, solutions) - or run_maze(maze, i, j + 1, solutions) - or run_maze(maze, i - 1, j, solutions) - or run_maze(maze, i, j - 1, solutions) + run_maze(maze, i + 1, j,destination_row,destination_column, solutions) + or run_maze(maze, i, j + 1,destination_row,destination_column, solutions) + or run_maze(maze, i - 1, j,destination_row,destination_column, solutions) + or run_maze(maze, i, j - 1,destination_row,destination_column, solutions) ): return True @@ -112,6 +121,7 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]]) return False + if __name__ == "__main__": import doctest From e90e0a4ed56fe9b7e99ba7356cba3b1eb113cf77 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Mon, 25 Sep 2023 01:50:52 +0500 Subject: [PATCH 02/18] [UPDATED] #9066 Rate_in_Maze.py --- backtracking/rat_in_maze.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 74069331e96d..35edd828dfc3 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -9,8 +9,9 @@ def solve_maze(maze: list[list[int]]) -> bool: Returns: Return: True if the maze has a solution or False if it does not. Description: - This method navigates through a maze represented as an n by n matrix, starting from a specified source cell - (default: top-left corner) and aiming to reach a destination cell (default: bottom-right corner). + This method navigates through a maze represented as an n by n matrix, + starting from a specified source cell (default: top-left corner) and + aiming to reach a destination cell (default: bottom-right corner). The maze consists of walls (0s) and open paths (1s). By providing custom row and column values, the source and destination cells can be adjusted. >>> maze = [[0, 1, 0, 1, 1], @@ -27,7 +28,8 @@ def solve_maze(maze: list[list[int]]) -> bool: True Note: - In the output maze, the ones (1s) represent one of the possible paths from the source to the destination. + In the output maze, the ones (1s) represent one of the possible + paths from the source to the destination. >>> maze = [[0, 1, 0, 1, 1], ... [0, 0, 0, 0, 0], @@ -71,7 +73,8 @@ def solve_maze(maze: list[list[int]]) -> bool: destination_row=size-1 destination_column=size-1 solutions = [[0 for _ in range(size)] for _ in range(size)] - solved = run_maze(maze, source_row, source_column,destination_row,destination_column, solutions) + solved = run_maze(maze, source_row, source_column,destination_row, + destination_column, solutions) if solved: print("\n".join(str(row) for row in solutions)) else: @@ -79,7 +82,8 @@ def solve_maze(maze: list[list[int]]) -> bool: return solved -def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,destination_column:int, solutions: list[list[int]]) -> bool: +def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int, + destination_column:int, solutions: list[list[int]]) -> bool: """ This method is recursive starting from (i, j) and going in one of four directions: up, down, left, right. @@ -109,10 +113,14 @@ def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int,destinati # check for directions if ( - run_maze(maze, i + 1, j,destination_row,destination_column, solutions) - or run_maze(maze, i, j + 1,destination_row,destination_column, solutions) - or run_maze(maze, i - 1, j,destination_row,destination_column, solutions) - or run_maze(maze, i, j - 1,destination_row,destination_column, solutions) + run_maze(maze, i + 1, j,destination_row, + destination_column, solutions) + or run_maze(maze, i, j + 1,destination_row, + destination_column, solutions) + or run_maze(maze, i - 1, j,destination_row, + destination_column, solutions) + or run_maze(maze, i, j - 1,destination_row, + destination_column, solutions) ): return True From c8c901c46c545b3f5e50d0993cc696b07e2101a1 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Mon, 25 Sep 2023 02:16:50 +0500 Subject: [PATCH 03/18] [UPDATED] #9066 Rate_in_Maze.py --- backtracking/rat_in_maze.py | 49 ++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 35edd828dfc3..3614d2a046a0 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -10,10 +10,11 @@ def solve_maze(maze: list[list[int]]) -> bool: Return: True if the maze has a solution or False if it does not. Description: This method navigates through a maze represented as an n by n matrix, - starting from a specified source cell (default: top-left corner) and + starting from a specified source cell (default: top-left corner) and aiming to reach a destination cell (default: bottom-right corner). The maze consists of walls (0s) and open paths (1s). - By providing custom row and column values, the source and destination cells can be adjusted. + By providing custom row and column values, the source and destination + cells can be adjusted. >>> maze = [[0, 1, 0, 1, 1], ... [0, 0, 0, 0, 0], ... [1, 0, 1, 0, 1], @@ -68,13 +69,14 @@ def solve_maze(maze: list[list[int]]) -> bool: """ # We need to create solution object to save path. size = len(maze) - source_row=0 - source_column=0 - destination_row=size-1 - destination_column=size-1 + source_row = 0 + source_column = 0 + destination_row = size - 1 + destination_column = size - 1 solutions = [[0 for _ in range(size)] for _ in range(size)] - solved = run_maze(maze, source_row, source_column,destination_row, - destination_column, solutions) + solved = run_maze( + maze, source_row, source_column, destination_row, destination_column, solutions + ) if solved: print("\n".join(str(row) for row in solutions)) else: @@ -82,8 +84,14 @@ def solve_maze(maze: list[list[int]]) -> bool: return solved -def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int, - destination_column:int, solutions: list[list[int]]) -> bool: +def run_maze( + maze: list[list[int]], + i: int, + j: int, + destination_row: int, + destination_column: int, + solutions: list[list[int]], +) -> bool: """ This method is recursive starting from (i, j) and going in one of four directions: up, down, left, right. @@ -97,7 +105,7 @@ def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int, """ size = len(maze) # Final check point. - if i == destination_row and j == destination_column: + if i == destination_row and j == destination_column: solutions[i][j] = 1 return True @@ -113,14 +121,16 @@ def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int, # check for directions if ( - run_maze(maze, i + 1, j,destination_row, - destination_column, solutions) - or run_maze(maze, i, j + 1,destination_row, - destination_column, solutions) - or run_maze(maze, i - 1, j,destination_row, - destination_column, solutions) - or run_maze(maze, i, j - 1,destination_row, - destination_column, solutions) + run_maze(maze, i + 1, j, destination_row, destination_column, solutions) + or run_maze( + maze, i, j + 1, destination_row, destination_column, solutions + ) + or run_maze( + maze, i - 1, j, destination_row, destination_column, solutions + ) + or run_maze( + maze, i, j - 1, destination_row, destination_column, solutions + ) ): return True @@ -129,7 +139,6 @@ def run_maze(maze: list[list[int]], i: int, j: int,destination_row:int, return False - if __name__ == "__main__": import doctest From 58bbb8105f7020cab16f32bdeb8d3912b2a24df2 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Mon, 25 Sep 2023 02:23:54 +0500 Subject: [PATCH 04/18] [UPDATED] #9066 rat_in_maze --- backtracking/rat_in_maze.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 3614d2a046a0..48bcf5f8f190 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -67,12 +67,12 @@ def solve_maze(maze: list[list[int]]) -> bool: No solution exists! False """ - # We need to create solution object to save path. size = len(maze) source_row = 0 source_column = 0 destination_row = size - 1 destination_column = size - 1 + # We need to create solution object to save path. solutions = [[0 for _ in range(size)] for _ in range(size)] solved = run_maze( maze, source_row, source_column, destination_row, destination_column, solutions From 8ff2d65939779fdee3a3568d3fe2dababad55c10 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq <115654418+Muhammadummerr@users.noreply.github.com> Date: Tue, 26 Sep 2023 20:49:12 +0500 Subject: [PATCH 05/18] Update backtracking/rat_in_maze.py Co-authored-by: dlesnoff <54949944+dlesnoff@users.noreply.github.com> --- backtracking/rat_in_maze.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 48bcf5f8f190..771e48d37226 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -12,7 +12,7 @@ def solve_maze(maze: list[list[int]]) -> bool: This method navigates through a maze represented as an n by n matrix, starting from a specified source cell (default: top-left corner) and aiming to reach a destination cell (default: bottom-right corner). - The maze consists of walls (0s) and open paths (1s). + The maze consists of walls (1s) and open paths (0s). By providing custom row and column values, the source and destination cells can be adjusted. >>> maze = [[0, 1, 0, 1, 1], From e7686d2fb90c67e264bc2a4bc650938a3f9b05bf Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Fri, 29 Sep 2023 00:49:38 +0500 Subject: [PATCH 06/18] Updated rat_in_maze.py. --- backtracking/rat_in_maze.py | 87 ++++++++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 16 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 48bcf5f8f190..d1a85ce577d2 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -1,18 +1,28 @@ from __future__ import annotations -def solve_maze(maze: list[list[int]]) -> bool: +def solve_maze( + maze: list[list[int]], + source_row: int, + source_column: int, + destination_row: int, + destination_column: int, +) -> bool: """ This method solves the "rat in maze" problem. Parameters : - maze(2D matrix) : maze + - maze(2D matrix) : maze + - source_row (int): The row index of the starting point. + - source_column (int): The column index of the starting point. + - destination_row (int): The row index of the destination point. + - destination_column (int): The column index of the destination point. Returns: Return: True if the maze has a solution or False if it does not. Description: This method navigates through a maze represented as an n by n matrix, - starting from a specified source cell (default: top-left corner) and - aiming to reach a destination cell (default: bottom-right corner). - The maze consists of walls (0s) and open paths (1s). + starting from a specified source cell and + aiming to reach a destination cell. + The maze consists of walls (1s) and open paths (0s). By providing custom row and column values, the source and destination cells can be adjusted. >>> maze = [[0, 1, 0, 1, 1], @@ -20,7 +30,7 @@ def solve_maze(maze: list[list[int]]) -> bool: ... [1, 0, 1, 0, 1], ... [0, 0, 1, 0, 0], ... [1, 0, 0, 1, 0]] - >>> solve_maze(maze) + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) [1, 0, 0, 0, 0] [1, 1, 1, 1, 0] [0, 0, 0, 1, 0] @@ -37,7 +47,7 @@ def solve_maze(maze: list[list[int]]) -> bool: ... [0, 0, 0, 0, 1], ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0]] - >>> solve_maze(maze) + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) [1, 0, 0, 0, 0] [1, 0, 0, 0, 0] [1, 0, 0, 0, 0] @@ -48,30 +58,75 @@ def solve_maze(maze: list[list[int]]) -> bool: >>> maze = [[0, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] - >>> solve_maze(maze) + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) [1, 1, 1] [0, 0, 1] [0, 0, 1] True - >>> maze = [[0, 1, 0], + >>> maze = [[1, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] - >>> solve_maze(maze) + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) + [0, 1, 1] + [0, 0, 1] + [0, 0, 1] + True + + >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], + ... [1, 0, 1, 0, 0, 1, 1, 1], + ... [0, 1, 0, 1, 0, 0, 1, 0], + ... [1, 1, 1, 0, 0, 1, 0, 1], + ... [0, 1, 0, 0, 1, 0, 1, 1], + ... [0, 0, 0, 1, 1, 1, 0, 1], + ... [0, 1, 0, 1, 0, 1, 1, 1], + ... [1, 1, 0, 0, 0, 0, 0, 1]] + >>> solve_maze(maze,0,2,len(maze)-1,2) + [0, 0, 1, 1, 0, 0, 0, 0] + [0, 0, 0, 1, 1, 0, 0, 0] + [0, 0, 0, 0, 1, 0, 0, 0] + [0, 0, 0, 1, 1, 0, 0, 0] + [0, 0, 1, 1, 0, 0, 0, 0] + [0, 0, 1, 0, 0, 0, 0, 0] + [0, 0, 1, 0, 0, 0, 0, 0] + [0, 0, 1, 0, 0, 0, 0, 0] + True + + + >>> maze = [[1, 0, 0], + ... [0, 1, 1], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) No solution exists! False >>> maze = [[0, 1], ... [1, 0]] - >>> solve_maze(maze) + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) No solution exists! False + + >>> maze = [[0, 1], + ... [1, 0]] + >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) + Invalid source coordinates + False + + >>> maze = [[1, 0, 0], + ... [0, 1, 1], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze),len(maze)-1) + Invalid destination coordinates + False """ size = len(maze) - source_row = 0 - source_column = 0 - destination_row = size - 1 - destination_column = size - 1 + # Check if source and destination coordinates are Invalid. + if not (0 <= source_row <= size - 1 and 0 <= source_column <= size - 1): + print("Invalid source coordinates") + return False + elif not (0 <= destination_row <= size - 1 and 0 <= destination_column <= size - 1): + print("Invalid destination coordinates") + return False # We need to create solution object to save path. solutions = [[0 for _ in range(size)] for _ in range(size)] solved = run_maze( @@ -105,7 +160,7 @@ def run_maze( """ size = len(maze) # Final check point. - if i == destination_row and j == destination_column: + if i == destination_row and j == destination_column and maze[i][j] == 0: solutions[i][j] = 1 return True From 932dafd0ff6526fd6fa43e9bca6191473ccf8cbc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 28 Sep 2023 20:01:18 +0000 Subject: [PATCH 07/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/rat_in_maze.py | 228 ++++++++++++++++++------------------ 1 file changed, 114 insertions(+), 114 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index baeaa24a02a8..9c979d72a2d6 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -9,120 +9,120 @@ def solve_maze( destination_column: int, ) -> bool: """ - This method solves the "rat in maze" problem. - Parameters : - - maze(2D matrix) : maze - - source_row (int): The row index of the starting point. - - source_column (int): The column index of the starting point. - - destination_row (int): The row index of the destination point. - - destination_column (int): The column index of the destination point. - Returns: - Return: True if the maze has a solution or False if it does not. - Description: - This method navigates through a maze represented as an n by n matrix, -<<<<<<< HEAD - starting from a specified source cell and - aiming to reach a destination cell. -======= - starting from a specified source cell (default: top-left corner) and - aiming to reach a destination cell (default: bottom-right corner). ->>>>>>> origin/new_branch - The maze consists of walls (1s) and open paths (0s). - By providing custom row and column values, the source and destination - cells can be adjusted. - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [1, 0, 1, 0, 1], - ... [0, 0, 1, 0, 0], - ... [1, 0, 0, 1, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 0] - [0, 0, 0, 1, 0] - [0, 0, 0, 1, 1] - [0, 0, 0, 0, 1] - True - - Note: - In the output maze, the ones (1s) represent one of the possible - paths from the source to the destination. - - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 1] - True - - >>> maze = [[0, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 1, 1] - [0, 0, 1] - [0, 0, 1] - True - - >>> maze = [[1, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [0, 1, 1] - [0, 0, 1] - [0, 0, 1] - True - - >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], - ... [1, 0, 1, 0, 0, 1, 1, 1], - ... [0, 1, 0, 1, 0, 0, 1, 0], - ... [1, 1, 1, 0, 0, 1, 0, 1], - ... [0, 1, 0, 0, 1, 0, 1, 1], - ... [0, 0, 0, 1, 1, 1, 0, 1], - ... [0, 1, 0, 1, 0, 1, 1, 1], - ... [1, 1, 0, 0, 0, 0, 0, 1]] - >>> solve_maze(maze,0,2,len(maze)-1,2) - [0, 0, 1, 1, 0, 0, 0, 0] - [0, 0, 0, 1, 1, 0, 0, 0] - [0, 0, 0, 0, 1, 0, 0, 0] - [0, 0, 0, 1, 1, 0, 0, 0] - [0, 0, 1, 1, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - True - - - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - No solution exists! - False - - >>> maze = [[0, 1], - ... [1, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - No solution exists! - False - - >>> maze = [[0, 1], - ... [1, 0]] - >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) - Invalid source coordinates - False - - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze),len(maze)-1) - Invalid destination coordinates - False + This method solves the "rat in maze" problem. + Parameters : + - maze(2D matrix) : maze + - source_row (int): The row index of the starting point. + - source_column (int): The column index of the starting point. + - destination_row (int): The row index of the destination point. + - destination_column (int): The column index of the destination point. + Returns: + Return: True if the maze has a solution or False if it does not. + Description: + This method navigates through a maze represented as an n by n matrix, + <<<<<<< HEAD + starting from a specified source cell and + aiming to reach a destination cell. + ======= + starting from a specified source cell (default: top-left corner) and + aiming to reach a destination cell (default: bottom-right corner). + >>>>>>> origin/new_branch + The maze consists of walls (1s) and open paths (0s). + By providing custom row and column values, the source and destination + cells can be adjusted. + >>> maze = [[0, 1, 0, 1, 1], + ... [0, 0, 0, 0, 0], + ... [1, 0, 1, 0, 1], + ... [0, 0, 1, 0, 0], + ... [1, 0, 0, 1, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [1, 0, 0, 0, 0] + [1, 1, 1, 1, 0] + [0, 0, 0, 1, 0] + [0, 0, 0, 1, 1] + [0, 0, 0, 0, 1] + True + + Note: + In the output maze, the ones (1s) represent one of the possible + paths from the source to the destination. + + >>> maze = [[0, 1, 0, 1, 1], + ... [0, 0, 0, 0, 0], + ... [0, 0, 0, 0, 1], + ... [0, 0, 0, 0, 0], + ... [0, 0, 0, 0, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [1, 0, 0, 0, 0] + [1, 0, 0, 0, 0] + [1, 0, 0, 0, 0] + [1, 0, 0, 0, 0] + [1, 1, 1, 1, 1] + True + + >>> maze = [[0, 0, 0], + ... [0, 1, 0], + ... [1, 0, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [1, 1, 1] + [0, 0, 1] + [0, 0, 1] + True + + >>> maze = [[1, 0, 0], + ... [0, 1, 0], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) + [0, 1, 1] + [0, 0, 1] + [0, 0, 1] + True + + >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], + ... [1, 0, 1, 0, 0, 1, 1, 1], + ... [0, 1, 0, 1, 0, 0, 1, 0], + ... [1, 1, 1, 0, 0, 1, 0, 1], + ... [0, 1, 0, 0, 1, 0, 1, 1], + ... [0, 0, 0, 1, 1, 1, 0, 1], + ... [0, 1, 0, 1, 0, 1, 1, 1], + ... [1, 1, 0, 0, 0, 0, 0, 1]] + >>> solve_maze(maze,0,2,len(maze)-1,2) + [0, 0, 1, 1, 0, 0, 0, 0] + [0, 0, 0, 1, 1, 0, 0, 0] + [0, 0, 0, 0, 1, 0, 0, 0] + [0, 0, 0, 1, 1, 0, 0, 0] + [0, 0, 1, 1, 0, 0, 0, 0] + [0, 0, 1, 0, 0, 0, 0, 0] + [0, 0, 1, 0, 0, 0, 0, 0] + [0, 0, 1, 0, 0, 0, 0, 0] + True + + + >>> maze = [[1, 0, 0], + ... [0, 1, 1], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) + No solution exists! + False + + >>> maze = [[0, 1], + ... [1, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + No solution exists! + False + + >>> maze = [[0, 1], + ... [1, 0]] + >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) + Invalid source coordinates + False + + >>> maze = [[1, 0, 0], + ... [0, 1, 1], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze),len(maze)-1) + Invalid destination coordinates + False """ size = len(maze) # Check if source and destination coordinates are Invalid. From 553d1f0c581776224b88fed96619caf549c3d641 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq <115654418+Muhammadummerr@users.noreply.github.com> Date: Fri, 29 Sep 2023 11:45:58 +0500 Subject: [PATCH 08/18] Update backtracking/rat_in_maze.py Co-authored-by: Rohan Anand <96521078+rohan472000@users.noreply.github.com> --- backtracking/rat_in_maze.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 9c979d72a2d6..6ade6f430d5c 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -26,7 +26,7 @@ def solve_maze( ======= starting from a specified source cell (default: top-left corner) and aiming to reach a destination cell (default: bottom-right corner). - >>>>>>> origin/new_branch + Origin/new_branch : The maze consists of walls (1s) and open paths (0s). By providing custom row and column values, the source and destination cells can be adjusted. From 765f8671a22e9948faaab28ab602e61c563707e6 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Fri, 29 Sep 2023 15:03:44 +0500 Subject: [PATCH 09/18] [Updated] rat_in_maze.py --- backtracking/rat_in_maze.py | 66 ++++++++++--------------------------- 1 file changed, 17 insertions(+), 49 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index baeaa24a02a8..6a3b99524850 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -17,16 +17,11 @@ def solve_maze( - destination_row (int): The row index of the destination point. - destination_column (int): The column index of the destination point. Returns: - Return: True if the maze has a solution or False if it does not. + Return: solution(2D matrix) if path exists ,otherwise None. Description: This method navigates through a maze represented as an n by n matrix, -<<<<<<< HEAD starting from a specified source cell and aiming to reach a destination cell. -======= - starting from a specified source cell (default: top-left corner) and - aiming to reach a destination cell (default: bottom-right corner). ->>>>>>> origin/new_branch The maze consists of walls (1s) and open paths (0s). By providing custom row and column values, the source and destination cells can be adjusted. @@ -36,12 +31,7 @@ def solve_maze( ... [0, 0, 1, 0, 0], ... [1, 0, 0, 1, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 0] - [0, 0, 0, 1, 0] - [0, 0, 0, 1, 1] - [0, 0, 0, 0, 1] - True + [[1, 0, 0, 0, 0], [1, 1, 1, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 1], [0, 0, 0, 0, 1]] Note: In the output maze, the ones (1s) represent one of the possible @@ -53,30 +43,19 @@ def solve_maze( ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 1] - True + [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 1, 1, 1]] >>> maze = [[0, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 1, 1] - [0, 0, 1] - [0, 0, 1] - True + [[1, 1, 1], [0, 0, 1], [0, 0, 1]] >>> maze = [[1, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [0, 1, 1] - [0, 0, 1] - [0, 0, 1] - True + [[0, 1, 1], [0, 0, 1], [0, 0, 1]] >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], ... [1, 0, 1, 0, 0, 1, 1, 1], @@ -87,15 +66,7 @@ def solve_maze( ... [0, 1, 0, 1, 0, 1, 1, 1], ... [1, 1, 0, 0, 0, 0, 0, 1]] >>> solve_maze(maze,0,2,len(maze)-1,2) - [0, 0, 1, 1, 0, 0, 0, 0] - [0, 0, 0, 1, 1, 0, 0, 0] - [0, 0, 0, 0, 1, 0, 0, 0] - [0, 0, 0, 1, 1, 0, 0, 0] - [0, 0, 1, 1, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - True + [[0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0]] >>> maze = [[1, 0, 0], @@ -103,45 +74,42 @@ def solve_maze( ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) No solution exists! - False - >>> maze = [[0, 1], - ... [1, 0]] + >>> maze = [[0, 0], + ... [1, 1]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) No solution exists! - False >>> maze = [[0, 1], ... [1, 0]] >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) Invalid source coordinates - False >>> maze = [[1, 0, 0], ... [0, 1, 1], ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze),len(maze)-1) Invalid destination coordinates - False + """ size = len(maze) # Check if source and destination coordinates are Invalid. if not (0 <= source_row <= size - 1 and 0 <= source_column <= size - 1): print("Invalid source coordinates") - return False + return None elif not (0 <= destination_row <= size - 1 and 0 <= destination_column <= size - 1): print("Invalid destination coordinates") - return False + return None # We need to create solution object to save path. solutions = [[0 for _ in range(size)] for _ in range(size)] solved = run_maze( maze, source_row, source_column, destination_row, destination_column, solutions ) if solved: - print("\n".join(str(row) for row in solutions)) + return solutions else: print("No solution exists!") - return solved + return None def run_maze( @@ -167,7 +135,7 @@ def run_maze( # Final check point. if i == destination_row and j == destination_column and maze[i][j] == 0: solutions[i][j] = 1 - return True + return solutions lower_flag = (not i < 0) and (not j < 0) # Check lower bounds upper_flag = (i < size) and (j < size) # Check upper bounds @@ -192,11 +160,11 @@ def run_maze( maze, i, j - 1, destination_row, destination_column, solutions ) ): - return True + return solutions solutions[i][j] = 0 - return False - return False + return None + return None if __name__ == "__main__": From 3897d0a15a3816aed634260f81e3237ec660bddc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 Sep 2023 10:08:41 +0000 Subject: [PATCH 10/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/rat_in_maze.py | 254 ++++++++++++++++++------------------ 1 file changed, 127 insertions(+), 127 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 7fb7105f3fef..30f52442183b 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -9,31 +9,7 @@ def solve_maze( destination_column: int, ) -> bool: """ -<<<<<<< HEAD - This method solves the "rat in maze" problem. - Parameters : - - maze(2D matrix) : maze - - source_row (int): The row index of the starting point. - - source_column (int): The column index of the starting point. - - destination_row (int): The row index of the destination point. - - destination_column (int): The column index of the destination point. - Returns: - Return: solution(2D matrix) if path exists ,otherwise None. - Description: - This method navigates through a maze represented as an n by n matrix, - starting from a specified source cell and - aiming to reach a destination cell. - The maze consists of walls (1s) and open paths (0s). - By providing custom row and column values, the source and destination - cells can be adjusted. - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [1, 0, 1, 0, 1], - ... [0, 0, 1, 0, 0], - ... [1, 0, 0, 1, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 0, 0, 0, 0], [1, 1, 1, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 1], [0, 0, 0, 0, 1]] -======= + <<<<<<< HEAD This method solves the "rat in maze" problem. Parameters : - maze(2D matrix) : maze @@ -42,16 +18,11 @@ def solve_maze( - destination_row (int): The row index of the destination point. - destination_column (int): The column index of the destination point. Returns: - Return: True if the maze has a solution or False if it does not. + Return: solution(2D matrix) if path exists ,otherwise None. Description: This method navigates through a maze represented as an n by n matrix, - <<<<<<< HEAD starting from a specified source cell and aiming to reach a destination cell. - ======= - starting from a specified source cell (default: top-left corner) and - aiming to reach a destination cell (default: bottom-right corner). - Origin/new_branch : The maze consists of walls (1s) and open paths (0s). By providing custom row and column values, the source and destination cells can be adjusted. @@ -61,104 +32,67 @@ def solve_maze( ... [0, 0, 1, 0, 0], ... [1, 0, 0, 1, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 0] - [0, 0, 0, 1, 0] - [0, 0, 0, 1, 1] - [0, 0, 0, 0, 1] - True ->>>>>>> 553d1f0c581776224b88fed96619caf549c3d641 - - Note: - In the output maze, the ones (1s) represent one of the possible - paths from the source to the destination. - -<<<<<<< HEAD - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 1, 1, 1]] - - >>> maze = [[0, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 1, 1], [0, 0, 1], [0, 0, 1]] - - >>> maze = [[1, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [[0, 1, 1], [0, 0, 1], [0, 0, 1]] - - >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], - ... [1, 0, 1, 0, 0, 1, 1, 1], - ... [0, 1, 0, 1, 0, 0, 1, 0], - ... [1, 1, 1, 0, 0, 1, 0, 1], - ... [0, 1, 0, 0, 1, 0, 1, 1], - ... [0, 0, 0, 1, 1, 1, 0, 1], - ... [0, 1, 0, 1, 0, 1, 1, 1], - ... [1, 1, 0, 0, 0, 0, 0, 1]] - >>> solve_maze(maze,0,2,len(maze)-1,2) - [[0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0]] - - - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - No solution exists! - - >>> maze = [[0, 0], - ... [1, 1]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - No solution exists! - - >>> maze = [[0, 1], - ... [1, 0]] - >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) - Invalid source coordinates + [[1, 0, 0, 0, 0], [1, 1, 1, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 1], [0, 0, 0, 0, 1]] + ======= + This method solves the "rat in maze" problem. + Parameters : + - maze(2D matrix) : maze + - source_row (int): The row index of the starting point. + - source_column (int): The column index of the starting point. + - destination_row (int): The row index of the destination point. + - destination_column (int): The column index of the destination point. + Returns: + Return: True if the maze has a solution or False if it does not. + Description: + This method navigates through a maze represented as an n by n matrix, + <<<<<<< HEAD + starting from a specified source cell and + aiming to reach a destination cell. + ======= + starting from a specified source cell (default: top-left corner) and + aiming to reach a destination cell (default: bottom-right corner). + Origin/new_branch : + The maze consists of walls (1s) and open paths (0s). + By providing custom row and column values, the source and destination + cells can be adjusted. + >>> maze = [[0, 1, 0, 1, 1], + ... [0, 0, 0, 0, 0], + ... [1, 0, 1, 0, 1], + ... [0, 0, 1, 0, 0], + ... [1, 0, 0, 1, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [1, 0, 0, 0, 0] + [1, 1, 1, 1, 0] + [0, 0, 0, 1, 0] + [0, 0, 0, 1, 1] + [0, 0, 0, 0, 1] + True + >>>>>>> 553d1f0c581776224b88fed96619caf549c3d641 - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze),len(maze)-1) - Invalid destination coordinates + Note: + In the output maze, the ones (1s) represent one of the possible + paths from the source to the destination. -======= + <<<<<<< HEAD >>> maze = [[0, 1, 0, 1, 1], ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 1], ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 1] - True + [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 1, 1, 1]] >>> maze = [[0, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 1, 1] - [0, 0, 1] - [0, 0, 1] - True + [[1, 1, 1], [0, 0, 1], [0, 0, 1]] >>> maze = [[1, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [0, 1, 1] - [0, 0, 1] - [0, 0, 1] - True + [[0, 1, 1], [0, 0, 1], [0, 0, 1]] >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], ... [1, 0, 1, 0, 0, 1, 1, 1], @@ -169,15 +103,7 @@ def solve_maze( ... [0, 1, 0, 1, 0, 1, 1, 1], ... [1, 1, 0, 0, 0, 0, 0, 1]] >>> solve_maze(maze,0,2,len(maze)-1,2) - [0, 0, 1, 1, 0, 0, 0, 0] - [0, 0, 0, 1, 1, 0, 0, 0] - [0, 0, 0, 0, 1, 0, 0, 0] - [0, 0, 0, 1, 1, 0, 0, 0] - [0, 0, 1, 1, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - True + [[0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0]] >>> maze = [[1, 0, 0], @@ -185,27 +111,101 @@ def solve_maze( ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) No solution exists! - False - >>> maze = [[0, 1], - ... [1, 0]] + >>> maze = [[0, 0], + ... [1, 1]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) No solution exists! - False >>> maze = [[0, 1], ... [1, 0]] >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) Invalid source coordinates - False >>> maze = [[1, 0, 0], ... [0, 1, 1], ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze),len(maze)-1) Invalid destination coordinates - False ->>>>>>> 553d1f0c581776224b88fed96619caf549c3d641 + + ======= + >>> maze = [[0, 1, 0, 1, 1], + ... [0, 0, 0, 0, 0], + ... [0, 0, 0, 0, 1], + ... [0, 0, 0, 0, 0], + ... [0, 0, 0, 0, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [1, 0, 0, 0, 0] + [1, 0, 0, 0, 0] + [1, 0, 0, 0, 0] + [1, 0, 0, 0, 0] + [1, 1, 1, 1, 1] + True + + >>> maze = [[0, 0, 0], + ... [0, 1, 0], + ... [1, 0, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [1, 1, 1] + [0, 0, 1] + [0, 0, 1] + True + + >>> maze = [[1, 0, 0], + ... [0, 1, 0], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) + [0, 1, 1] + [0, 0, 1] + [0, 0, 1] + True + + >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], + ... [1, 0, 1, 0, 0, 1, 1, 1], + ... [0, 1, 0, 1, 0, 0, 1, 0], + ... [1, 1, 1, 0, 0, 1, 0, 1], + ... [0, 1, 0, 0, 1, 0, 1, 1], + ... [0, 0, 0, 1, 1, 1, 0, 1], + ... [0, 1, 0, 1, 0, 1, 1, 1], + ... [1, 1, 0, 0, 0, 0, 0, 1]] + >>> solve_maze(maze,0,2,len(maze)-1,2) + [0, 0, 1, 1, 0, 0, 0, 0] + [0, 0, 0, 1, 1, 0, 0, 0] + [0, 0, 0, 0, 1, 0, 0, 0] + [0, 0, 0, 1, 1, 0, 0, 0] + [0, 0, 1, 1, 0, 0, 0, 0] + [0, 0, 1, 0, 0, 0, 0, 0] + [0, 0, 1, 0, 0, 0, 0, 0] + [0, 0, 1, 0, 0, 0, 0, 0] + True + + + >>> maze = [[1, 0, 0], + ... [0, 1, 1], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) + No solution exists! + False + + >>> maze = [[0, 1], + ... [1, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + No solution exists! + False + + >>> maze = [[0, 1], + ... [1, 0]] + >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) + Invalid source coordinates + False + + >>> maze = [[1, 0, 0], + ... [0, 1, 1], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze),len(maze)-1) + Invalid destination coordinates + False + >>>>>>> 553d1f0c581776224b88fed96619caf549c3d641 """ size = len(maze) # Check if source and destination coordinates are Invalid. From 84c9766d34e2cf81f0300eaa8173f2c1fdc57b79 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Fri, 29 Sep 2023 15:10:45 +0500 Subject: [PATCH 11/18] UPDATED rat_in_maze.py --- backtracking/rat_in_maze.py | 254 ++++++++++++++++++------------------ 1 file changed, 127 insertions(+), 127 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 7fb7105f3fef..30f52442183b 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -9,31 +9,7 @@ def solve_maze( destination_column: int, ) -> bool: """ -<<<<<<< HEAD - This method solves the "rat in maze" problem. - Parameters : - - maze(2D matrix) : maze - - source_row (int): The row index of the starting point. - - source_column (int): The column index of the starting point. - - destination_row (int): The row index of the destination point. - - destination_column (int): The column index of the destination point. - Returns: - Return: solution(2D matrix) if path exists ,otherwise None. - Description: - This method navigates through a maze represented as an n by n matrix, - starting from a specified source cell and - aiming to reach a destination cell. - The maze consists of walls (1s) and open paths (0s). - By providing custom row and column values, the source and destination - cells can be adjusted. - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [1, 0, 1, 0, 1], - ... [0, 0, 1, 0, 0], - ... [1, 0, 0, 1, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 0, 0, 0, 0], [1, 1, 1, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 1], [0, 0, 0, 0, 1]] -======= + <<<<<<< HEAD This method solves the "rat in maze" problem. Parameters : - maze(2D matrix) : maze @@ -42,16 +18,11 @@ def solve_maze( - destination_row (int): The row index of the destination point. - destination_column (int): The column index of the destination point. Returns: - Return: True if the maze has a solution or False if it does not. + Return: solution(2D matrix) if path exists ,otherwise None. Description: This method navigates through a maze represented as an n by n matrix, - <<<<<<< HEAD starting from a specified source cell and aiming to reach a destination cell. - ======= - starting from a specified source cell (default: top-left corner) and - aiming to reach a destination cell (default: bottom-right corner). - Origin/new_branch : The maze consists of walls (1s) and open paths (0s). By providing custom row and column values, the source and destination cells can be adjusted. @@ -61,104 +32,67 @@ def solve_maze( ... [0, 0, 1, 0, 0], ... [1, 0, 0, 1, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 0] - [0, 0, 0, 1, 0] - [0, 0, 0, 1, 1] - [0, 0, 0, 0, 1] - True ->>>>>>> 553d1f0c581776224b88fed96619caf549c3d641 - - Note: - In the output maze, the ones (1s) represent one of the possible - paths from the source to the destination. - -<<<<<<< HEAD - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 1, 1, 1]] - - >>> maze = [[0, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 1, 1], [0, 0, 1], [0, 0, 1]] - - >>> maze = [[1, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [[0, 1, 1], [0, 0, 1], [0, 0, 1]] - - >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], - ... [1, 0, 1, 0, 0, 1, 1, 1], - ... [0, 1, 0, 1, 0, 0, 1, 0], - ... [1, 1, 1, 0, 0, 1, 0, 1], - ... [0, 1, 0, 0, 1, 0, 1, 1], - ... [0, 0, 0, 1, 1, 1, 0, 1], - ... [0, 1, 0, 1, 0, 1, 1, 1], - ... [1, 1, 0, 0, 0, 0, 0, 1]] - >>> solve_maze(maze,0,2,len(maze)-1,2) - [[0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0]] - - - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - No solution exists! - - >>> maze = [[0, 0], - ... [1, 1]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - No solution exists! - - >>> maze = [[0, 1], - ... [1, 0]] - >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) - Invalid source coordinates + [[1, 0, 0, 0, 0], [1, 1, 1, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 1], [0, 0, 0, 0, 1]] + ======= + This method solves the "rat in maze" problem. + Parameters : + - maze(2D matrix) : maze + - source_row (int): The row index of the starting point. + - source_column (int): The column index of the starting point. + - destination_row (int): The row index of the destination point. + - destination_column (int): The column index of the destination point. + Returns: + Return: True if the maze has a solution or False if it does not. + Description: + This method navigates through a maze represented as an n by n matrix, + <<<<<<< HEAD + starting from a specified source cell and + aiming to reach a destination cell. + ======= + starting from a specified source cell (default: top-left corner) and + aiming to reach a destination cell (default: bottom-right corner). + Origin/new_branch : + The maze consists of walls (1s) and open paths (0s). + By providing custom row and column values, the source and destination + cells can be adjusted. + >>> maze = [[0, 1, 0, 1, 1], + ... [0, 0, 0, 0, 0], + ... [1, 0, 1, 0, 1], + ... [0, 0, 1, 0, 0], + ... [1, 0, 0, 1, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [1, 0, 0, 0, 0] + [1, 1, 1, 1, 0] + [0, 0, 0, 1, 0] + [0, 0, 0, 1, 1] + [0, 0, 0, 0, 1] + True + >>>>>>> 553d1f0c581776224b88fed96619caf549c3d641 - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze),len(maze)-1) - Invalid destination coordinates + Note: + In the output maze, the ones (1s) represent one of the possible + paths from the source to the destination. -======= + <<<<<<< HEAD >>> maze = [[0, 1, 0, 1, 1], ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 1], ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 1] - True + [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 1, 1, 1]] >>> maze = [[0, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 1, 1] - [0, 0, 1] - [0, 0, 1] - True + [[1, 1, 1], [0, 0, 1], [0, 0, 1]] >>> maze = [[1, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [0, 1, 1] - [0, 0, 1] - [0, 0, 1] - True + [[0, 1, 1], [0, 0, 1], [0, 0, 1]] >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], ... [1, 0, 1, 0, 0, 1, 1, 1], @@ -169,15 +103,7 @@ def solve_maze( ... [0, 1, 0, 1, 0, 1, 1, 1], ... [1, 1, 0, 0, 0, 0, 0, 1]] >>> solve_maze(maze,0,2,len(maze)-1,2) - [0, 0, 1, 1, 0, 0, 0, 0] - [0, 0, 0, 1, 1, 0, 0, 0] - [0, 0, 0, 0, 1, 0, 0, 0] - [0, 0, 0, 1, 1, 0, 0, 0] - [0, 0, 1, 1, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - True + [[0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0]] >>> maze = [[1, 0, 0], @@ -185,27 +111,101 @@ def solve_maze( ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) No solution exists! - False - >>> maze = [[0, 1], - ... [1, 0]] + >>> maze = [[0, 0], + ... [1, 1]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) No solution exists! - False >>> maze = [[0, 1], ... [1, 0]] >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) Invalid source coordinates - False >>> maze = [[1, 0, 0], ... [0, 1, 1], ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze),len(maze)-1) Invalid destination coordinates - False ->>>>>>> 553d1f0c581776224b88fed96619caf549c3d641 + + ======= + >>> maze = [[0, 1, 0, 1, 1], + ... [0, 0, 0, 0, 0], + ... [0, 0, 0, 0, 1], + ... [0, 0, 0, 0, 0], + ... [0, 0, 0, 0, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [1, 0, 0, 0, 0] + [1, 0, 0, 0, 0] + [1, 0, 0, 0, 0] + [1, 0, 0, 0, 0] + [1, 1, 1, 1, 1] + True + + >>> maze = [[0, 0, 0], + ... [0, 1, 0], + ... [1, 0, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [1, 1, 1] + [0, 0, 1] + [0, 0, 1] + True + + >>> maze = [[1, 0, 0], + ... [0, 1, 0], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) + [0, 1, 1] + [0, 0, 1] + [0, 0, 1] + True + + >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], + ... [1, 0, 1, 0, 0, 1, 1, 1], + ... [0, 1, 0, 1, 0, 0, 1, 0], + ... [1, 1, 1, 0, 0, 1, 0, 1], + ... [0, 1, 0, 0, 1, 0, 1, 1], + ... [0, 0, 0, 1, 1, 1, 0, 1], + ... [0, 1, 0, 1, 0, 1, 1, 1], + ... [1, 1, 0, 0, 0, 0, 0, 1]] + >>> solve_maze(maze,0,2,len(maze)-1,2) + [0, 0, 1, 1, 0, 0, 0, 0] + [0, 0, 0, 1, 1, 0, 0, 0] + [0, 0, 0, 0, 1, 0, 0, 0] + [0, 0, 0, 1, 1, 0, 0, 0] + [0, 0, 1, 1, 0, 0, 0, 0] + [0, 0, 1, 0, 0, 0, 0, 0] + [0, 0, 1, 0, 0, 0, 0, 0] + [0, 0, 1, 0, 0, 0, 0, 0] + True + + + >>> maze = [[1, 0, 0], + ... [0, 1, 1], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) + No solution exists! + False + + >>> maze = [[0, 1], + ... [1, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + No solution exists! + False + + >>> maze = [[0, 1], + ... [1, 0]] + >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) + Invalid source coordinates + False + + >>> maze = [[1, 0, 0], + ... [0, 1, 1], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze),len(maze)-1) + Invalid destination coordinates + False + >>>>>>> 553d1f0c581776224b88fed96619caf549c3d641 """ size = len(maze) # Check if source and destination coordinates are Invalid. From 2045f7f45ef2f39a8dcdd1d4684b167d5590187e Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Fri, 29 Sep 2023 16:27:47 +0500 Subject: [PATCH 12/18] Formatted rat_in_maze.py --- backtracking/rat_in_maze.py | 281 +++++++++++------------------------- 1 file changed, 85 insertions(+), 196 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 30f52442183b..1ab363b00209 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -9,203 +9,92 @@ def solve_maze( destination_column: int, ) -> bool: """ - <<<<<<< HEAD - This method solves the "rat in maze" problem. - Parameters : - - maze(2D matrix) : maze - - source_row (int): The row index of the starting point. - - source_column (int): The column index of the starting point. - - destination_row (int): The row index of the destination point. - - destination_column (int): The column index of the destination point. - Returns: - Return: solution(2D matrix) if path exists ,otherwise None. - Description: - This method navigates through a maze represented as an n by n matrix, - starting from a specified source cell and - aiming to reach a destination cell. - The maze consists of walls (1s) and open paths (0s). - By providing custom row and column values, the source and destination - cells can be adjusted. - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [1, 0, 1, 0, 1], - ... [0, 0, 1, 0, 0], - ... [1, 0, 0, 1, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 0, 0, 0, 0], [1, 1, 1, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 1], [0, 0, 0, 0, 1]] - ======= - This method solves the "rat in maze" problem. - Parameters : - - maze(2D matrix) : maze - - source_row (int): The row index of the starting point. - - source_column (int): The column index of the starting point. - - destination_row (int): The row index of the destination point. - - destination_column (int): The column index of the destination point. - Returns: - Return: True if the maze has a solution or False if it does not. - Description: - This method navigates through a maze represented as an n by n matrix, - <<<<<<< HEAD - starting from a specified source cell and - aiming to reach a destination cell. - ======= - starting from a specified source cell (default: top-left corner) and - aiming to reach a destination cell (default: bottom-right corner). - Origin/new_branch : - The maze consists of walls (1s) and open paths (0s). - By providing custom row and column values, the source and destination - cells can be adjusted. - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [1, 0, 1, 0, 1], - ... [0, 0, 1, 0, 0], - ... [1, 0, 0, 1, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 0] - [0, 0, 0, 1, 0] - [0, 0, 0, 1, 1] - [0, 0, 0, 0, 1] - True - >>>>>>> 553d1f0c581776224b88fed96619caf549c3d641 - - Note: - In the output maze, the ones (1s) represent one of the possible - paths from the source to the destination. - - <<<<<<< HEAD - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 1, 1, 1]] - - >>> maze = [[0, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 1, 1], [0, 0, 1], [0, 0, 1]] - - >>> maze = [[1, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [[0, 1, 1], [0, 0, 1], [0, 0, 1]] - - >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], - ... [1, 0, 1, 0, 0, 1, 1, 1], - ... [0, 1, 0, 1, 0, 0, 1, 0], - ... [1, 1, 1, 0, 0, 1, 0, 1], - ... [0, 1, 0, 0, 1, 0, 1, 1], - ... [0, 0, 0, 1, 1, 1, 0, 1], - ... [0, 1, 0, 1, 0, 1, 1, 1], - ... [1, 1, 0, 0, 0, 0, 0, 1]] - >>> solve_maze(maze,0,2,len(maze)-1,2) - [[0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0]] - - - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - No solution exists! - - >>> maze = [[0, 0], - ... [1, 1]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - No solution exists! - - >>> maze = [[0, 1], - ... [1, 0]] - >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) - Invalid source coordinates - - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze),len(maze)-1) - Invalid destination coordinates - - ======= - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 1] - True - - >>> maze = [[0, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 1, 1] - [0, 0, 1] - [0, 0, 1] - True - - >>> maze = [[1, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [0, 1, 1] - [0, 0, 1] - [0, 0, 1] - True - - >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], - ... [1, 0, 1, 0, 0, 1, 1, 1], - ... [0, 1, 0, 1, 0, 0, 1, 0], - ... [1, 1, 1, 0, 0, 1, 0, 1], - ... [0, 1, 0, 0, 1, 0, 1, 1], - ... [0, 0, 0, 1, 1, 1, 0, 1], - ... [0, 1, 0, 1, 0, 1, 1, 1], - ... [1, 1, 0, 0, 0, 0, 0, 1]] - >>> solve_maze(maze,0,2,len(maze)-1,2) - [0, 0, 1, 1, 0, 0, 0, 0] - [0, 0, 0, 1, 1, 0, 0, 0] - [0, 0, 0, 0, 1, 0, 0, 0] - [0, 0, 0, 1, 1, 0, 0, 0] - [0, 0, 1, 1, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - True - - - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - No solution exists! - False - - >>> maze = [[0, 1], - ... [1, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - No solution exists! - False - - >>> maze = [[0, 1], - ... [1, 0]] - >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) - Invalid source coordinates - False + This method solves the "rat in maze" problem. + Parameters : + - maze(2D matrix) : maze + - source_row (int): The row index of the starting point. + - source_column (int): The column index of the starting point. + - destination_row (int): The row index of the destination point. + - destination_column (int): The column index of the destination point. + Returns: + Return: solution(2D matrix) if path exist ,otherwise None. + Description: + This method navigates through a maze represented as an n by n matrix, + starting from a specified source cell and + aiming to reach a destination cell. + The maze consists of walls (1s) and open paths (0s). + By providing custom row and column values, the source and destination + cells can be adjusted. + >>> maze = [[0, 1, 0, 1, 1], + ... [0, 0, 0, 0, 0], + ... [1, 0, 1, 0, 1], + ... [0, 0, 1, 0, 0], + ... [1, 0, 0, 1, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [[1, 0, 0, 0, 0], [1, 1, 1, 1, 0], [0, 0, 0, 1, 0],\ + [0, 0, 0, 1, 1], [0, 0, 0, 0, 1]] + + Note: + In the output maze, the ones (1s) represent one of the possible + paths from the source to the destination. + + >>> maze = [[0, 1, 0, 1, 1], + ... [0, 0, 0, 0, 0], + ... [0, 0, 0, 0, 1], + ... [0, 0, 0, 0, 0], + ... [0, 0, 0, 0, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0],\ + [1, 0, 0, 0, 0], [1, 1, 1, 1, 1]] + + >>> maze = [[0, 0, 0], + ... [0, 1, 0], + ... [1, 0, 0]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [[1, 1, 1], [0, 0, 1], [0, 0, 1]] + + >>> maze = [[1, 0, 0], + ... [0, 1, 0], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) + [[0, 1, 1], [0, 0, 1], [0, 0, 1]] + + >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], + ... [1, 0, 1, 0, 0, 1, 1, 1], + ... [0, 1, 0, 1, 0, 0, 1, 0], + ... [1, 1, 1, 0, 0, 1, 0, 1], + ... [0, 1, 0, 0, 1, 0, 1, 1], + ... [0, 0, 0, 1, 1, 1, 0, 1], + ... [0, 1, 0, 1, 0, 1, 1, 1], + ... [1, 1, 0, 0, 0, 0, 0, 1]] + >>> solve_maze(maze,0,2,len(maze)-1,2) + [[0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0],\ + [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0],\ + [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0]] + + + >>> maze = [[1, 0, 0], + ... [0, 1, 1], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) + No solution exists! + + >>> maze = [[0, 0], + ... [1, 1]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + No solution exists! + + >>> maze = [[0, 1], + ... [1, 0]] + >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) + Invalid source coordinates + + >>> maze = [[1, 0, 0], + ... [0, 1, 1], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze),len(maze)-1) + Invalid destination coordinates - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze),len(maze)-1) - Invalid destination coordinates - False - >>>>>>> 553d1f0c581776224b88fed96619caf549c3d641 """ size = len(maze) # Check if source and destination coordinates are Invalid. From 92590f8c7dae922cc8671e92711558698957e3c2 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Fri, 29 Sep 2023 16:51:31 +0500 Subject: [PATCH 13/18] formatted rat_in_maze.py --- backtracking/rat_in_maze.py | 200 ------------------------------------ 1 file changed, 200 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 3b2353010761..1ab363b00209 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -9,7 +9,6 @@ def solve_maze( destination_column: int, ) -> bool: """ -<<<<<<< HEAD This method solves the "rat in maze" problem. Parameters : - maze(2D matrix) : maze @@ -96,205 +95,6 @@ def solve_maze( >>> solve_maze(maze,0,1,len(maze),len(maze)-1) Invalid destination coordinates -======= - <<<<<<< HEAD - This method solves the "rat in maze" problem. - Parameters : - - maze(2D matrix) : maze - - source_row (int): The row index of the starting point. - - source_column (int): The column index of the starting point. - - destination_row (int): The row index of the destination point. - - destination_column (int): The column index of the destination point. - Returns: - Return: solution(2D matrix) if path exists ,otherwise None. - Description: - This method navigates through a maze represented as an n by n matrix, - starting from a specified source cell and - aiming to reach a destination cell. - The maze consists of walls (1s) and open paths (0s). - By providing custom row and column values, the source and destination - cells can be adjusted. - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [1, 0, 1, 0, 1], - ... [0, 0, 1, 0, 0], - ... [1, 0, 0, 1, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 0, 0, 0, 0], [1, 1, 1, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 1], [0, 0, 0, 0, 1]] - ======= - This method solves the "rat in maze" problem. - Parameters : - - maze(2D matrix) : maze - - source_row (int): The row index of the starting point. - - source_column (int): The column index of the starting point. - - destination_row (int): The row index of the destination point. - - destination_column (int): The column index of the destination point. - Returns: - Return: True if the maze has a solution or False if it does not. - Description: - This method navigates through a maze represented as an n by n matrix, - <<<<<<< HEAD - starting from a specified source cell and - aiming to reach a destination cell. - ======= - starting from a specified source cell (default: top-left corner) and - aiming to reach a destination cell (default: bottom-right corner). - Origin/new_branch : - The maze consists of walls (1s) and open paths (0s). - By providing custom row and column values, the source and destination - cells can be adjusted. - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [1, 0, 1, 0, 1], - ... [0, 0, 1, 0, 0], - ... [1, 0, 0, 1, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 0] - [0, 0, 0, 1, 0] - [0, 0, 0, 1, 1] - [0, 0, 0, 0, 1] - True - >>>>>>> 553d1f0c581776224b88fed96619caf549c3d641 - - Note: - In the output maze, the ones (1s) represent one of the possible - paths from the source to the destination. - - <<<<<<< HEAD - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 1, 1, 1]] - - >>> maze = [[0, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 1, 1], [0, 0, 1], [0, 0, 1]] - - >>> maze = [[1, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [[0, 1, 1], [0, 0, 1], [0, 0, 1]] - - >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], - ... [1, 0, 1, 0, 0, 1, 1, 1], - ... [0, 1, 0, 1, 0, 0, 1, 0], - ... [1, 1, 1, 0, 0, 1, 0, 1], - ... [0, 1, 0, 0, 1, 0, 1, 1], - ... [0, 0, 0, 1, 1, 1, 0, 1], - ... [0, 1, 0, 1, 0, 1, 1, 1], - ... [1, 1, 0, 0, 0, 0, 0, 1]] - >>> solve_maze(maze,0,2,len(maze)-1,2) - [[0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0]] - - - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - No solution exists! - - >>> maze = [[0, 0], - ... [1, 1]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - No solution exists! - - >>> maze = [[0, 1], - ... [1, 0]] - >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) - Invalid source coordinates - - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze),len(maze)-1) - Invalid destination coordinates - - ======= - >>> maze = [[0, 1, 0, 1, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 1], - ... [0, 0, 0, 0, 0], - ... [0, 0, 0, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 1] - True - - >>> maze = [[0, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [1, 1, 1] - [0, 0, 1] - [0, 0, 1] - True - - >>> maze = [[1, 0, 0], - ... [0, 1, 0], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [0, 1, 1] - [0, 0, 1] - [0, 0, 1] - True - - >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], - ... [1, 0, 1, 0, 0, 1, 1, 1], - ... [0, 1, 0, 1, 0, 0, 1, 0], - ... [1, 1, 1, 0, 0, 1, 0, 1], - ... [0, 1, 0, 0, 1, 0, 1, 1], - ... [0, 0, 0, 1, 1, 1, 0, 1], - ... [0, 1, 0, 1, 0, 1, 1, 1], - ... [1, 1, 0, 0, 0, 0, 0, 1]] - >>> solve_maze(maze,0,2,len(maze)-1,2) - [0, 0, 1, 1, 0, 0, 0, 0] - [0, 0, 0, 1, 1, 0, 0, 0] - [0, 0, 0, 0, 1, 0, 0, 0] - [0, 0, 0, 1, 1, 0, 0, 0] - [0, 0, 1, 1, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - [0, 0, 1, 0, 0, 0, 0, 0] - True - - - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - No solution exists! - False - - >>> maze = [[0, 1], - ... [1, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - No solution exists! - False - - >>> maze = [[0, 1], - ... [1, 0]] - >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) - Invalid source coordinates - False - - >>> maze = [[1, 0, 0], - ... [0, 1, 1], - ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze),len(maze)-1) - Invalid destination coordinates - False - >>>>>>> 553d1f0c581776224b88fed96619caf549c3d641 ->>>>>>> 3897d0a15a3816aed634260f81e3237ec660bddc """ size = len(maze) # Check if source and destination coordinates are Invalid. From 39a00ca2f2ac6dbd2c55ae5ed33418d6ffab36fd Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Fri, 29 Sep 2023 19:28:38 +0500 Subject: [PATCH 14/18] fixed return type. --- backtracking/rat_in_maze.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 1ab363b00209..5cdcfa4f47fc 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -7,7 +7,7 @@ def solve_maze( source_column: int, destination_row: int, destination_column: int, -) -> bool: +) -> list[list[int]] or None: """ This method solves the "rat in maze" problem. Parameters : @@ -71,11 +71,9 @@ def solve_maze( [[0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0],\ [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0],\ [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0]] - - >>> maze = [[1, 0, 0], ... [0, 1, 1], - ... [1, 0, 0]] + ... [1, 0, 1]] >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) No solution exists! From 3076868d8c069f81a26470a27d1dcdff066aa51a Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Fri, 29 Sep 2023 19:32:43 +0500 Subject: [PATCH 15/18] fixed return type. --- backtracking/rat_in_maze.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 5cdcfa4f47fc..aa2f66e08708 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -121,7 +121,7 @@ def run_maze( destination_row: int, destination_column: int, solutions: list[list[int]], -) -> bool: +) -> list[list[int]] or None: """ This method is recursive starting from (i, j) and going in one of four directions: up, down, left, right. From 9e045ffc9b03c6c31491634de8d66424037af56a Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Fri, 29 Sep 2023 19:39:47 +0500 Subject: [PATCH 16/18] fixed return type. --- backtracking/rat_in_maze.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index aa2f66e08708..fe76246bcc55 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -7,7 +7,7 @@ def solve_maze( source_column: int, destination_row: int, destination_column: int, -) -> list[list[int]] or None: +) -> list[list[int]] | None: """ This method solves the "rat in maze" problem. Parameters : @@ -121,7 +121,7 @@ def run_maze( destination_row: int, destination_column: int, solutions: list[list[int]], -) -> list[list[int]] or None: +) -> list[list[int]] | None: """ This method is recursive starting from (i, j) and going in one of four directions: up, down, left, right. From 6ffb7b06681784f771eb9e3f612bc3cb4c1bae2d Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Fri, 29 Sep 2023 23:44:07 +0500 Subject: [PATCH 17/18] removed print statements. --- backtracking/rat_in_maze.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index fe76246bcc55..9ddb016976b7 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -75,32 +75,26 @@ def solve_maze( ... [0, 1, 1], ... [1, 0, 1]] >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - No solution exists! >>> maze = [[0, 0], ... [1, 1]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - No solution exists! >>> maze = [[0, 1], ... [1, 0]] >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) - Invalid source coordinates >>> maze = [[1, 0, 0], ... [0, 1, 1], ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze),len(maze)-1) - Invalid destination coordinates """ size = len(maze) # Check if source and destination coordinates are Invalid. - if not (0 <= source_row <= size - 1 and 0 <= source_column <= size - 1): - print("Invalid source coordinates") - return None - elif not (0 <= destination_row <= size - 1 and 0 <= destination_column <= size - 1): - print("Invalid destination coordinates") + if not (0 <= source_row <= size - 1 and 0 <= source_column <= size - 1) or ( + not (0 <= destination_row <= size - 1 and 0 <= destination_column <= size - 1) + ): return None # We need to create solution object to save path. solutions = [[0 for _ in range(size)] for _ in range(size)] @@ -110,7 +104,6 @@ def solve_maze( if solved: return solutions else: - print("No solution exists!") return None @@ -126,7 +119,7 @@ def run_maze( This method is recursive starting from (i, j) and going in one of four directions: up, down, left, right. If a path is found to destination it returns True otherwise it returns False. - Parameters: + Parameters maze(2D matrix) : maze i, j : coordinates of matrix solutions(2D matrix) : solutions From d5c3f4f252b00cdb1c567afa1f4bd5f9b4728401 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Sat, 30 Sep 2023 00:18:37 +0500 Subject: [PATCH 18/18] Refactor path representation in solution. --- backtracking/rat_in_maze.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 9ddb016976b7..27f5d80120b6 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -31,11 +31,11 @@ def solve_maze( ... [0, 0, 1, 0, 0], ... [1, 0, 0, 1, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 0, 0, 0, 0], [1, 1, 1, 1, 0], [0, 0, 0, 1, 0],\ - [0, 0, 0, 1, 1], [0, 0, 0, 0, 1]] + [[0, 1, 1, 1, 1], [0, 0, 0, 0, 1], [1, 1, 1, 0, 1],\ + [1, 1, 1, 0, 0], [1, 1, 1, 1, 0]] Note: - In the output maze, the ones (1s) represent one of the possible + In the output maze, the zeros (0s) represent one of the possible paths from the source to the destination. >>> maze = [[0, 1, 0, 1, 1], @@ -44,20 +44,20 @@ def solve_maze( ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0],\ - [1, 0, 0, 0, 0], [1, 1, 1, 1, 1]] + [[0, 1, 1, 1, 1], [0, 1, 1, 1, 1], [0, 1, 1, 1, 1],\ + [0, 1, 1, 1, 1], [0, 0, 0, 0, 0]] >>> maze = [[0, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[1, 1, 1], [0, 0, 1], [0, 0, 1]] + [[0, 0, 0], [1, 1, 0], [1, 1, 0]] >>> maze = [[1, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [[0, 1, 1], [0, 0, 1], [0, 0, 1]] + [[1, 0, 0], [1, 1, 0], [1, 1, 0]] >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], ... [1, 0, 1, 0, 0, 1, 1, 1], @@ -68,9 +68,9 @@ def solve_maze( ... [0, 1, 0, 1, 0, 1, 1, 1], ... [1, 1, 0, 0, 0, 0, 0, 1]] >>> solve_maze(maze,0,2,len(maze)-1,2) - [[0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0],\ - [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0],\ - [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0]] + [[1, 1, 0, 0, 1, 1, 1, 1], [1, 1, 1, 0, 0, 1, 1, 1], [1, 1, 1, 1, 0, 1, 1, 1],\ + [1, 1, 1, 0, 0, 1, 1, 1], [1, 1, 0, 0, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1],\ + [1, 1, 0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1]] >>> maze = [[1, 0, 0], ... [0, 1, 1], ... [1, 0, 1]] @@ -97,7 +97,7 @@ def solve_maze( ): return None # We need to create solution object to save path. - solutions = [[0 for _ in range(size)] for _ in range(size)] + solutions = [[1 for _ in range(size)] for _ in range(size)] solved = run_maze( maze, source_row, source_column, destination_row, destination_column, solutions ) @@ -129,7 +129,7 @@ def run_maze( size = len(maze) # Final check point. if i == destination_row and j == destination_column and maze[i][j] == 0: - solutions[i][j] = 1 + solutions[i][j] = 0 return solutions lower_flag = (not i < 0) and (not j < 0) # Check lower bounds @@ -137,10 +137,10 @@ def run_maze( if lower_flag and upper_flag: # check for already visited and block points. - block_flag = (not solutions[i][j]) and (not maze[i][j]) + block_flag = (solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited - solutions[i][j] = 1 + solutions[i][j] = 0 # check for directions if ( @@ -157,7 +157,7 @@ def run_maze( ): return solutions - solutions[i][j] = 0 + solutions[i][j] = 1 return None return None