HDU 2571 Fate (DP)

Source: Internet
Author: User

FateTime limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticeHDU 2571

Description

Passing through the Glen means that the great Lord Lemon 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

The 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

Please output an integer for each set of test data, indicating the maximum lucky value that Yifenfei can get.

Sample Input

13 89 10 10 10 10-10 10 1010-11-1 0 2 11 10-20-11-11 10 11 2 10-10-10

Sample Output

52

The idea is that each cell has a maximum value for this position to the lower-right corner, first figure out the last line (because the last line can only go to the right) and the last column (can only go down), and then from the Devil's upper left corner of the position to start one by one, for these positions, can go the route is a step down, or to the right step, or to the right K times step, The maximum value of these three scenarios is updated with its own value. The last output is 1 rows and 1 columns of values.

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4 5 intMainvoid)6 {7     intc,n,m;8     intdp[ -][1005],max;9 Tenscanf"%d",&c); One      while(C--) A     { -scanf"%d%d",&n,&m); -          for(inti =1; I <= N;i + +) the              for(intj =1; J <= M;j + +) -scanf"%d",&dp[i][j]); -  -          for(inti = m-1; I >=1; I--)//last line +         { -max = Dp[n][i +1];//1 steps to the right. +              for(intK =2; I * k <= m;k + +)//go to the right, K times step. Amax = Max < Dp[n][i * k]? Dp[n][i *K]: Max; atDp[n][i] + =Max; -         } -          for(inti = n-1; I >=1; I--)//last column -Dp[i][m] + = Dp[i +1][M];//you can just go down. -  -          for(inti = n-1; I >=1; I--) in              for(intj = m-1; J >=1; J--) -             { tomax = Dp[i][j +1];//compare three scenarios for the rest of the locations +max = max > Dp[i +1][J]? Max:dp[i +1][j]; -                  for(intK =2; J * k <= m;k + +) themax = max > Dp[i][j * k]? MAX:DP[I][J *K]; *DP[I][J] + =Max; $             }Panax Notoginseng  -  theprintf"%d\n", dp[1][1]); +     } A  the     return    0; +}

HDU 2571 Fate (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.