Discover Treasures---dual-line DP

Source: Internet
Author: User

two-line DP, in Zheng Light when the two-line DP, which is multidimensional DP should be relatively simple, but that time complexity of the optimization always do not understand. Just attach the code, and then come back and add some explanations.
#include <stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<queue>#include<vector>#include<Set>#include<stack>#include<string>#include<sstream>#include<map>#include<cctype>#include<limits.h>using namespacestd;intmap1[ -][ -],m,n,dp[ -][ -];intGetmax () {inti,j,k;  for(i=1; i<=m+n-1;i++)//In this case the sum of the number of rows running below    {         for(j=m-1; j>=0; j--)//several lines are routed down several lines.        {             for(k=m-1; k>j;k--)//start from a low line.             {                if(i>=k&&i>=j) Dp[j][k]=max (Max (dp[j][k],dp[j-1][k-1]), Max (dp[j-1][k],dp[j][k-1])) +map1[j][i-j]+map1[k][i-K]; }        }    }    returndp[m-2][m-1];}intMain () {intN; scanf ("%d", &n);//Number of cases     while(n--) {memset (MAP1,0,sizeof(MAP1)); Memset (DP,0,sizeof(DP)); scanf ("%d%d", &m,&n);//input length and width        inti,j;  for(i=0; i<m;i++)        {             for(j=0; j<n;j++) {scanf ("%d", &map1[i][j]);//input Diagram}} printf ("%d\n", Getmax ()); }    return 0;}

 

Discover Treasures---dual-line 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.