#K94372. Minimum Cost Path
Minimum Cost Path
Minimum Cost Path
Given an r × c grid where each cell \( grid[i][j] \) represents the cost at that cell, your task is to compute the minimum cost required to travel from the top-left corner to the bottom-right corner. At each step, you may only move either to the right or down.
The recurrence relation used to obtain the minimum cost is given by:
\( dp[i][j] = \min(dp[i-1][j],\; dp[i][j-1]) + grid[i][j] \)
If the grid is empty (i.e. there are no cells to traverse), output \(-1\).
inputFormat
The input is provided via standard input (stdin). The first line contains two integers \( r \) and \( c \) representing the number of rows and columns respectively. If either \( r = 0 \) or \( c = 0 \), the grid is considered empty. Otherwise, the next \( r \) lines each contain \( c \) space-separated integers, representing the cost matrix.
outputFormat
Output the minimum cost to reach the bottom-right corner of the grid from the top-left corner. If the grid is empty, output \(-1\).
## sample3 3
1 3 1
1 5 1
4 2 1
7