/* http://acm.nyist.net/JudgeOnline/problem.php?pid=61 传纸条 http://acm.nyist.net/JudgeOnline/problem.php?pid=712 探 寻 宝 藏 题意:给一个矩阵,求两条不相交的线从左上角到右下角经过的元素的最大和 双线dp - 即同时考虑两条不相交的线,使其线上的和最大 显然我们需要记录每一步时两个线同时往前走的位置(这样可以容易的控制其不相交). 状态:dp[k,(x1,y1),(x2,y2)] 在第k步,双线里一线在(x1,y1) 二线在(x2,y2) 的最大和 转移:dp[k,(x1,y1),(x2,y2)] = max(dp[k-1,(x1-1,y1),(x2-1,y2)], dp[k-1,(x1,y1-1),(x2-1,y2)], dp[k-1,(x1-1,y1),(x2,y2-1)], dp[k-1,(x1,y1-1),(x2,y2-1)]) + map[x1][y1] + map[x2][y2]; */ #include <iostream> #include <cstring> #include <cstdio> using namespace std; template <typename _T> inline _T Max(_T a , _T b){ return (a<b)?(b):(a); } template <typename _T> inline _T Max(_T a , _T b , _T c , _T d){ return (Max(a,b) < Max(c,d)) ? (Max(c,d)) : (Max(a,b)); } const int M = 52; int map[M][M]; int dp[M+M][M][M]; // dp[k][i][j]: 第k步时,双线里 一线在i位置(横坐标为i,纵坐标为k-i) 另一线在j位置(横坐标为j,纵坐标为k-j)的最大和 int main() { freopen("in.txt","r",stdin); int T;cin >> T; while(T--){ int row , col; cin >> row >> col; memset(dp,0,sizeof(dp)); for(int i = 1 ; i <= row ; i++) for(int j = 1 ; j <= col ; j++) scanf("%d" , &map[i][j]); int beg = map[1][1]; int end = map[row][col]; map[1][1] = map[row][col] = 0; int bound = col + row; for(int k = 1 ; k <= bound ; k++){ // 起点和终点为唯一交叉处,特判 for(int x1 = 1 ; x1 <= row ; x1++){ for(int x2 = 1 ; x2 <= row ; x2++){ if (x1 == x2 && !(k==bound && x1==row))continue; if ( !(1<=x1 && x1<=row) || !(1<=x2 && x2<=row))continue; int y1 = k - x1; int y2 = k - x2; if ( !(1<=y1 && y1<=col) || !(1<=y2 && y2<=col))continue; dp[k][x1][x2] = Max(dp[k-1][x1-1][x2-1], dp[k-1][ x1 ][x2-1], dp[k-1][x1-1][ x2 ], dp[k-1][ x1 ][ x2 ]); dp[k][x1][x2] += map[x1][y1] + map[x2][y2]; } } } cout << beg + dp[bound][row][row] + end << endl; } return 0; }