#K3601. Minimum Cost Path in Grid
Minimum Cost Path in Grid
Minimum Cost Path in Grid
You are given a grid with n rows and m columns. Each cell of the grid contains an integer cost. Your task is to find the minimum cost path from the top-left corner (cell (0, 0)) to the bottom-right corner (cell (n-1, m-1)).
The cost of a path is defined as the sum of the costs of all cells visited along the path. You can move to any of the four directly adjacent cells (up, down, left, right) provided they are within the grid boundaries.
This problem can be formulated as finding the shortest path in a weighted grid where the weight of each cell is given by its cost. In mathematical terms, if the cost at cell \((i, j)\) is denoted by \(w_{ij}\), then the cost of a path \(P\) is:
Your task is to compute the minimum possible cost to reach cell \((n-1, m-1)\) from \((0, 0)\).
inputFormat
The input consists of multiple test cases. Each test case is formatted as follows:
- The first line contains two integers
n
andm
representing the number of rows and columns respectively. - This is followed by
n
lines, each containingm
integers representing the grid cells.
The input terminates when a line with two zeros (0 0
) is encountered, which should not be processed.
outputFormat
For each test case, output a single integer in a new line: the minimum cost to travel from the top-left to the bottom-right corner of the grid.
## sample3 3
1 3 1
1 5 1
4 2 1
2 2
1 2
1 1
1 4
5 9 1 2
0 0
7
3
17
</p>