#C3962. Minimum Cost Path in a Grid
Minimum Cost Path in a Grid
Minimum Cost Path in a Grid
You are given a 2D grid of positive integers representing costs. Your task is to compute the minimum cost path from the top-left cell \( (1,1) \) to the bottom-right cell \( (M,N) \). You are only allowed to move either right or down at any step.
The path cost is the sum of all the numbers along the path. The recurrence relation for the minimum cost \( C(i,j) \) is given by:
\( C(i,j) = grid[i][j] + \min(C(i-1,j),\, C(i,j-1)) \)
Note: The grid indices are 1-indexed for explanation purposes. In your implementation, treat them as 0-indexed.
inputFormat
The input consists of multiple test cases. For each test case, the first line contains two integers \( M \) and \( N \) representing the number of rows and columns in the grid. Each of the next \( M \) lines contains \( N \) space-separated integers. The input terminates with a line containing "0 0" which should not be processed.
outputFormat
For each test case, output a single integer: the minimum cost to travel from the top-left cell to the bottom-right cell of the grid. Print each answer on a new line.
## sample3 3
1 3 1
1 5 1
4 2 1
0 0
7
</p>