其他分享
首页 > 其他分享> > dp---数字三角形模型1--方格取数

dp---数字三角形模型1--方格取数

作者:互联网

 

 

 

 

 

 代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 using namespace std;
 5 const int N = 12;
 6 int g[N][N], dp[2*N][N][N], n;
 7 int main()
 8 {
 9     scanf("%d", &n);
10     int y, x, num;
11     while (cin >> y >> x >> num && (y != 0 || x != 0 || num != 0))
12         g[y][x] = num;
13 
14     for (int k = 1; k <= 2 * n - 1; k++)
15     {
16         for (int i = 1; i <= k; i++)
17         {
18             for (int j = 1; j <= k; j++)
19             {
20                 if (i == j)
21                 {
22                     dp[k][i][j] = max(dp[k - 1][i - 1][j - 1], dp[k][i][j]);
23                     dp[k][i][j] = max(dp[k - 1][i - 1][j], dp[k][i][j]);
24                     dp[k][i][j] = max(dp[k - 1][i][j - 1], dp[k][i][j]);
25                     dp[k][i][j] = max(dp[k - 1][i][j], dp[k][i][j]);
26                     dp[k][i][j] += g[k - i + 1][i];
27                 }
28                 else
29                 {
30                     dp[k][i][j] = max(dp[k - 1][i - 1][j - 1], dp[k][i][j]);
31                     dp[k][i][j] = max(dp[k - 1][i - 1][j], dp[k][i][j]);
32                     dp[k][i][j] = max(dp[k - 1][i][j - 1], dp[k][i][j]);
33                     dp[k][i][j] = max(dp[k - 1][i][j], dp[k][i][j]);
34                     dp[k][i][j] += g[k - i + 1][i]+g[k-j+1][j];
35                 }
36             }
37         }
38     }
39     printf ("%d",dp[2*n-1][n][n]);
40     return 0;
41 }

 

标签:---,12,--,取数,int,num,include,dp
来源: https://www.cnblogs.com/cilinmengye/p/16401993.html