forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRatInAMaze.java
92 lines (79 loc) · 2.4 KB
/
RatInAMaze.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
package com.rampatra.backtracking;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 10/18/15
* @time: 10:39 AM
*/
public class RatInAMaze {
/**
* @param i
* @param j
* @param maze
* @return
*/
public static boolean isValidMove(int i, int j, int[][] maze) {
if (i >= 0 && i < maze.length && j >= 0 && j < maze[0].length && maze[i][j] == 1) {
return true;
} else {
return false;
}
}
/**
* @param i
* @param j
* @param xMoves
* @param yMoves
* @param maze
* @param path
* @return
*/
public static boolean isValidPath(int i, int j, int[] xMoves, int[] yMoves, int[][] maze, int[][] path) {
if (i == maze.length - 1 && j == maze[0].length - 1) return true;
int nextI, nextJ;
for (int k = 0; k < xMoves.length; k++) {
nextI = i + xMoves[k];
nextJ = j + yMoves[k];
if (isValidMove(nextI, nextJ, maze)) {
path[nextI][nextJ] = 1;
if (isValidPath(nextI, nextJ, xMoves, yMoves, maze, path)) {
return true;
} else {
path[nextI][nextJ] = 0;
}
}
}
return false;
}
/**
* @param i is the start row
* @param j is the start column
* @param maze is the maze in which a path has to be found (1 denotes rat can traverse and 0 denotes it cannot)
*/
public static void printMazePath(int i, int j, int[][] maze) {
int[] xMoves = {0, 1};
int[] yMoves = {1, 0};
int[][] path = new int[maze.length][maze[0].length];
System.out.println("Maze");
System.out.println("---------------");
print2DMatrix(maze);
System.out.println("---------------");
if (isValidPath(i, j, xMoves, yMoves, maze, path)) {
print2DMatrix(path);
} else {
System.out.println("No escape path found!");
}
}
public static void print2DMatrix(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
System.out.print("[" + array[i][j] + "]");
}
System.out.println();
}
}
public static void main(String[] args) {
printMazePath(0, 0, new int[][]{{1, 1, 1, 1}, {0, 0, 1, 1}});
}
}