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

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

时间: 2024-08-03 13:48:19

【TOJ 3242】FatMouse and Java Beans(dp)的相关文章

【POJ 2029】 Get Many Persimmon Trees(DP)

[POJ 2029] Get Many Persimmon Trees(DP) Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4024   Accepted: 2628 Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in the 18

【Hibernate步步为营】--单向关联一对一映射(一)

上篇文章对多对一的关联映射做了详细的分析,它在实现上可以有两种方式,并且这两种方式实现也很简单,关键是标签<many-to-one>的使用,它分别指明了多端和一端的映射关系,这种映射关系既是对象模型中的聚合关系.接下来继续讨论关联映射. 一.唯一外键 唯一外键说的是数据库表中的每一行的外键唯一对应着另一张表中的主键,也就是说一个表的主键作为另一张表的外键,并且它们之间的关系是唯一的,这种反应到关系模型中如下图所示: 上图的两个实体表,分别为人和身份证,很明显的一个人对应着一个身份证.身份证作为

【UI插件】简单的日历插件(下)—— 学习MVC思想

前言 我们上次写了一个简单的日历插件,但是只是一个半成品,而且做完后发现一些问题,于是我们今天尝试来解决这些问题 PS:距离上次貌似很久了 上次,我们大概遇到哪些问题呢: ① 既然想做一套UI库,那么就应该考虑其它UI库的接入问题 这个意思就是,我们的系统中所有UI插件应该有一些统一行为,我们如果希望统一为所有的插件加一点什么东西,需要有位置可加 这个意味着,可能我们所有的插件需要继承至一个抽象的UI类,并且该类提供了通用的几个事件点 ② 上次做的日历插件虽然说是简单,其耦合还是比较严重的(其实

【Linux学习】Linux的文件权限(一)

Linux操作系统是一个非常优秀的操作系统,同时也是一个多用户.多任务的操作系统.那么这就意味着会有很多的人同时使用同一个操作系统的情况.这时,对于一个用户来说,保护好自己的隐私权就成了一个很关键的问题. ★用户与用户组 "用户与用户组"能很好的解决这个问题,用户可以根据自己的意愿设置自己的文件的权限. ●  文件所有者 文件所有者顾名思义就是拥有文件的用户.如果我的用户下面有一些比较隐私的文件不希望别人看见,我就可以设置文件的权限:只有自己的用户(文件所有者)可以修改或者查看这些文件

【Shader实战】卡通风格的Shader(一)

写在前面 呜,其实很早就看到了这类Shader,实现方法很多,效果也有些许不一样.从这篇开始,陆续学习一下接触到的卡通类型Shader的编写. 本篇的最后效果如下(只有怪物和苹果部分): 本篇文章里指的卡通效果具有如下特点: 简化了模型中使用的颜色 简化光照,使模型具有明确的明暗区域 在模型边缘部分绘制轮廓(也就是描边) 我们再来回顾一下Unity Surface Shader的pipeline.(来源:Unity Gems) 由上图可以看出,我们一共有4个可修改渲染结果的机会(绿色方框中的代码

【华为练习题 】 n位水仙花数(初级)

[华为练习题 ] n位水仙花数(初级) 题目 水仙花数又称阿姆斯特朗数. 水仙花数是指一个n 位数( n≥3 ),它的每个位上的数字的n 次幂之和等于它本身.(例如:1^3 + 5^3 + 3^3 = 153) 求输入的数字是否为水仙花数 解答 #include <iostream> #include <vector> using namespace std; bool isRight(int n){ vector<int> v; int sum = 0, tmp =

【华为练习题 】 字符串的最小周期(中级)

[华为练习题 ] 字符串的最小周期(中级) 题目 如果一个字符串可以由某个长度为k的字符串重复多次得到,我们说该串以k为周期.例如,abcabcabcabc以3为周期(注意,它也可以6和12为周期,结果取最小周期3).字符串的长度小于等于100,由调用者保证. 原型: int GetMinPeriod(char *inputstring); 输入参数: char * inputstring:字符串 返回值: int 字符串最小周期 分析 用指针向后寻找与第一个字符相同的字符,找到之后,验证两字符

【UVA - 10815】Andy&#39;s First Dictionary (set)

Andy's First Dictionary Description 不提英文了 直接上中文大意吧 XY学长刚刚立下了再不过CET就直播xx的flag,为了不真的开启直播模式,XY学长决定好好学习英语.于是他每天都读一篇只包含生词的英语文章,并以自己高达450的智商在一秒钟之内记忆下来. 现在给你一篇XY学长今天要读的文章,请你写一个程序,输出他都学习到了哪些单词.要求:如果文章中有相同的单词,那么仅仅输出一次:而且如果两个单词只有大小写不同,将他们视为相同的单词. Input 测试数据将输入

HDU 2845 Beans (DP)

Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any 1*1 grid. Now you want to eat the beans and collect the qualities, but everyo