51nod 1084: Matrix fetch problem V2

Source: Internet
Author: User
Tags time limit

1084 matrix fetch problem V2 base time limit: 2 second space limit: 131072 KB score: 80 Difficulty: 5-level algorithm problem collection concern a m*n matrix has a different positive integer, through this lattice, you can get the reward of the corresponding value, first from the left up to the right, then from the right down to the top left. The 1th time can only go down and to the right, the 2nd time can only go up and left.   Two plays if you pass the same lattice, the reward for that lattice is calculated once and the maximum value can be obtained. For example: 3 * 3 of squares.
1 3 3 2 1 3 2 2 1
The maximum value that can be obtained is: 17. 1, 2, 3, 2, 1, 3, 3 The reward for the starting and ending points is calculated only 1 times. Input

Line 1th: 2 number M N, the middle is separated by a space, the size of the matrix. (2 <= M, N <=)
2-n + 1 Lines: The number of M per line, separated by a space in the middle, corresponding to the value of the reward in the lattice. (1 <= a[i,j] <= 10000)
Output
The maximum value the output can achieve.
Input example
3 3
1 3 3
2 1 3
2 2 1
Output example
17


DP to do, you start to count the number of towers, and then will go through the path of 0, and then take one time, found that this is not right. Example:

1 1 3

3 3 3

3 1 1

And then self-reaction, the problem is actually poj3422http://blog.csdn.net/u010885899/article/details/50330303 sub-problem, here the k=2, with the network flow ran over the results.

In fact here with Dp[k][m][n] represents the maximum value of the number of two people in the walk. Has gone the K-step, the first person to go to the second row, the next person to go to the nth row, through the walk of the K step, know its column number. Then two people walk there are four directions, down, down, right, bottom right, right and left. Take the maximum value again.

For the position equal, judge, take only one value.

Code:

#pragma warning (disable:4996) #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #include <queue> #include <map> using Nam
Espace std;

typedef long Long LL;
int res;

int m, n;
int a[205][205];

int dp[405][205][205];
	void input () {int i, J;

	scanf ("%d%d", &m, &n);
		for (i = 1; I <= n; i++) {for (j = 1; j <= M; j + +) {scanf ("%d", &a[i][j]);

	}}} void Solve () {int I, j, K; for (i = 2; I <= n + M + 1, i++) {for (j = 1, j <= n&&i-j >= 0; j + +) {for (k = 1; k <= N&A Mp;&i-k >= 0;
					k++) {if (j = = k) {Dp[i][j][k] = max (Dp[i][j][k], dp[i-1][j-1][k-1] + a[j][i-j]);//All the way down here.
					Dp[i][j][k] = max (Dp[i][j][k], dp[i-1][j][k-1] + a[j][i-j]);
					Dp[i][j][k] = max (Dp[i][j][k], dp[i-1][j-1][k] + a[j][i-j]); Dp[i][j][k] = max (Dp[i][j][k], dp[i-1][j][k] + A[J][I-J]);
					} else {Dp[i][j][k] = max (Dp[i][j][k], dp[i-1][j-1][k-1] + a[j][i-j] + a[k][i-k]);//All the way down here.
					Dp[i][j][k] = max (Dp[i][j][k], dp[i-1][j][k-1] + a[j][i-j] + a[k][i-k]);
					Dp[i][j][k] = max (Dp[i][j][k], dp[i-1][j-1][k] + a[j][i-j] + a[k][i-k]);
				Dp[i][j][k] = max (Dp[i][j][k], dp[i-1][j][k] + a[j][i-j] + a[k][i-k]);
}}}} cout << dp[n + M + 1][n][n] << Endl;
	} int main () {//freopen ("I.txt", "R", stdin);

	Freopen ("O.txt", "w", stdout);
	Input ();

	Solve ();
return 0; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.