-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #545 from riyarane46/main
added rat in a maze and kth permutation problem in backtracking folder
- Loading branch information
Showing
3 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...ata_Structures/Design_and_Analysis_of_Algorithms/Backtracking/kth_permutation_sequence.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class Solution: | ||
def getPermutation(self, n: int, k: int) -> str: | ||
# Initialize the list of numbers from 1 to n | ||
numbers = list(range(1, n + 1)) | ||
# Initialize the result string | ||
result = [] | ||
# Decrement k by 1 to convert to 0-based index | ||
k -= 1 | ||
|
||
def backtrack(nums, k, path): | ||
if not nums: | ||
# If no numbers left, we've found the kth permutation | ||
return True | ||
|
||
# Calculate the factorial of the remaining numbers | ||
factorial = 1 | ||
for i in range(1, len(nums)): | ||
factorial *= i | ||
|
||
for i in range(len(nums)): | ||
if k >= factorial: | ||
# Skip this number, as it's not part of the kth permutation | ||
k -= factorial | ||
else: | ||
# Add the current number to the path | ||
path.append(str(nums[i])) | ||
# Remove the used number from the list | ||
nums.pop(i) | ||
# Recursively build the rest of the permutation | ||
if backtrack(nums, k, path): | ||
return True | ||
# If not successful, backtrack | ||
nums.insert(i, int(path.pop())) | ||
|
||
# Move to the next number | ||
k %= factorial | ||
|
||
return False | ||
|
||
# Start the backtracking process | ||
backtrack(numbers, k, result) | ||
|
||
# Join the result list into a string and return | ||
return ''.join(result) | ||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
solution = Solution() | ||
n = 4 | ||
k = 9 | ||
print(f"The {k}th permutation of {n} numbers is: {solution.getPermutation(n, k)}") |
75 changes: 75 additions & 0 deletions
75
...ithms_and_Data_Structures/Design_and_Analysis_of_Algorithms/Backtracking/rat_in_a_maze.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
def print_solution(solution): | ||
""" | ||
Print the solution matrix. | ||
""" | ||
for row in solution: | ||
print(row) | ||
|
||
def is_safe(maze, x, y): | ||
""" | ||
Check if the given position (x, y) is safe to move to. | ||
Returns True if the position is within the maze and is not blocked. | ||
""" | ||
n = len(maze) | ||
return 0 <= x < n and 0 <= y < n and maze[x][y] == 1 | ||
|
||
def solve_maze(maze): | ||
""" | ||
Main function to solve the maze using backtracking. | ||
Returns the solution matrix if a path is found, otherwise returns None. | ||
""" | ||
n = len(maze) | ||
# Create a solution matrix initialized with zeros | ||
solution = [[0 for _ in range(n)] for _ in range(n)] | ||
|
||
if solve_maze_util(maze, 0, 0, solution): | ||
return solution | ||
else: | ||
print("No solution exists.") | ||
return None | ||
|
||
def solve_maze_util(maze, x, y, solution): | ||
""" | ||
Recursive utility function to solve the maze. | ||
""" | ||
n = len(maze) | ||
|
||
# Base case: if we've reached the destination | ||
if x == n - 1 and y == n - 1 and maze[x][y] == 1: | ||
solution[x][y] = 1 | ||
return True | ||
|
||
# Check if the current position is safe to move | ||
if is_safe(maze, x, y): | ||
# Mark the current cell as part of the solution path | ||
solution[x][y] = 1 | ||
|
||
# Move forward in x direction | ||
if solve_maze_util(maze, x + 1, y, solution): | ||
return True | ||
|
||
# If moving in x direction doesn't work, move down in y direction | ||
if solve_maze_util(maze, x, y + 1, solution): | ||
return True | ||
|
||
# If none of the above movements work, backtrack | ||
solution[x][y] = 0 | ||
return False | ||
|
||
return False | ||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
# 1 represents open path, 0 represents blocked path | ||
maze = [ | ||
[1, 0, 0, 0], | ||
[1, 1, 0, 1], | ||
[0, 1, 0, 0], | ||
[1, 1, 1, 1] | ||
] | ||
|
||
solution = solve_maze(maze) | ||
|
||
if solution: | ||
print("Solution found:") | ||
print_solution(solution) |