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 DescriptionDu du Xiong is a bear who loves adventure. He accidentally falls into a m * n matrix maze, which can only start from the first square in the upper left corner of the matrix, only the first grid in the upper-right corner can be used to walk out of the maze. Each grid can only go up and down to the right to go to the grid that has not been traveled before. Each grid has some gold coins (either positive or negative, there may be robbers blocking and robbery,The gold coins on Dudu Xiong can be negative, and they need to be written into arrears to the robbers.) When du Xiong started, his body had 0 gold coins. How many gold coins does Du Xiong have when he walked out of the maze?
InputThe first line of the input is an integer T (T <200), indicating a total of T groups of data.
Enter two positive integers m, n (m <= 100, n <= 100) in the first row of each data group ). In the next m row, n integers in each line represent the number of gold coins in the corresponding grid. Each integer is greater than or equal to-100 and less than or equal to 100.
OutputFor each group of data, you must first output a separate line "Case #? : ", Where the question mark should be filled with the current number of data groups. The number of groups starts from 1.
Each group of test data outputs a row and an integer, which indicates the maximum number of coins you can obtain when you go to the upper right corner Based on the optimal strategy.
Sample Input2
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 
 
  #include 
  
   #include #include 
   
    #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;}
   
  
 


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.