First of all, your question has some errors.
" I have to start at location (0,0) and I can either move up or move right.". I think it should be down or right.
That is from (i,j)
you can move to (i,j+1)
and (i+1,j)
.
The recursive formula will be
minCost(m, n) = min (minCost(m-1, n-1), minCost(m-1, n), minCost(m, n-1)) + cost[m][n]
The time complexity for this will be exponential
as same sub problem is computed many times and this will continue for each cell.
To cover up on time complexity, you can do this using dynamic programming.
Create a temp
table in a bottom up manner to store the results of the sub problem so that we do not need to compute again. In simple terms i,j
index of the temp table will store the minimum cost path from 0,0
to i,j.
The time complexity is reduced to O(nm)
. I can't give the whole code to you as it seems to be a homework question.
On your second question which is to find the total paths. There will be only one minimum cost path. Or if you want to find all the paths.