fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Codechef
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. // your code goes here
  10. Scanner sc=new Scanner(System.in);
  11. int t=sc.nextInt();
  12. while(t-->0){
  13. int n=sc.nextInt();
  14. int m=sc.nextInt();
  15. int y=sc.nextInt();
  16. int[][]val=new int[n+1][m+1];
  17. // number of paths having even sum
  18. for(int i=1;i<=n;i++){
  19. for(int j=1;j<=m;j++){
  20. val[i][j]=sc.nextInt();
  21. }
  22. }
  23. int out=solve(n,m,val,y);
  24. System.out.println(out);
  25. }
  26. sc.close();
  27. }
  28. public static int solve(int n,int m,int[][]val,int y){
  29. if(y>2000) return -1; // not possible
  30. int [][][]dp=new int[n+1][m+1][2001];
  31. dp[1][1][val[1][1]]=1;
  32. for(int i=1;i<=n;i++){
  33. for(int j=1;j<=m;j++){
  34. for(int k=0;k<=2000;k++){
  35. if(i==1&&j==1)continue;
  36. int rem=k-val[i][j];
  37. if(i>1&&rem>=0){
  38. dp[i][j][k]+=dp[i-1][j][rem];
  39. }
  40. if(j>1&&rem>=0){
  41. dp[i][j][k]+=dp[i][j-1][rem];
  42. }
  43. }
  44. }
  45. }
  46. return dp[n][m][y];
  47. }
  48. }
Success #stdin #stdout 0.15s 58580KB
stdin
3
2 3 10
1 2 3
6 5 4
3 3 26
2 4 6
8 6 4
6 2 8
3 3 5000
1 2 3
4 5 1
2 1 2
stdout
1
2
-1