trying to assign default value using Arrays.fill method, but I'm getting ArrayStoreException. Code is given Below "for finding all path from source to destination in 2d array with only k moves"
I went through few links(https://stackoverflow.com/questions/15120089/convert-arraylist-to-array-throws-java-lang-arraystoreexception,https://stackoverflow.com/questions/12369957/dealing-with-an-arraystoreexception), but not able to locate the problem
public class FindPathWithConstaint {
int[][][][] dp = new int[100][100][100][2];
public static void main(String arg[]) {
int m = 3, n = 3, k =2;
FindPathWithConstaint obj = new FindPathWithConstaint();
System.out.println(obj.findAllPath(m,n,k));
}
public int findAllPath(int m, int n, int k) {
if(m==0 && n==0)
return 1;
Arrays.fill(dp,-1);
return countAllPaths(m-1,n,k,1) + countAllPaths(m,n-1,k,0);
}
public int countAllPaths(int i, int j, int k, int d) {
if(i<0 || j<0)
return 0;
if(i==0 && j==0)
return 1;
if(k==0) {
if(d==0 && i==0)
return 1;
if(d==1 && j==0)
return 1;
}
if(dp[i][j][k][d]!=-1)
return dp[i][j][k][d];
if(d==0)
return countAllPaths(i,j-1,k,0) + countAllPaths(i-1,j,k-1,1);
else
return countAllPaths(i-1,j,k,1) + countAllPaths(i,j-1,k-1,0);
}
}