Walk on the grid (score 50 points): You are in the N-dimensional grid in position (x_1,x2,...,x_N) . Grid sizes (D_1,D_2,...D_N) . In one step, you can go one step forward or backward in any of the N dimensions. (Thus, there are always 2N possible different moves). How many ways can you take M steps so that you do not leave the grid at any point? You leave the grid if you are for any x_i , or x_i <= 0 or x_i > D_i .
Input: The first line contains the number of test cases T T test cases follow. For each test case, the first line contains N and M , the second line contains x_1,x_2...,x_N , and the third line contains D_1,D_2,...,D_N .
So, in the solution above, I'm trying to take one dimensional array. The website claims 38753340 is the answer, but I am not getting it.
public class GridWalking { public static void main(String[] args) { try { long arr[] = new long[78]; long pos = 44; long totake = 287; Double val = calculate(arr, pos, totake); System.out.println(val % 1000000007); } catch (Exception e) { System.out.println(e); e.printStackTrace(); } } public static HashMap<String, Double> calculated = new HashMap<String, Double>(); private static Double calculate(long[] arr, long pos, long totake) { if (calculated.containsKey(pos + "" + totake)) { return calculated.get(pos + "" + totake); } if (0 == totake) { calculated.put(pos + "" + totake, new Double(1)); return new Double(1); } if (pos == arr.length - 1) { Double b = calculate(arr, pos - 1, totake - 1); Double ret = b; calculated.put(pos + "" + totake, new Double(ret)); return ret; } if (pos == 0) { Double b = calculate(arr, pos + 1, totake - 1); Double ret = b; calculated.put(pos + "" + totake, new Double(ret)); return ret; } Double a = calculate(arr, pos + 1, totake - 1); Double b = calculate(arr, pos - 1, totake - 1); Double ret = (a + b); calculated.put(pos + "" + totake, ret); return ret; } }