Dynamic programming · 11 / 15 Exercise Hard

Minimum cost path

Objective

In a grid with a cost on each square you go from the top left corner to the bottom right one moving only right or down. The best cost of a square is its cost plus the smaller of the best costs of the square above and the one to the left.

Instructions

  • The first line has rows f and columns c; then f lines with c integers.
  • Show Minimum cost: x with the minimum sum of costs of the path (including both corners).
Input
3 3
1 3 1
1 5 1
4 2 1
Output
Minimum cost: 7

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise