swjtu 1583 Sample Collections

本文介绍了一个机器人游戏中的算法问题,即机器人如何在一个n×n的网格中从左上角走到右下角,通过两次行走来收集尽可能多的样本。文章提供了一段C++代码实现,利用动态规划解决该问题。

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;
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值