-
Notifications
You must be signed in to change notification settings - Fork 0
/
dp_931.cpp
36 lines (34 loc) · 1010 Bytes
/
dp_931.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "headl.h"
class Solution {
public:
int minFallingPathSum(vector<vector<int>>& matrix) {
int n = matrix.size(), m = matrix[0].size();
vector<vector<int>> ans(n, vector<int>(m));
for (int i = 0; i < m; i++)
{
ans[0][i] = matrix[0][i];
}
for (int i = 1; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (j == 0) ans[i][j] = min(ans[i - 1][j], ans[i - 1][j + 1]);
else if (j == m - 1) ans[i][j] = min(ans[i - 1][j], ans[i - 1][j - 1]);
else
ans[i][j] = min(ans[i - 1][j], min(ans[i - 1][j - 1], ans[i - 1][j + 1])) + matrix[i][j];
}
}
int a = ans[n - 1][0];
for (int i = 1; i < m; i++)
{
a = min(a, ans[n - 1][i]);
}
return a;
}
};
int main()
{
freopen("test_in.txt", "r", stdin);
freopen("test_out.txt", "w", stdout);
Solution* a = new Solution();
}