题目链接:https://www.luogu.org/problemnew/show/P1508
好迷的一道题啊,一开始题意没理解,原来起点是最后一行的下方,而不是在最后一行上。顺推比较麻烦,不容易写,我比较懒,改成了倒推,以后看哪个简单就用哪个吧。呃呃,其实这道题和数字三角形很相似,不过每次需要考虑三个方向。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 using namespace std; 6 7 const int mmax = 205, inf = 0x3f3f3f3f; 8 9 int dp[mmax][mmax]; 10 11 int main() { 12 int m, n, ans; 13 scanf("%d%d", &m, &n); 14 memset(dp, -inf, sizeof(dp)); 15 for (int i = 1; i <= m; ++i) 16 for (int j = 1; j <= n; ++j) scanf("%d", &dp[i][j]); 17 for (int i = 2; i <= m; ++i) 18 for (int j = 1; j <= n; ++j) { 19 int mm = max(dp[i - 1][j - 1], max(dp[i - 1][j], dp[i - 1][j + 1])); 20 dp[i][j] += mm; 21 } 22 ans = max(dp[m][n / 2], max(dp[m][n / 2 + 1], dp[m][n / 2 + 2])); 23 printf("%d", ans); 24 return 0; 25 }