Can't solve it?
hey guys,
i posted the actual question in other thread but now i'm countering a syntax type of error and i don't know what's wrong.
Those of you who don't know about the actual problem, here it is: I need to find the shortest possible path from one 'X' to another 'X' and 'A's representing the blocks.
Please help? Thanks in advance! =]
Code:
import java.util.*;
publicclass ShortestPathFinder
{
staticchar grid[][] ={
{'.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','X','.','A','.','.','.'},
{'.','.','.','.','A','X','.','.'},
{'.','.','.','.','A','.','.','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.'}
};
staticint sLoc[] =newint[2];
staticint eLoc[] =newint[2];
staticchar block ='A', way ='.';
staticfinalint SPOINT = 1;
staticfinalint EPOINT = 2;
publicstaticvoid main (){
display();
int numShapes = 0;
for(int i = 0; i < grid.length; i++){
for (int j = 0; j < grid[i].length; j++){
if(grid[i][j] =='X'){
numShapes ++;
if ( numShapes == 1){
sLoc[0] = i;
sLoc[1] = j;
//System.out.print(sLoc[0] + " " + sLoc[1]);
}else{
eLoc[0] = i;
eLoc[1] = j;
//System.out.print(loc[2] + " " + loc[3]);
}
}
}
}
int x = sLoc[0], y = sLoc[1];
shortPath (grid, x, y);
System.out.println();
//display();
}
// display the whole grid
publicstaticvoid display(){
for(int i = 0; i < grid.length; i++){
for (int j = 0; j < grid[i].length; j++){
System.out.print(grid[i][j] +" ");
}
System.out.println();
}
}
publicstaticvoid shortPath (char g[][],int r,int c){
g [r][c] = SPOINT;
g [eLoc[0]][eLoc[1]] = EPOINT;
if (r < 7 && !(g[r + 1][c] == EPOINT) && !(g[r + 1][c] == block)){// check downward
shortPath(g, r + 1, c);
}
if (r > 0 && !(g[r - 1][c] == EPOINT) && !(g[r - 1][c] == block)){// check upward
shortPath(g, r - 1, c);
}
if (c < 7 && !(g[r][c + 1] == EPOINT) && !(g[r][c + 1] == block)){// check right
shortPath(g, r, c + 1);
}
if (c > 0 && !(g[r][c - 1] == EPOINT) && !(g[r][c - 1] == block)){// check left
shortPath(g, r, c - 1);
}
}
The error is java.lang.StackOverflowError
I don't know how to fix it?

