Filthy Rich(动态规划)

来源:互联网 发布:ubuntu文档 编辑:程序博客网 时间:2024/05/29 14:36

Filthy Rich
Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status
Description
They say that in Phrygia, the streets are paved with gold. You’re currently on vacation in Phrygia, and to your astonishment you discover that this is to be taken literally: small heaps of gold are distributed throughout the city. On a certain day, the Phrygians even allow all the tourists to collect as much gold as they can in a limited rectangular area. As it happens, this day is tomorrow, and you decide to become filthy rich on this day. All the other tourists decided the same however, so it’s going to get crowded. Thus, you only have one chance to cross the field. What is the best way to do so?

Given a rectangular map and amounts of gold on every field, determine the maximum amount of gold you can collect when starting in the upper left corner of the map and moving to the adjacent field in the east, south, or south-east in each step, until you end up in the lower right corner.

Input
The input starts with a line containing a single integer, the number of test cases.
Each test case starts with a line, containing the two integers r and c, separated by a space (1 <= r, c <= 1000). This line is followed by r rows, each containing c many integers, separated by a space. These integers tell you how much gold is on each field. The amount of gold never negative.
The maximum amount of gold will always fit in an int.

Output
For each test case, write a line containing “Scenario #i:”, where i is the number of the test case, followed by a line containing the maximum amount of gold you can collect in this test case. Finish each test case with an empty line.

Sample Input
1
3 4
1 10 8 8
0 0 1 8
0 27 0 4

Sample Output
Scenario #1:
42

题意:首行给出T,代表有T组数据。每组数据第一行给出R,C代表接着有R行C列的数据。起点在左上角,终点在右下角,每一步可以向下向右向右下走,输出途中最大数字和。

思路:动态规划,DP[i][j]代表到达(i,j)能获取的最大数字和
状态转移方程
DP[i][j]=max(DP[i][j],DP[i][j-1]+map[i][j]),j-1>=0;
DP[i][j]=max(DP[i][j],DP[i-1][j]+map[i][j]),i-1>=0;
DP[i][j]=max(DP[i][j],DP[i-1][j-1]+map[i][j]),i-1>=0,j-1>=0;

代码

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<math.h>#include<queue>using namespace std;const int maxn=1002;int map[maxn][maxn];int DP[maxn][maxn];int main(){    int T;    scanf("%d",&T);    for(int casen=1; casen<=T; casen++)    {        int R,C;//地图尺寸        scanf("%d%d",&R,&C);        for(int i=0; i<R; i++)        {            getchar();            for(int j=0; j<C; j++)                scanf("%d",&map[i][j]);        }        memset(DP,0,sizeof(DP));        DP[0][0]=map[0][0];        for(int i=0; i<R; i++)            for(int j=0; j<C; j++)            {                if(j-1>=0&&DP[i][j]<DP[i][j-1]+map[i][j])                    DP[i][j]=DP[i][j-1]+map[i][j];                if(i-1>=0&&DP[i][j]<DP[i-1][j]+map[i][j])                    DP[i][j]=DP[i-1][j]+map[i][j];                if(i-1>=0&&j-1>=0&&DP[i][j]<DP[i-1][j-1]+map[i][j])                    DP[i][j]=DP[i-1][j-1]+map[i][j];            }        printf("Scenario #%d:\n",casen);        printf("%d\n\n",DP[R-1][C-1]);//两次换行,此题有毒    }    return 0;}
0 0
原创粉丝点击