A warehouse floor is a grid with rows rows and cols columns. Cell (i, j) has a
restocking cost. A picker starts at the top-left cell (0, 0) and must reach the bottom-right cell
(rows - 1, cols - 1). From any cell (i, j) the picker may move to:
(i, j + 1) (one column right),(i + 1, j) (one row down), or(i + 1, j + 1) (one row down AND one column right, diagonally),as long as the destination cell is within the grid.
Print the minimum possible total restocking cost of a route from (0, 0) to (rows - 1, cols - 1),
summing the cost of every cell visited along the way (including both the start and end cells).
Line 1: two integers rows cols.
Next rows lines: cols space-separated integers each -- the restocking costs of that row's cells.
A single integer: the minimum total restocking cost of a route from the top-left to the bottom-right cell.
Example 1
Input
2 2 1 2 3 4
Expected
5
Explanation
Moving diagonally from (0,0) straight to (1,1) costs 1+4=5, which beats going right-then-down (1+2+4=7) or down-then-right (1+3+4=8), so the answer is 5.
Example 2
Input
1 1 5
Expected
5
Explanation
The grid has just one cell, so the route trivially stays there and the total cost is 5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →