-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backtracking - recursion: finds all possible maze path having some hu…
…rdles
- Loading branch information
1 parent
10c84bc
commit d9cfec3
Showing
1 changed file
with
87 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package Lecture12; | ||
|
||
public class mazePathWithHurdles { | ||
|
||
public static void main(String[] args) { | ||
|
||
int[][] arr = { { 0, 0, 9, 0 }, { 0, 0, 9, 0 }, { 9, 0, 0, 0 }, | ||
{ 0, 9, 0, 0 } }; | ||
// here 9 is the hurdle | ||
System.out.println("given board: "); | ||
displayMatrix(arr); | ||
int currentRow = 0, currentCol = 0, endRow = 3, endCol = 3; | ||
System.out.println("possible paths: \n"); | ||
printMazePathHurdle(arr, endRow, endCol, currentRow, currentCol); | ||
|
||
} | ||
|
||
public static void printMazePathHurdle(int[][] arr, int endRow, int endCol, | ||
int currentRow, int currentCol) { | ||
|
||
if (currentRow == endRow && currentCol == endCol) { | ||
arr[currentRow][currentCol] = 1; | ||
displayMatrix(arr); | ||
return; | ||
} | ||
if (currentRow > endRow | currentCol > endCol) { | ||
return; | ||
} | ||
|
||
if (arr[currentRow][currentCol] == 9) { | ||
return; // back-track to previous cell | ||
} | ||
|
||
arr[currentRow][currentCol] = 1; // set to 1 to indicate the cell is | ||
// traversed | ||
|
||
printMazePathHurdle(arr, endRow, endCol, currentRow, currentCol + 1); // traverse | ||
// horizontally | ||
printMazePathHurdle(arr, endRow, endCol, currentRow + 1, currentCol); // traverse | ||
// vertically | ||
arr[currentRow][currentCol] = 1; | ||
} | ||
|
||
public static void displayMatrix(int[][] arr) { | ||
for (int row = 0; row < arr[0].length; row++) { | ||
for (int col = 0; col < arr[row].length; col++) { | ||
System.out.print(arr[row][col]); | ||
} | ||
System.out.println(); | ||
} | ||
System.out.println("--------------"); | ||
} | ||
} | ||
|
||
/* | ||
output: | ||
given board: | ||
0090 | ||
0090 | ||
9000 | ||
0900 | ||
-------------- | ||
possible paths: | ||
1190 | ||
0190 | ||
9111 | ||
0901 | ||
-------------- | ||
1190 | ||
0190 | ||
9111 | ||
0911 | ||
-------------- | ||
1190 | ||
1190 | ||
9111 | ||
0911 | ||
-------------- | ||
1190 | ||
1190 | ||
9111 | ||
0911 | ||
-------------- | ||
*/ |