swjtu 1583 Sample Collections

Sample Collections

Time Limit:1000MS  Memory Limit:65536K
Total Submit:81 Accepted:20

Description

In a robot game, all the robots will collect as many samples as possible in a n×n squares. Each sample in these squares has a value, but not all the square has samples. A robot will move down or right from left top square named A to the right bottom square named B. On the way from A to B, it can collect the samples as many as possible. And in order to collect enough samples, it can walk two times from A to B. Once the sample is collected, the sample in the square is disappeared.

Input

Input includes a number of test cases. First line of each test case is an integer number n (n≤100), which implies the n×n squares, the following lines is the position of the samples and its value. Each line includes three integers. The first two integers are the position of the sample in the square. Suppose the position of the left top square is 1,1, and the position of the right bottom square is n, n. The last integer in this line is the value of the sample. The last line is 0,0,0, which implies the end of this test case.

Output

Output an integer for each test case, which is the maximum value of the samples the robot has collected. Each output for a test case contains one line.

Sample Input

8
2 3 13
2 6 6
3 5 7
4 4 14
5 2 21
5 6 4
6 3 15
7 2 14
0 0 0

Sample Output

67

Source


水dp,不解释。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int map[105][105];
int dp[205][105][105];

int main()
{
    int i,j,n,x1,y1,x2,y2,x,y,c,k,tag;
    while(scanf("%d",&n)!=EOF)
    {
        memset(map,0,sizeof(map));
        while(1)
        {
            scanf("%d%d%d",&x,&y,&c);
            if (x==0 && y==0 && c==0) break;
            x--,y--;
            map[x][y]=c;
        }
        memset(dp,0,sizeof(dp));
        dp[0][0][0]=map[0][0];
        for (i=1;i<=2*(n-1);i++)
        {
            for (j=0;j<n;j++)
            {
                for (k=0;k<n;k++)
                {
                    x1=j;
                    y1=i-j;
                    x2=k;
                    y2=i-k;
                    if (j==k) tag=map[x1][y1];
                    else tag=map[x1][y1]+map[x2][y2];
                    if (x1>0 && x2>0)
                    {
                        dp[i][j][k]=max(dp[i][j][k],dp[i-1][j-1][k-1]+tag);
                    }
                    if (x1>0 && y1>0)
                    {
                        dp[i][j][k]=max(dp[i][j][k],dp[i-1][j-1][k]+tag);
                    }
                    if (y1>0 && x2>0)
                    {
                        dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k-1]+tag);
                    }
                    if (y1>0 && y2>0)
                    {
                        dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k]+tag);
                    }
                //    printf("%d..%d..%d,..%d\n",i,j,k,dp[i][j][k]);
                }
            }
        }
        printf("%d\n",dp[2*(n-1)][n-1][n-1]);
    }
    return 0;
}


你可能感兴趣的:(c,Collections,Integer,input,each,output)