码迷,mamicode.com
首页 > 编程语言 > 详细

【TOJ 3242】FatMouse and Java Beans(dp)

时间:2018-05-12 23:55:00      阅读:378      评论:0      收藏:0      [点我收藏+]

标签:when   using   upper   starting   class   eve   family   follow   pre   

描述

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

 

【TOJ 3242】FatMouse and Java Beans(dp)

标签:when   using   upper   starting   class   eve   family   follow   pre   

原文地址:https://www.cnblogs.com/kannyi/p/9030581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!