hdu2571 Destiny (Dynamic planning)

Source: Internet
Author: User

Fate

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 23957 Accepted Submission (s): 8296


Problem description through the valley means that the lemon of the great Lord has been infinitely close!
But who can think, Yifenfei in kill some cats, but again face the fate of the maze of the test, this is the Devil lemon set up another organ. You know, whoever, if trapped in the maze for more than 1 hours, will surely die!
Poor Yifenfei in order to save mm, righteousness without return to jump into the maze. Let us help to help the persistent him!
The great maze of destiny can be seen as a two-dimensional grid array, as shown in:

Yifenfei first in the upper left corner, the aim of course is to reach the lower right corner of the great Lord's seat. Every lattice in the maze is cursed by the goddess of luck or the devil, so each grid corresponds to a value, where it automatically gets the corresponding value.
It is now stipulated that Yifenfei can only go to the right or down and only one grid at a time. But if you go to the right, you can walk one block at a time or go to the number of columns that are currently in the number of columns, that is: if the current lattice is (x, y), the next step can be (x+1,y), (x,y+1) or (x,y*k) where k>1.
In order to be able to best grasp the eradication of the Devil Lemon,yifenfei hope to be able to get the greatest fortune in this maze of fate.

Input data is first an integer c, which represents the number of groups of test data.
The first line of each set of test data is two integer n,m, representing the number of rows and columns (1<=n<=20,10<=m<=1000);
followed by n rows of data, each containing M integers representing the lucky value K (|k|<100) for the lattice of the N row M column.

Output match each set of test data with an integer that represents the maximum lucky value that Yifenfei can get.

Sample Input13 89 10 10 10 10-10 10 1010-11-1 0 2 11 10-20-11-11 10 11 2 10-10-10

Sample Output52

Test instructions: From the upper left corner to the lower right corner, each position has a weight, to make the right down to the bottom of the weight and maximum, the output maximum value

The main point: with the dynamic programming, the DP array in each position can be reached at the current maximum value, DP array initially to start to 100, cannot be 0, because there are negative weights, 0 is not small enough

DP[0][1] and Dp[1][0] to initialize to 0, the first position is more special

1#include <bits/stdc++.h>2 using namespacestd;3 inta[ -][1005];4 intdp[ -][1005];5 intMain ()6 {7     intC;8      while(~SCANF ("%d",&c))9     {Ten          while(c--) One         { A             intn,m; -scanf"%d%d",&n,&m); -memset (dp,- -,sizeof(DP)); thedp[0][1]=dp[1][0]=0; -              for(intI=1; i<=n; i++) -             { -                  for(intj=1; j<=m; J + +) +                 { -scanf"%d",&a[i][j]); +                 } A             } at              for(intI=1; i<=n; i++) -             { -                  for(intj=1; j<=m; J + +) -                 { -  -                     intt,maxx=-10001; in                      for(intt=2; t<=j; t++) -                     { to                         if(j%t==0) +                         { -Maxx=max (Maxx,max (dp[i-1][j],dp[i][j/T])); the                         } *  $                     }Panax NotoginsengMaxx=max (Maxx,max (dp[i-1][j],dp[i][j-1])); -  thedp[i][j]=a[i][j]+Maxx; +                 } A             } theprintf"%d\n", Dp[n][m]); +         } -     } $     return 0; $}

hdu2571 Destiny (Dynamic planning)

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.