Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d4eb6d6

Browse files
authored
Merge pull request #86 from ayushs1ngh/master
Added a competitive coding problem Rat in a Maze
2 parents 714ce2f + a8c0eff commit d4eb6d6

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Rat In A Maze
2+
3+
**Problem from GeeksForGeeks**
4+
A Maze is given as N*N binary matrix of blocks where source block is the upper left most block i.e., maze[0][0] and destination block is lower rightmost block i.e., maze[N-1][N-1]. A rat starts from source and has to reach the destination. The rat can move only in two directions: forward and down.
5+
In the maze matrix, 0 means the block is a dead end and 1 means the block can be used in the path from source to destination. Note that this is a simple version of the typical Maze problem. For example, a more complex version can be that the rat can move in 4 directions and a more complex version can be with a limited number of moves.
6+
7+
**Algorithm:**
8+
If destination is reached
9+
print the solution matrix
10+
Else
11+
a) Mark current cell in solution matrix as 1.
12+
b) Move forward in the horizontal direction and recursively check if this
13+
move leads to a solution.
14+
c) If the move chosen in the above step doesn't lead to a solution
15+
then move down and check if this move leads to a solution.
16+
d) If none of the above solutions works then unmark this cell as 0
17+
(BACKTRACK) and return false.
18+
19+
20+
### Example:
21+
22+
**Input** :
23+
```java
24+
maze[][] = { { 1, 0, 0, 0 },
25+
{ 1, 1, 0, 1 },
26+
{ 0, 1, 0, 0 },
27+
{ 1, 1, 1, 1 } }
28+
```
29+
30+
**Output:** :
31+
```java
32+
The 1 values show the path of rat.
33+
1 0 0 0
34+
1 1 0 0
35+
0 1 0 0
36+
0 1 1 1
37+
```
38+
39+
40+
### Complexity Analysis:
41+
42+
**Time Complexity:** O(2^(n^2)).
43+
The recursion can run upperbound 2^(n^2) times.
44+
45+
**Space Complexity:** O(n^2).
46+
Output matrix is required so an extra space of size n*n is needed.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Java program to solve Rat in a Maze problem using backtracking */
2+
3+
public class RatMaze {
4+
5+
// Size of the maze
6+
static int N;
7+
8+
/* A utility function to print solution matrix sol[N][N] */
9+
void printSolution(int sol[][])
10+
{
11+
for (int i = 0; i < N; i++) {
12+
for (int j = 0; j < N; j++)
13+
System.out.print(
14+
" " + sol[i][j] + " ");
15+
System.out.println();
16+
}
17+
}
18+
19+
/* A utility function to check if x, y is valid index for N*N maze */
20+
boolean isSafe(
21+
int maze[][], int x, int y)
22+
{
23+
// if (x, y outside maze) return false
24+
return (x >= 0 && x < N && y >= 0
25+
&& y < N && maze[x][y] == 1);
26+
}
27+
28+
/* This function solves the Maze problem using Backtracking. It mainly uses solveMazeUtil() to solve the problem. It returns false if no path is possible, otherwise return true and
29+
prints the path in the form of 1s. Please note that there may be more than one solutions, this function prints one of the feasible solutions.*/
30+
boolean solveMaze(int maze[][])
31+
{
32+
int sol[][] = new int[N][N];
33+
34+
if (solveMazeUtil(maze, 0, 0, sol) == false) {
35+
System.out.print("Solution doesn't exist");
36+
return false;
37+
}
38+
39+
printSolution(sol);
40+
return true;
41+
}
42+
43+
/* A recursive utility function to solve Maze problem */
44+
boolean solveMazeUtil(int maze[][], int x, int y,
45+
int sol[][])
46+
{
47+
// if (x, y is goal) return true
48+
if (x == N - 1 && y == N - 1
49+
&& maze[x][y] == 1) {
50+
sol[x][y] = 1;
51+
return true;
52+
}
53+
54+
// Check if maze[x][y] is valid
55+
if (isSafe(maze, x, y) == true) {
56+
// mark x, y as part of solution path
57+
sol[x][y] = 1;
58+
59+
/* Move forward in x direction */
60+
if (solveMazeUtil(maze, x + 1, y, sol))
61+
return true;
62+
63+
/* If moving in x direction doesn't give solution then Move down in y direction */
64+
if (solveMazeUtil(maze, x, y + 1, sol))
65+
return true;
66+
67+
/* If none of the above movements works then BACKTRACK: unmark x, y as part of solution path */
68+
sol[x][y] = 0;
69+
return false;
70+
}
71+
72+
return false;
73+
}
74+
75+
public static void main(String args[])
76+
{
77+
RatMaze rat = new RatMaze();
78+
int maze[][] = { { 1, 0, 0, 0 },
79+
{ 1, 1, 0, 1 },
80+
{ 0, 1, 0, 0 },
81+
{ 1, 1, 1, 1 } };
82+
83+
N = maze.length;
84+
rat.solveMaze(maze);
85+
}
86+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /