Hdu4826 --- Labyrinth (simple dp)

Source: Internet
Author: User

Hdu4826 --- Labyrinth (simple dp)

Problem Description
Du 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 a robber in the Road robbery. The gold coins on Dudu Xiong can be negative, and you need to write an overdue bill to the robbers. At the beginning, Dudu Xiong's gold coins were 0, q: How many gold coins does du Xiong have when he walks out of the maze?

Input
The 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.

Output
For 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 Input

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

Sample Output

Case #1: 18 Case #2: 4

Source
2014 Baidu STAR Program Design Competition-qualifying

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

Set dp [I] [j] [0-2] to indicate the optimal value for entering (I, j) from three directions.

/*************************************** * *********************************> File Name: hdu4826.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: ******************************** **************************************** /# include# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           
             # Include using namespace std; const double pi = acos (-1); const int inf = 0x3f3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair
            
              PLL; int dp [110] [110] [3]; int mat [110] [110]; int main () {int t; int icase = 1; scanf ("% d", & t); while (t --) {int n, m; scanf ("% d", & n, & m ); memset (dp,-inf, sizeof (dp); for (int I = 1; I <= n; ++ I) {for (int j = 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 (int I = 2; I <= n; ++ I) {dp [I] [1] [0] = dp [I-1] [1] [0] + mat [I] [1];} for (int j = 2; j <= m; ++ j) {for (int I = 1; I <= n; ++ I) // enter {dp [I] [j] [2] = max (dp [I] [j-1] [0] from the left, max (dp [I] [j-1] [1], dp [I] [j-1] [2]) + mat [I] [j];} for (int I = 2; I <= n; ++ I) {dp [I] [j] [0] = max (dp [I-1] [j] [0], dp [I-1] [j] [2]) + mat [I] [j] ;}for (int I = n-1; I> = 1; -- I) {dp [I] [j] [1] = max (dp [I + 1] [j] [2], dp [I + 1] [j] [1]) + mat [I] [j] ;}} int ans = max (dp [1] [m] [0], max (dp [1] [m] [1], dp [1] [m] [2]); printf ("Case # % d: \ n", icase ++); printf ("% d \ n ", ans );}}
            
           
          
         
        
       
      
     
    
   

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.