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[][]val=new int[n+1][m+1];
  16. // number of paths having even sum
  17. for(int i=1;i<=n;i++){
  18. for(int j=1;j<=m;j++){
  19. val[i][j]=sc.nextInt();
  20. }
  21. }
  22. int out=solve(n,m,val);
  23. System.out.println(out);
  24. }
  25. sc.close();
  26. }
  27. public static int solve(int n,int m,int[][]val){
  28. int [][][]dp=new int[n+1][m+1][3];
  29. if (val[1][1]%2==0)dp[1][1][2] = 1;
  30. else dp[1][1][1]=1;
  31. for(int i=1;i<=n;i++){
  32. for(int j=1;j<=m;j++){
  33. if(i==1&&j==1)continue;
  34. if(val[i][j]%2==0){
  35. dp[i][j][2]=dp[i][j-1][2]+dp[i-1][j][2];
  36. dp[i][j][1]=dp[i][j-1][1]+dp[i-1][j][1];
  37. }else{
  38. dp[i][j][2]=dp[i][j-1][1]+dp[i-1][j][1];
  39. dp[i][j][1]=dp[i][j-1][2]+dp[i-1][j][2];
  40. }
  41. }
  42. }
  43. return dp[n][m][2];
  44. }
  45. }
Success #stdin #stdout 0.18s 56664KB
stdin
2
2 3
1 2 3
6 5 4
3 3
2 4 6
8 6 4
6 2 8
stdout
3
6