1
+ package Lecture12 ;
2
+
3
+ public class mazePathWithHurdles {
4
+
5
+ public static void main (String [] args ) {
6
+
7
+ int [][] arr = { { 0 , 0 , 9 , 0 }, { 0 , 0 , 9 , 0 }, { 9 , 0 , 0 , 0 },
8
+ { 0 , 9 , 0 , 0 } };
9
+ // here 9 is the hurdle
10
+ System .out .println ("given board: " );
11
+ displayMatrix (arr );
12
+ int currentRow = 0 , currentCol = 0 , endRow = 3 , endCol = 3 ;
13
+ System .out .println ("possible paths: \n " );
14
+ printMazePathHurdle (arr , endRow , endCol , currentRow , currentCol );
15
+
16
+ }
17
+
18
+ public static void printMazePathHurdle (int [][] arr , int endRow , int endCol ,
19
+ int currentRow , int currentCol ) {
20
+
21
+ if (currentRow == endRow && currentCol == endCol ) {
22
+ arr [currentRow ][currentCol ] = 1 ;
23
+ displayMatrix (arr );
24
+ return ;
25
+ }
26
+ if (currentRow > endRow | currentCol > endCol ) {
27
+ return ;
28
+ }
29
+
30
+ if (arr [currentRow ][currentCol ] == 9 ) {
31
+ return ; // back-track to previous cell
32
+ }
33
+
34
+ arr [currentRow ][currentCol ] = 1 ; // set to 1 to indicate the cell is
35
+ // traversed
36
+
37
+ printMazePathHurdle (arr , endRow , endCol , currentRow , currentCol + 1 ); // traverse
38
+ // horizontally
39
+ printMazePathHurdle (arr , endRow , endCol , currentRow + 1 , currentCol ); // traverse
40
+ // vertically
41
+ arr [currentRow ][currentCol ] = 1 ;
42
+ }
43
+
44
+ public static void displayMatrix (int [][] arr ) {
45
+ for (int row = 0 ; row < arr [0 ].length ; row ++) {
46
+ for (int col = 0 ; col < arr [row ].length ; col ++) {
47
+ System .out .print (arr [row ][col ]);
48
+ }
49
+ System .out .println ();
50
+ }
51
+ System .out .println ("--------------" );
52
+ }
53
+ }
54
+
55
+ /*
56
+ output:
57
+
58
+ given board:
59
+ 0090
60
+ 0090
61
+ 9000
62
+ 0900
63
+ --------------
64
+ possible paths:
65
+
66
+ 1190
67
+ 0190
68
+ 9111
69
+ 0901
70
+ --------------
71
+ 1190
72
+ 0190
73
+ 9111
74
+ 0911
75
+ --------------
76
+ 1190
77
+ 1190
78
+ 9111
79
+ 0911
80
+ --------------
81
+ 1190
82
+ 1190
83
+ 9111
84
+ 0911
85
+ --------------
86
+
87
+ */
0 commit comments