Skip to content

Instantly share code, notes, and snippets.

@unnisworld
Last active March 21, 2019 00:05
Show Gist options
  • Save unnisworld/f42dadfc008d95c776ee1ab4e98ad578 to your computer and use it in GitHub Desktop.
Save unnisworld/f42dadfc008d95c776ee1ab4e98ad578 to your computer and use it in GitHub Desktop.
Find path in NxN maze - https://www.youtube.com/watch?v=keb6rP07Yqg. This implementation has reversed the role of 0's and 1's. Cells with 1's are occupied and you can travel only through 0's.
public class BackTrackSolution {
static int M = 9;
static int N = 10;
static boolean isValid(int[][] mat, int x, int y, int[][] visitedNodex) {
if ( (x >=0 && x < M) && (y >= 0 && y < N) && mat[x][y] == 0 && visitedNodex[x][y] == 0 ) {
return true;
}
return false;
}
static boolean findShortestPath(int[][] mat, Point curPos, Point dest, int[][] pathMatrix, int[][] visitedNodex) {
int currentX = curPos.x, currentY = curPos.y, destX = dest.x, destY = dest.y;
if (currentX == destX && currentY == destY) {
pathMatrix[currentX][currentY] = 1;
return true;
}
if (isValid(mat, currentX, currentY, visitedNodex)) {
pathMatrix[currentX][currentY] = 1;
visitedNodex[currentX][currentY] = 1;
System.out.println("Moved to position ("+ currentX + ", " + currentY + ")");
if ( findShortestPath(mat, new Point(currentX+1, currentY), new Point(destX, destY), pathMatrix, visitedNodex) ) return true;
if ( findShortestPath(mat, new Point(currentX, currentY+1), new Point(destX, destY), pathMatrix, visitedNodex) ) return true;
if ( findShortestPath(mat, new Point(currentX-1, currentY), new Point(destX, destY), pathMatrix, visitedNodex) ) return true;
if ( findShortestPath(mat, new Point(currentX, currentY-1), new Point(destX, destY), pathMatrix, visitedNodex) ) return true;
pathMatrix[currentX][currentY] = 0;
System.out.println("Backtracked from ("+ currentX + ", " + currentY + ")");
return false;
}
return false;
}
public static void main(String[] args) {
int mat[][] =
{
{ 0, 1, 0, 0, 0, 0, 1, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 1, 0, 1, 0 },
{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 1, 0, 1 },
{ 0, 1, 0, 0, 0, 0, 1, 0, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 0, 1, 0, 0, 0, 0, 1, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 1, 0, 1, 1, 0 }
};
int[][] pathMatrix = new int[M][N];
int[][] visitedNodex = new int[M][N];
findShortestPath(mat, new Point(0, 0), new Point(3,4), pathMatrix, visitedNodex);
for(int i=0;i<pathMatrix.length;i++) {
for(int j=0;j<pathMatrix[i].length;j++) {
System.out.print(pathMatrix[i][j] + ", ");
}
System.out.println();
}
}
static class Point {
Point(int x, int y) {
this.x = x;
this.y = y;
}
private int x;
private int y;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Point))
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment