I have written a short Java code for solving a simple maze problem to go from S to G.
I do not understand where the problem is going wrong.
import java.util.Scanner;
public class tester
{
static char [][] grid={
{'.','.'},
{'.','.'},
{'S','G'},
};
static int a=2;
static int b=2;
static boolean findpath(int x, int y)
{
if((x > grid.length-1) || (y > grid[0].length-1) || (x < 0 || y < 0))
{
return false;
}
else if(x==a && y==b){
return true;
}
else if (findpath(x,y-1) == true){
return true;
}
else if (findpath(x+1,y) == true){
return true;
}
else if (findpath(x,y+1) == true) {
return true;
}
else if (findpath(x-1,y) == true){
return true;
}
return false;
}
public static void main(String[] args){
boolean result=findpath(2,0);
System.out.print(result);
}
}
I am giving the starting position directly and goal is defined in a & b. Do help.