-
Notifications
You must be signed in to change notification settings - Fork 5
/
RatInMaze.java
71 lines (61 loc) · 2.42 KB
/
RatInMaze.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*https://practice.geeksforgeeks.org/problems/rat-in-a-maze-problem/1*/
class Solution {
static ArrayList<String> list;
static String currPath;
static int n;
public static boolean isSafe(int r, int c, int[][] maze, boolean[][] visited) {
//if boundary conditions violate, return false
if (r == -1 || r == n || c == -1 || c == n)
return false;
//if the move is unsafe, return false
if (visited[r][c] || maze[r][c] == 0)
return false;
//return true
return true;
}
public static void addPaths(int r, int c, int[][] maze, boolean[][] visited) {
//if conditions violate, return
if (r == -1 || c == -1 || r == n || c == n || maze[r][c] == 0) return;
//if board completed, add solution
if (r == n-1 && c == n-1)
{
list.add(currPath);
}
visited[r][c] = true; //backtracking step-1
//move in all four directions
if (isSafe(r+1,c,maze,visited))
{
currPath += "D"; //backtracking step - 1
addPaths(r+1,c,maze,visited); //backtracking step - 2
currPath = currPath.substring(0,currPath.length()-1); //backtracking step - 3
}
if (isSafe(r,c+1,maze,visited))
{
currPath += "R"; //backtracking step - 1
addPaths(r,c+1,maze,visited); //backtracking step - 2
currPath = currPath.substring(0,currPath.length()-1); //backtracking step - 3
}
if (isSafe(r-1,c,maze,visited))
{
currPath += "U"; //backtracking step - 1
addPaths(r-1,c,maze,visited); //backtracking step - 2
currPath = currPath.substring(0,currPath.length()-1); //backtracking step - 3
}
if (isSafe(r,c-1,maze,visited))
{
currPath += "L"; //backtracking step - 1
addPaths(r,c-1,maze,visited); //backtracking step - 2
currPath = currPath.substring(0,currPath.length()-1); //backtracking step - 3
}
visited[r][c] = false; //backtracking step - 3
}
public static ArrayList<String> findPath(int[][] m, int N) {
n = N;
list = new ArrayList<String>();
currPath = "";
boolean[][] visited = new boolean[n][n];
addPaths(0, 0, m, visited);
Collections.sort(list);
return list;
}
}