Number of luogu1004 squares (question 4th of the NOIP2000 Improvement Group)
Time limit 1000MS/128MB
Topic description
With N*n squares (n<=9), we fill in some squares with positive integers, while the other squares
Man number 0. As shown in the following illustration (see sample):
A
0 0 0 0 0
0 0 0 0 0 0 0 6 0 0 0 0 0 0 7 0
0 0 0 0 0/0 0 0 0 0 21 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0-0. B
Someone starts at point A in the upper-left corner of the graph, can walk down, or go right until you reach the B in the lower right corner.
Point. On the way, he can take the number in the square (the box will become the number 0 after taking away).
The man walked two times from point A to point B, trying to find 2 of these paths, making the sum of the numbers maximum. Input output Format input format:
The first behavior of the input is an integer n (a n*n graph), followed by three integers per line, and the first two
Represents the position, and the third number is the number placed on that position. A single line of 0 represents the end of the input. Output format:
Simply output an integer representing the largest and most obtained on the 2 path. Input and Output sample input sample #:
8
2 3 2 6 6 3 5 7 4 4 5 2 5 6 4 6 (3) 7
0
0
Output Sample #:
67
Code
#include <iostream>
#include <algorithm>
using namespace std;
const int N =;
int a[n][n],f[2*n][n][n];
int main () {
int n,x,y,z;
cin>>n;
while (cin>>x>>y>>z)
if (!x &&!y &&!z) a break;
else a[x][y]=z;
F[2][1][1] = a[1][1];
for (int k=3; k<=2*n; k++) for (int
x1=1; x1<=n; x1++) for
(int x2=1; x2<=n; x2++) {
if (x1>1 &A mp;& x2>1) //
F[k][x1][x2]=max (f[k][x1][x2],f[k-1][x1-1][x2-1);
if (k-x1>1 && k-x2>1) //Zo
F[k][x1][x2]=max (f[k][x1][x2],f[k-1][x1][x2));
if (x1>1 && k-x2>1) //upper left
F[k][x1][x2]=max (f[k][x1][x2],f[k-1][x1-1][x2));
if (k-x1>1 && x2>1) ///upper left
F[k][x1][x2]=max (f[k][x1][x2],f[k-1][x1][x2-1));
if (X1==X2) f[k][x1][x2]+=a[x1][k-x1];
else f[k][x1][x2]+=a[x1][k-x1]+a[x2][k-x2];
}
cout<<f[2*n][n][n]<<endl;
return 0;
}