Baidu Star 1004 Labyrinth

Source: Internet
Author: User

LabyrinthTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1173 Accepted Submission (s): 388


problem DescriptionThe degree of the bear is a mere adventure-loving bear. Once stumbled into a maze of m*n matrix, the maze can only start from the first square in the upper left corner of the matrix, only to go to the top right corner of the first lattice to calculate out of the maze, each time only can walk a lattice, and only can go up and down to the right to walk once did not pass the lattice, each lattice has some gold (or It's possible to meet robbers, the degree of gold in the bear can be negative, need to write IOUs to robbers), the degree of bears start when the number of gold coins is 0, asked degree of the bear out of the maze when the body of the maximum number of gold coins?
InputThe first line of the input is an integer t (T < 200), which means that the group data is owned together.
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 line. 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. First, you need to output a separate line, "Case #?:", where the question mark should be filled in the current number of data groups, the number of groups from 1 to start.


Each set of test data outputs a row. Output An integer that represents the maximum number of coins that you can get when you go to the upper-right corner based on 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 outputcase #1:
18
Case #2:
4

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define INF 0x3f3f3f3f; #define MAXN 110using namespace Std;int dp[maxn][maxn];int ma[maxn][maxn];int n,m;void dp (int c) {for (int i=        1;i<=n;i++) {int temp=dp[i][c-1]+ma[i][c];        Dp[i][c]=max (dp[i][c],temp);            for (int j=i+1;j<=n;j++) {temp+=ma[j][c];        Dp[j][c]=max (dp[j][c],temp);        }} for (int i=n;i>=1;i--) {int temp=dp[i][c-1]+ma[i][c];        Dp[i][c]=max (dp[i][c],temp);            for (int j=i-1;j>=1;j--) {temp+=ma[j][c];        Dp[j][c]=max (dp[j][c],temp);    }}}int Main () {int t;cin>>t;    int c=1;        while (t--) {scanf ("%d%d", &n,&m);            for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) {scanf ("%d", &ma[i][j]);        Dp[i][j]=-inf;        } Dp[1][1]=ma[1][1];            for (int i=2;i<=n;i++)DP[I][1]=DP[I-1][1]+MA[I][1];        for (int j=2;j<=m;j++) DP (j);        printf ("Case #%d:\n", C + +);    printf ("%d\n", Dp[1][m]); } return 0;}


Copyright Notice: Welcome to follow my open source project: HTTPS://GITHUB.COM/SKYQINSC

Baidu Star 1004 Labyrinth

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.