hdu4826---Labyrinth (simple DP)

Source: Internet
Author: User

Problem Description
Degree of the bear is a love to explore the bear, once accidentally fell into a m*n matrix maze, the maze can only from the top left corner of the matrix to start walking, only to go to the top right corner of the first lattice to calculate out of the maze, every time can only walk a lattice, and can only go up and down to the right before the lattice, Every lattice has some gold (or positive or negative, there may be bandits to rob, degree bears can be negative gold coins, the need to write IOUs to robbers), the degree of bear just started when the body gold number 0, asked degree of the bear out of the maze when the maximum number of gold coins?

Input
The first line of input is an integer t (T < 200), which represents a total of T-group data.
Enter two positive integer m,n (m<=100,n<=100) for the first row of each set of data. The next m line, n integers per row, represents the number of coins that can be obtained in the corresponding lattice, each integer greater than or equal to 100 and less than or equal to 100.

Output
For each set of data, you first need to output a separate line, "Case #?:", where the question mark should fill in the current number of data groups, and the number of groups is calculated from 1.
Each set of test data outputs a single line, outputting an integer that represents the maximum number of coins that you can get when you go to the upper-right corner according to the optimal play.

Sample Input

2 3 4 1-1 1 0 2-2 4 2 3 5 1-90 2 2 1 1 1 1

Sample Output

Case #1: #2:4

Source
2014 Baidu Star Program Design Competition-Qualifying Tournament

Recommend
liuyiding | We have carefully selected several similar problems for you:5177 5173 5169 5168 5165

Dp[i][j][0-2] Indicates the optimal value of (I, J) entered from three directions

/************************************************************************* > File Name:hdu4826.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 February 26 Thursday 17:21 46 seconds ******************************** ****************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace STD;Const DoublePI =ACOs(-1);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;intdp[ the][ the][3];intmat[ the][ the];intMain () {intTintIcase =1;scanf("%d", &t); while(t--) {intN, M;scanf("%d%d", &n, &m);memset(DP,-inf,sizeof(DP)); for(inti =1; I <= N; ++i) { for(intj =1; J <= M; ++J) {scanf("%d", &mat[i][j]); }} dp[1][1][0] = dp[1][1][1] = dp[1][1][2] = mat[1][1]; for(inti =2; I <= N; ++i) {dp[i][1][0] = Dp[i-1][1][0] + mat[i][1]; } for(intj =2; J <= M; ++J) { for(inti =1; I <= N; ++i)//Enter from the left{dp[i][j][2] = max (Dp[i][j-1][0], Max (Dp[i][j-1][1], Dp[i][j-1][2])) + mat[i][j]; } for(inti =2; I <= N; ++i) {dp[i][j][0] = max (Dp[i-1][j][0], Dp[i-1][j][2]) + mat[i][j]; } for(inti = n-1; I >=1; I.) {dp[i][j][1] = max (dp[i +1][j][2], Dp[i +1][j][1]) + mat[i][j]; }        }intans = max (dp[1][m][0], Max (dp[1][m][1], dp[1][m][2]));printf("Case #%d:\n", icase++);printf("%d\n", ans); }}

hdu4826---Labyrinth (simple 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.