"TOJ 3242" Fatmouse and Java Beans (DP)

Source: Internet
Author: User

Describe

Fatmouse is lucky enough for it found a rectangular box in a storehouse which contains his favorite Food-javabeans.
The box is splited to r*c cells, and in each cell there be some JavaBeans in it.
The problem is to determine the maximum amount of JavaBeans, Fatmouse can collect when starting in the upper left corn Er 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 .

Input

The input starts with a line containing a single integer, the number of the test cases.
Each test case is starts with a line, containing the integers r and C, separated by a space (1<=r, c<=1000).
Then followed by the R rows, each containing c integers, and separated by a space. These
Integers show how many JavaBeans is there in each cell. The amount of JavaBeans never negative.
The maximum amount of JavaBeans would always fit into an int.

Output

For each test case, write a line containing "Scenario #i:", where I am the number of the the test case,
followed by a line containing the maximum amount of JavaBeans so fatmouse can collect in this test case.
The Finish test case is with a empty line.

Sample input

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

Sample output

Scenario #1:
42

Test instructions

is a two-dimensional array to the bottom right of DP's spicy chicken problem! can only go right and down, each time the value of the path can be added, the maximum value.

#include <bits/stdc++.h>using namespacestd;inta[1005][1005];intMain () {intn,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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.