Matrix
Problem Description
Yifenfei very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.
Every time yifenfei should to do is that choose a detour which frome the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix yifenfei choose. But from the top to the bottom can only choose right and down, from the bottom to the top can only choose left and up. And yifenfei can not pass the same area of the Matrix except the start and end.
Input
The input contains multiple test cases.
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
Output
For each test case output the maximal values yifenfei can get.
Sample Input
2
10 3
5 10
3
10 3 3
2 5 3
6 7 10
5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
Sample Output
28
46
80
Author
yifenfei
Source
ZJFC 2009-3 Programming Contest
题目类型:多线程DP
算法分析:使用dp[i][j][k][q]表示两个点分别到达(i,j)和(k,p)时所获得的最大数,然后直接递推,最后输出dp[n][n][n][n]的值即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 30 + 6; int ans[maxn][maxn], dp[maxn][maxn][maxn][maxn]; int main() { // ifstream cin ("aaa.txt"); int n; while (cin >> n) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> ans[i][j]; memset (dp, 0, sizeof (dp)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n ;j++) for (int k = 1; k <= n; k++) for (int q = 1; q <= n; q++) { int tempa = max (dp[i-1][j][k-1][q], dp[i-1][j][k][q-1]); int tempb = max (dp[i][j-1][k-1][q], dp[i][j-1][k][q-1]); tempa = max (tempa, tempb); if ((i == k) && (j == q)) dp[i][j][k][q] = tempa + ans[i][j]; else dp[i][j][k][q] = tempa + ans[i][j] + ans[k][q]; } cout << dp[n][n][n][n] << endl; } return 0; } |
- « 上一篇:hdu2669
- hdu2815:下一篇 »