Below is some code that sees if there is a path in a maze from the top-left to the bottom right. My question with this code is where we check failedPoints.contains(p)
: how could we ever get true
?
Right before we do if(failedPoints.contains(p))
we are creating a new Point
object. Even if the new Point
object has the same row
and col
values as another Point
in failedPoints
, the objects will be different, so shouldn't failedPoints.contains(p)
always return false
?
boolean getPath(boolean[][] maze, int row, int col, ArrayList<Point> path, HashSet<Point> failedPoints){
/*if out of bounds or not available, return*/
if(col<0 || row<0 || !maze[row][col]){
return false;
}
Point p = new Point(row,col);
/* If we've already visited this cell return*/
if(failedPoints.contains(p)){
return false;
}
boolean isAtOrigin = (row == 0) && (col == 0);
/*If there's a path from start to my current location, add my location.*/
if(isAtOrigin || getPath(maze,row,col -1, path, failedPoints) || getPath(maze,row-1, col, path,failedPoints)){
path.add(p);
return true;
}
failedPoints.add(p); //Cache result
return false;
}