【TOJ 3242】FatMouse and Java Beans(dp)

描述

FatMouse is lucky enough for it found a rectangular box in a storehouse which contains his favorite food-JavaBeans.
The box is splited into r*c cells, and in each cell there are some JavaBeans in it.
The problem is to determine the maximum amount of JavaBeans that FatMouse can collect when starting in the upper left corner of the box
and moving to the adjacent field in the east, south, or south-east in each step, until it end up in the lower right corner.

输入

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).
Then followed by r rows, each containing c integers, separated by a space. These
integers show how many JavaBeans are there in each cell. The amount of JavaBeans never negative.
The maximum amount of JavaBeans will always fit in an int.

输出

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 JavaBeans that FatMouse can collect in this test case.
Finish each test case with an empty line.

样例输入

1
3 4
1 10 8 8
0 0 1 8
0 27 0 4

样例输出

Scenario #1:
42

题意

就是一个往二维数组右下角dp的辣鸡题!只能往右和往下走,每次路径的值都可以相加,求最大值。

#include <bits/stdc++.h>
using namespace std;
int a[1005][1005];
int main()
{
    int n,m,i,j,t,num=0;
    cin>>t;
    while(t--)
    {
        num++;
        cin>>n>>m;
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++) 
                scanf("%d",&a[i][j]);
                
        for(i=1;i<=m;i++) 
            a[1][i]=a[1][i]+a[1][i-1];
        for(i=1;i<=n;i++)
            a[i][1]=a[i][1]+a[i-1][1];
            
        for(i=2;i<=n;i++)
            for(j=2;j<=m;j++)
                a[i][j]=max(a[i][j-1],a[i-1][j])+a[i][j];

        cout<<"Scenario #"<<num<<":"<<endl;
        cout<<a[n][m]<<endl<<endl;
    }
}

猜你喜欢

转载自www.cnblogs.com/kannyi/p/9030581.html
今日推荐