fork(1) download
/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	  public static void main(String[] args)
  {
    int arr[][] =
    {
      { 1, 2, 3 },
      { 4, 6, 5 },
      { 3, 2, 1 }
    };
    int dp[][][] = new int[3][3][20];
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        for (int k = 0; k < 20; k++)
          dp[i][j][k] = -1;
    System.out.println(countPath(arr, 0, 0, 0, 12, dp));
  }

  public static int countPath(int arr[][], int i, int j, int coinSum, int k, int dp[][][])
  {

    if (i == arr.length - 1 && j == arr.length - 1 && (coinSum + arr[i][j]) == 12)
      return 1;
    if (i >= arr.length || j >= arr.length)
      return 0;
    if (dp[i][j][coinSum] != -1)
    {
      System.out.println("cache hittttt..... !!!!!!");
      return dp[i][j][coinSum];
    }
    dp[i][j][coinSum] =
      countPath(arr, i + 1, j, coinSum + arr[i][j], k, dp) + countPath(arr, i, j + 1, coinSum + arr[i][j], k, dp);
    return dp[i][j][coinSum];
  }
}
Success #stdin #stdout 0.09s 320576KB
stdin
Standard input is empty
stdout
2