Leetcode-931 Minimum Falling Path Sum(下降路径最小和)

 1 class Solution
 2 {
 3     public:
 4         int minFallingPathSum(vector<vector<int>>& A)
 5         {
 6             vector<vector<int>> B = A;
 7             for(int i = 0;i < A.size();i ++)
 8             {
 9                 for(int j = 0;j < A[i].size();j ++)
10                 {
11                     B[i][j] = A[j][i];
12                 }
13             }
14             
15             vector<vector<int>> dp = B;
16             for(int i = B.size()-2;i >= 0;i --)
17             {
18                 for(int j = 0;j < B.size();j ++)
19                 {
20                     if(j==0)
21                     {
22                         dp[j][i] = min(dp[j][i+1],dp[j+1][i+1]) + B[j][i];
23                     }
24                     else if(j==B.size()-1)
25                     {
26                         dp[j][i] = min(dp[j][i+1],dp[j-1][i+1]) + B[j][i];
27                     }
28                     else
29                     {
30                         dp[j][i] = min(dp[j][i+1],min(dp[j+1][i+1],dp[j-1][i+1])) + B[j][i];
31                     }
32                 }
33             }
34             
35             int result = 393939;
36             for(int i = 0;i < dp.size();i ++)
37             {
38                 if(dp[i][0] < result)
39                     result = dp[i][0];
40             }
41             return result;
42         }
43 };

 

posted @ 2018-10-31 19:19  Asurudo  阅读(213)  评论(0编辑  收藏  举报