66 * ends are marked with different integers.
77 */
88public final class MazeRecursion {
9+ private static final int OPEN = 0 ;
10+ private static final int PATH = 2 ;
11+ private static final int DEAD_END = 3 ;
12+
13+ private static final int DEFAULT_START_ROW = 1 ;
14+ private static final int DEFAULT_START_COLUMN = 1 ;
15+ private static final int DEFAULT_GOAL_ROW = 6 ;
16+ private static final int DEFAULT_GOAL_COLUMN = 5 ;
17+
18+ // Each entry is {rowDelta, colDelta}
19+ // down -> right -> up -> left
920 private static final int [][] FIRST_STRATEGY_DIRECTIONS = {{1 , 0 }, {0 , 1 }, {-1 , 0 }, {0 , -1 }};
21+ // up -> right -> down -> left
1022 private static final int [][] SECOND_STRATEGY_DIRECTIONS = {{-1 , 0 }, {0 , 1 }, {1 , 0 }, {0 , -1 }};
1123
1224 private MazeRecursion () {
@@ -21,14 +33,7 @@ private MazeRecursion() {
2133 * @throws IllegalArgumentException if the maze is null, empty, or not rectangular
2234 */
2335 public static int [][] solveMazeUsingFirstStrategy (int [][] map ) {
24- validateMaze (map );
25- if (!isInBounds (map , 1 , 1 ) || !isInBounds (map , 6 , 5 )) {
26- return null ;
27- }
28- if (map [6 ][5 ] == 2 ) {
29- return map ;
30- }
31- return solve (map , 1 , 1 , 6 , 5 , FIRST_STRATEGY_DIRECTIONS ) ? map : null ;
36+ return solveWithDefaultEndpoints (map , FIRST_STRATEGY_DIRECTIONS );
3237 }
3338
3439 /**
@@ -40,14 +45,7 @@ public static int[][] solveMazeUsingFirstStrategy(int[][] map) {
4045 * @throws IllegalArgumentException if the maze is null, empty, or not rectangular
4146 */
4247 public static int [][] solveMazeUsingSecondStrategy (int [][] map ) {
43- validateMaze (map );
44- if (!isInBounds (map , 1 , 1 ) || !isInBounds (map , 6 , 5 )) {
45- return null ;
46- }
47- if (map [6 ][5 ] == 2 ) {
48- return map ;
49- }
50- return solve (map , 1 , 1 , 6 , 5 , SECOND_STRATEGY_DIRECTIONS ) ? map : null ;
48+ return solveWithDefaultEndpoints (map , SECOND_STRATEGY_DIRECTIONS );
5149 }
5250
5351 /**
@@ -84,12 +82,23 @@ public static int[][] solveMazeUsingSecondStrategy(int[][] map, int startRow, in
8482 return solve (map , startRow , startColumn , goalRow , goalColumn , SECOND_STRATEGY_DIRECTIONS ) ? map : null ;
8583 }
8684
85+ private static int [][] solveWithDefaultEndpoints (int [][] map , int [][] directions ) {
86+ validateMaze (map );
87+ if (!isInBounds (map , DEFAULT_START_ROW , DEFAULT_START_COLUMN ) || !isInBounds (map , DEFAULT_GOAL_ROW , DEFAULT_GOAL_COLUMN )) {
88+ return null ;
89+ }
90+ if (map [DEFAULT_GOAL_ROW ][DEFAULT_GOAL_COLUMN ] == PATH ) {
91+ return map ;
92+ }
93+ return solve (map , DEFAULT_START_ROW , DEFAULT_START_COLUMN , DEFAULT_GOAL_ROW , DEFAULT_GOAL_COLUMN , directions ) ? map : null ;
94+ }
95+
8796 private static boolean solve (int [][] map , int row , int column , int goalRow , int goalColumn , int [][] directions ) {
88- if (!isInBounds (map , row , column ) || map [row ][column ] != 0 ) {
97+ if (!isInBounds (map , row , column ) || map [row ][column ] != OPEN ) {
8998 return false ;
9099 }
91100
92- map [row ][column ] = 2 ;
101+ map [row ][column ] = PATH ;
93102 if (row == goalRow && column == goalColumn ) {
94103 return true ;
95104 }
@@ -100,7 +109,7 @@ private static boolean solve(int[][] map, int row, int column, int goalRow, int
100109 }
101110 }
102111
103- map [row ][column ] = 3 ;
112+ map [row ][column ] = DEAD_END ;
104113 return false ;
105114 }
106115
0 commit comments