-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathBlockedMaze.java
164 lines (133 loc) · 3.21 KB
/
BlockedMaze.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
Given a Maze of dimensions M*N in which X represents blocked
path and - represents open path. It is required to generate
all the possible pathways to reach from top-left corner to
bottom-right corner. Movement is allowed in all the four
directions. The code will generate all the possible pathways
where * will represent the path of the movement.
*/
import java.util.Scanner;
public class BlockedMaze {
private static void display(char[][] maze, int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(maze[i][j] + " ");
}
System.out.println();
}
}
private static boolean isSafe(char[][] maze, int i, int j, int m, int n, boolean[][] visited) {
if (i < 0 || j < 0 || i >= m || j >= n) {
return false;
}
if (visited[i][j] == true || maze[i][j] == 'X') {
return false;
}
return true;
}
private static void blockedMaze(char[][] maze, int i, int j, int m, int n, boolean[][] visited) {
if (i == m - 1 && j == n - 1) {
display(maze, m, n);
System.out.println();
return;
} else {
// Move in up direction
if (isSafe(maze, i - 1, j, m, n, visited)) {
visited[i - 1][j] = true;
maze[i - 1][j] = '*';
blockedMaze(maze, i - 1, j, m, n, visited);
visited[i - 1][j] = false;
maze[i - 1][j] = '-';
}
// Move in left direction
if (isSafe(maze, i, j - 1, m, n, visited)) {
visited[i][j - 1] = true;
maze[i][j - 1] = '*';
blockedMaze(maze, i, j - 1, m, n, visited);
visited[i][j - 1] = false;
maze[i][j - 1] = '-';
}
// Move in down direction
if (isSafe(maze, i + 1, j, m, n, visited)) {
visited[i + 1][j] = true;
maze[i + 1][j] = '*';
blockedMaze(maze, i + 1, j, m, n, visited);
visited[i + 1][j] = false;
maze[i + 1][j] = '-';
}
// Move in right direction
if (isSafe(maze, i, j + 1, m, n, visited)) {
visited[i][j + 1] = true;
maze[i][j + 1] = '*';
blockedMaze(maze, i, j + 1, m, n, visited);
visited[i][j + 1] = false;
maze[i][j + 1] = '-';
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of Maze - ");
int m = sc.nextInt();
int n = sc.nextInt();
char[][] maze = new char[m][n];
System.out.println ("Draw the Maze - ");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
maze[i][j] = sc.next().charAt(0);
}
}
// the matrix visited will keep the track
// of the path we are visiting to prevent
// from looping over the same path again.
boolean[][] visited = new boolean[m][n];
maze[0][0] = '*';
visited[0][0] = true;
System.out.println ("Possible pathways - ");
blockedMaze(maze, 0, 0, m, n, visited);
}
}
/**
Time Complexity : O(4^(m*n))
Space Complexity : O(m*n)
Input :
Enter size of Maze - 5 5
Draw the Maze -
- X - - -
- - - X -
X - X - -
- - - - -
X - - X -
Output :
Possible pathways -
* X - - -
* * - X -
X * X * *
- * * * *
X * * X *
* X - - -
* * - X -
X * X - -
- * * * *
X * * X *
* X - - -
* * - X -
X * X * *
- * * * *
X - - X *
* X - - -
* * - X -
X * X - -
- * * * *
X - - X *
* X * * *
* * * X *
X - X * *
- - - * *
X - - X *
* X * * *
* * * X *
X - X - *
- - - - *
X - - X *
*/