题目传送:传纸条
思路:双线程DP,具体看代码;
AC代码:
#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define INF 0x7fffffff using namespace std; int n, m; int a[55][55]; int dp[105][55][55];//dp[k][i][j]表示能走到第k个斜线上第i个数和第j个数的最大值 int main() { scanf("%d %d", &n, &m); for(int i = 0; i <= n + 1; i ++) {//赋无关的值为负无穷 for(int j = 0; j <= m + 1; j ++) { a[i][j] = -INF; } } for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) { scanf("%d", &a[i][j]); } } int len = (n + m - 1); for(int k = 2; k <= len; k ++) { for(int i = 1; i < k; i ++) { for(int j = i + 1; j <= k; j ++) { dp[k][i][j] = a[k-i+1][i] + a[k-j+1][j] + max(max(dp[k-1][i-1][j-1], dp[k-1][i-1][j]), max(dp[k-1][i][j-1], dp[k-1][i][j])); } } } printf("%d\n", dp[len-1][m-1][m]); return 0; }
时间: 2024-10-12 15:32:30