fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args)
  11. {
  12. int arr[][] =
  13. {
  14. { 1, 2, 3 },
  15. { 4, 6, 5 },
  16. { 3, 2, 1 }
  17. };
  18. int dp[][][] = new int[3][3][20];
  19. for (int i = 0; i < 3; i++)
  20. for (int j = 0; j < 3; j++)
  21. for (int k = 0; k < 20; k++)
  22. dp[i][j][k] = -1;
  23. System.out.println(countPath(arr, 0, 0, 0, 12, dp));
  24. }
  25.  
  26. public static int countPath(int arr[][], int i, int j, int coinSum, int k, int dp[][][])
  27. {
  28.  
  29. if (i == arr.length - 1 && j == arr.length - 1 && (coinSum + arr[i][j]) == 12)
  30. return 1;
  31. if (i >= arr.length || j >= arr.length)
  32. return 0;
  33. if (dp[i][j][coinSum] != -1)
  34. {
  35. System.out.println("cache hittttt..... !!!!!!");
  36. return dp[i][j][coinSum];
  37. }
  38. dp[i][j][coinSum] =
  39. countPath(arr, i + 1, j, coinSum + arr[i][j], k, dp) + countPath(arr, i, j + 1, coinSum + arr[i][j], k, dp);
  40. return dp[i][j][coinSum];
  41. }
  42. }
Success #stdin #stdout 0.09s 320576KB
stdin
Standard input is empty
stdout
2