Problem 448
Minimum Falling Path Sum
Medium · 20 pts · Matrix, Dynamic Programming
Problem
A falling path through an N x N matrix starts at any cell of the first row and moves down one row at a time; from column j it may go to column j - 1, j or j + 1 of the next row (staying inside the matrix). Find the minimum possible sum of the values along a falling path. Process the rows from top to bottom, keeping for each column the best sum of a path that ends there.
Input
First line: N. Next N lines: N integers each.
Output
Print the minimum falling path sum.
Constraints
1 <= N <= 100; -100 <= value <= 100
Example input
3 2 1 3 6 5 4 7 8 9
Example output
13
Explanation
In the matrix 2 1 3 / 6 5 4 / 7 8 9, the path 1 → 5 → 7 (or 1 → 4 → 8) has sum 13, which is the smallest possible.