Hello there, i've been trying to write a java class to solve the n queens problem using some sort of stacking and recursion, the answers are stored in grids(two dimensionnal arrays), but i've hit a dead wall which is stack overflow for recursion at n=8 (max recursion depth reached 2298) So i've been wondering if there is some way to bypass this dead by doing something complex like allocating more heap space in java(if possible?) or using multi thread(point me out to a tutorial/examples)... or please do advice on how to optimize the code... Thanks in advance
public void resoudre(){
this.gridPile.push(copyGrid(grid));
try{
int row = gridPile.size()-1;
if(gridPile.size()==0)row = 0;
chooseGridSpace(this.grid, locateFirstAvailable(grid, row));
if(gridPile.size() == this.taille){
gridSolutions.push(copyGrid(grid));
grid = gridPile.pop();
boolean erronous = true;
while(erronous){
try{
MakeNextUnavailable(grid, gridPile.size());
erronous = false;
}
catch(UnavailabilityException r1){
try{
grid = gridPile.pop();
}
catch(java.util.EmptyStackException r2){
return;
}
}
}
}
}
catch(InvalidPositionException e1){
this.grid = gridPile.pop();
boolean error = true;
while(error){
try{
MakeNextUnavailable(grid, gridPile.size());
error = false;
}
catch(UnavailabilityException er){
try{
this.grid = gridPile.pop();
}
catch(java.util.EmptyStackException err){
return;
}
}
}
}
catch(java.lang.ArrayIndexOutOfBoundsException e2){
return;
}
this.resoudre();
}
private static void chooseGridSpace(int[][] grid, Position a){
grid[a.getLigne()][a.getColonne()] = 1;
fillNotAvailable(grid, a);
}