Hi im implementing shortest path maze solver algorithm using breadth first search. when i input large scale puzzle 320x320 it gives exeption error. please help.(Heap space allocated to 2gb)
this is the error,
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at Maze.getPathBFS(Maze.java:58)
at Maze.main(Maze.java:132)
this is part of my code,
//data structure to store matrix cell coordinates
private static class Point {
int x;
int y;
Point parent;
public Point(int x, int y, Point parent) {
this.x = x;
this.y = y;
this.parent = parent;
}
public Point getParent() {
return this.parent;
}
public String toString() {
return "x = " + x + " y = " + y;
}
}
//queue to store cells
public static Queue<Point> path = new LinkedList<>();
// BFS algorithm to acquire the shortest path to destination
public static Point getPathBFS(int x, int y, String[][] graph) {
//start point
path.add(new Point(x, y, null));
while (!path.isEmpty()) {
Point point = path.remove();
//finding destination coordinate
if (graph[point.x][point.y].equals("F")) {
return point;
}
//checking neighbour cells for path and add path to the queue
if (isValid(point.x + 1, point.y, graph)) { //checking down cell is valid to visit
graph[point.x][point.y] = "-1"; // mark reached cell
Point nextP = new Point(point.x + 1, point.y, point);
path.add(nextP);
}
if (isValid(point.x - 1, point.y, graph)) {//checking Up cell is valid to visit
graph[point.x][point.y] = "-1";
Point nextP = new Point(point.x - 1, point.y, point);
path.add(nextP);
}
if (isValid(point.x, point.y + 1, graph)) { //checking Right cell is valid to visit
graph[point.x][point.y] = "-1";
Point nextP = new Point(point.x, point.y + 1, point);
path.add(nextP);
}
if (isValid(point.x, point.y - 1, graph)) { //checking left cell is valid to visit
graph[point.x][point.y] = "-1";
Point nextP = new Point(point.x, point.y - 1, point);
path.add(nextP);
}
}
return null;
}
is there any code change must be done to reduce memory consumption in the code?