Code[vs of the dynamic planning of checkerboard type] 1220 digital triangles

Source: Internet
Author: User

/*DP[I][J]: = from vertex arrival point (i,j), the maximum value on the path is initialized: dp[][] = {-inf}//test data has a negative number, so the initial value dp[][] =-infdp[1][1] = arr[1][1] State equation: dp[i][j] = Max (Dp[i-1][j-1], dp[i-1][j]) + arr[i][j] Answer: max{Dp[n][i]} 0 <= i <= n-1*/
1 #define_crtdbg_map_alloc2#include <stdlib.h>3#include <crtdbg.h>4 #define_crt_secure_no_warnings5 #defineHOME6 7#include <iostream>8#include <cstdlib>9#include <cstdio>Ten#include <cstddef> One#include <iterator> A#include <algorithm> -#include <string> -#include <locale> the#include <cmath> -#include <vector> -#include <cstring> - using namespacestd; + Const intINF =0x3f3f3f3f; - Const intMAXN = -; + Const intMax = the; A  at intN; - intArr[max][max]; - intDp[max][max]; -  - BOOLCheck (intXintYintlen) - { in     if((x<=0) || (y<=0) || (y>len)) -     { to         return false; +     } -     return true; the } *  $ voidSolve ()Panax Notoginseng { -     intx1, y1, x2, y2; thedp[1][1] = arr[1][1]; +      for(inti =1; I <= N; ++i) A     { the          for(intj =1; J <= I; ++j) +         { -X1 = i-1; $Y1 = J-1; $x2 = i-1; -y2 =J; -             if(Check (x1, y1, I-1)) the             { -DP[I][J] = max (Dp[i][j], dp[x1][y1] +arr[i][j]);Wuyi             } the             if(Check (x2, y2, I-1)) -             { WuDP[I][J] = max (Dp[i][j], Dp[x2][y2] +arr[i][j]); -             } About         } $     } -     /*for (int i = 1; I <= N; ++i) -     { - For (int j = 1; J <= i; ++j) A         { + cout << "(" << I << "," << J << "):" << dp[i][j] << Endl; the         } -     }*/ $cout << *max_element (Dp[n], Dp[n] + N +1) <<Endl; the } the  the intMain () the { - #ifdef HOME inFreopen ("inch","R", stdin); the     //freopen ("Out", "w", stdout); the #endif About      for(inti =0; i < Max; ++i) the     { the          for(intj =0; J < Max; ++j) the         { +DP[I][J] =-INF; -         } the     }Bayi  the  theCIN >>N; -      for(inti =1; I <= N; ++i) -     { the          for(intj =1; J <= I; ++j) the         { theCIN >>Arr[i][j]; the         } -     } the Solve (); the  the #ifdef HOME94Cerr <<"Time Elapsed:"<< clock ()/Clocks_per_sec <<"Ms"<<Endl; the _CrtDumpMemoryLeaks (); theSystem"Pause"); the #endif98     return 0; About}

Code[vs of the dynamic planning of checkerboard type] 1220 digital triangles

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.