HDU-2571 Destiny

Passing through the valley means that you are infinitely close to the great demon king Lemon! 
But who would have thought that after beheading some shrimp soldiers and crab generals, yifenfei would face the test of the great labyrinth of fate again. This is another mechanism set up by the demon king, Lemon. You know, no matter who, if they are trapped in the maze for more than 1 hour, they will surely die! 
Poor yifenfei jumped into the maze without hesitation in order to save MM. Let's help the persistent him together! 
The Labyrinth of Fate can be regarded as a two-dimensional grid array, as shown in the following figure: 
 
yifenfei is in the upper left corner at the beginning, of course, the purpose is to reach the location of the big devil in the lower right corner. Each grid in the maze is cursed by the goddess of luck or the devil of pain, so each grid corresponds to a value, and when you go there, you will automatically get the corresponding value. 
Now it is stipulated that yifenfei can only go right or down, and can only go down one space at a time. But if you go to the right, you can go one grid at a time or go to the grid where the number of columns in the row is a multiple of the current column number, that is: if the current grid 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 destroy the demon king Lemon with the greatest confidence, yifenfei hopes to get the greatest luck value in this big labyrinth of fate. 
 
Input input data is first an integer C, which represents the number of groups of test data. 
The first row of each set of test data is two integers n, m, which represent the number of rows and columns respectively (1<=n<=20, 10<=m<=1000); 
followed by n rows of data, each row contains m integers, representing the lucky value K ( |k|<100 ) corresponding to the grid of n rows and m columns. 
Output Please output an integer corresponding to each set of test data, indicating the maximum lucky value that yifenfei can get. Sample Input
1
3 8
9 10 10 10 10 -10 10 10
10 -11 -1 0 2 11 10 -20
-11 -11 10 11 2 10 -10 -10
Sample Output
52


This problem can be solved by using the entry-level dp idea. In the state transition equation, in addition to the next grid and the right grid, a loop is used to find the case with the largest lucky value in the way of jumping through the column multiples.

This question reminds myself of two problems, one is that the data here has negative numbers, so the initialized dp array cannot be initialized to 0, but can be -0x3f3f3f3f, and the function form is memset(dp,-0x3f,sizeof(dp)); Another problem is that because of concerns about the time of cin and cout, when using scanf and printf, PRESENTATION ERROR is caused by forgetting to add \n during printf, and this problem should be kept in mind in the future.

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int dp[22][1004];
int k[22][1004];
intmain()
{
    int C;
    cin>>C;
    while(C--)
    {
        int m,n;
        scanf("%d%d",&m,&n);
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++)
            scanf("%d",&k[i][j]);
        memset(dp,-0x3f,sizeof(dp));
        dp[1][1]=k[1][1];
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++)
            {
                if(i==1&&j==1) continue;
                int x=-0x3f3f3f3f;
                for(int n1=2;n1<=j;n1++)
                    if(j%n1==0&&dp[i][j/n1]>x) x=dp[i][j/n1];//Find the maximum value in dp that will jump directly to the ij position through a multiple of j
                if(i>1&&j>1)
                dp[i][j]=k[i][j]+max(max(dp[i-1][j],dp[i][j-1]),x);
                if(i==1) {dp[i][j]=k[i][j]+max(dp[i][j-1],x);continue;}
                if(j==1) dp[i][j]=k[i][j]+dp[i-1][j];
            }
        printf("%d\n",dp[m][n]);
    }
    return 0;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325474714&siteId=291194637