HDU-ACM Steps Fate

Source: Internet
Author: User

/* indicates just touch DP. This is my contact with the 3rd DP problem, just beginning to think is to use DFS+DP, later stack overflow ... Think carefully,

In fact, the problem and the same as the tower, as long as each step to get the optimal sub-structure, the final result must be optimal. The initialization of the topic should be done well, so

Can avoid a lot of trouble * *

#include "iostream"
#include "stdio.h"
#include "algorithm"
#include "string.h"
#include "Cmath"
#include "queue"
#include "Stdlib.h"
#define INF-32766
using namespace Std;
int n,m;
int dp[25][1005];
void Init ()
{
for (int i=0;i<=n;i++) {dp[0][i]=inf;dp[i][0]=inf;}
dp[1][0]=dp[0][1]=0;
}
int max (int x,int y)
{
Return x>y?x:y;
}
int main ()
{
int t;
cin>>t;
while (t--)
{
cin>>n>>m;
int i,j,k;
Init ();
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
cin>>dp[i][j];
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
{
int ans=inf;
for (k=1;k<j;k++)
{
if (j%k==0) Ans=max (Ans,dp[i][k]);
}
Ans=max (Ans,dp[i][j-1]);
Dp[i][j]+=max (Ans,dp[i-1][j]); Optimal sub-structure corresponding to optimal results
}
cout<<dp[n][m]<<endl;
}
return 0;
}

HDU-ACM Steps Fate

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.