Quote:How to find the number of path from source to destination which adds upto to a given sum k in a 2d matrix?
This look like some homework, but your main effort is pasting the requirement.
What is the question?
Quote:Use Dynamic programming to solve this.
You are given hint about how to do.
We are not doing your homework, if you want help, you need to show your work and explain problem.
To get you started, you can start with a brute force approach (trying every path 1 by 1), then refine.
Quote:In the above piece of code ,it was getting compiled sucessfully, and the ouput printed is a higer value than what is expected
How is your code handle letters that are not 'x' ?
Looks like your code never goes beyond this point
int countpath(char a, int i,int j,int val)
(i == n-1) &&
j == (n-1)
(a[n-1][n-1] == k) ||
(a[n-2][n-2] == k) ||
(a[n-1][n-2] == k) ||
(a[n-2][n-1] == k)
a = 0;
i = 0 &&
j < n-1 &&
a[i][j+1] != 'x'
return countpath(a, 0, j+1, (a[i][j]+a[i][j+1])-'0');
j = 0 &&
i < n-1 &&
a[i+1][j] != 'x'
return countpath(a, i+1, 0, (a[i][j]+a[i+1][j])-'0');
if ( a[i][j] != 'x' )
return countpath(a, i+1,j+1, (a[i+1][j+1]+a[i+1][j]+a[i][j+1])-'0');
Looks like you should learn the debugger.