This project contains a Go implementation to calculate the minimum cost path in a 2D grid.
The minCost
function calculates the minimum cost to reach a specific cell in a 2D grid from the top-left corner.
cost [][]int
: A 2D slice representing the cost grid.m int
: The row index of the target cell.n int
: The column index of the target cell.
This project contains a Go implementation to calculate the minimum cost path in a 2D grid.
The minCost
function calculates the minimum cost to reach a specific cell in a 2D grid from the top-left corner. Here's a detailed description of how it works:
cost [][]int
: A 2D slice representing the cost grid.m int
: The row index of the target cell.n int
: The column index of the target cell.
-
Base Cases:
- If either
m
orn
is negative, the function returnsmath.MaxInt64
, indicating an invalid path. - If both
m
andn
are zero, the function returns the cost of the top-left cell (cost[0][0]
), as this is the starting point.
- If either
-
Recursive Calculation:
- The function recursively calculates the minimum cost to reach the target cell
(m, n)
by considering three possible previous cells:- The cell directly above
(m-1, n)
. - The cell directly to the left
(m, n-1)
. - The cell diagonally above and to the left
(m-1, n-1)
.
- The cell directly above
- It adds the cost of the current cell (
cost[m][n]
) to the minimum of the costs to reach these three previous cells.
- The function recursively calculates the minimum cost to reach the target cell
- The function returns the minimum cost to reach the cell
(m, n)
from the top-left corner(0, 0)
.