Matrix Fetch Game NOIP 2007

Source: Internet
Author: User

2016-05-31 17:26:45

Title Link: NOIP 2007 matrix Fetch game (CODEVS)

Main topic:

Given a matrix, each time at the beginning of each line or at the end of the line takes a number multiplied by the number of 2^, to get the most points obtained

Solution:

Dynamic planning

DP[I][J] Indicates the maximum score obtained from the current line I position to the J position

Transfer equation:

Dp[i][j]=max (DP[I+1][J]+A[I]*2^X,DP[I][J-1]+A[I]*2^X);

Places to be aware of:

1.M and N give the range of a little pit ah, 2^80 only quietly do not speak, so directly on the 100000 of the pressure, feel more than better than fine

2.2 of the time to use, so pre-processing out to use

1 //Matrix Fetch Game (NOIP2007)2 //dynamic programming with high accuracy3#include <stdio.h>4#include <algorithm>5#include <string.h>6 using namespacestd;7 Const intmaxn= -;8 Const intmaxm= -;9 Const intMaxl= -;Ten intn,m; One intA[MAXN]; A intDP[MAXN][MAXM][MAXL]; - intYS[MAXN][MAXL]; - intANS[MAXL]; the intTMP[MAXL]; - voidMulti (int*x,int*y,intz) - { -memset (TMP,0,sizeof(TMP)); +tmp[0]=y[0]; -      for(intI=1; i<=tmp[0];i++) +     { Atmp[i]=y[i]*Z; at     } -      for(intI=1; i<=tmp[0];i++) -     { -tmp[i+1]+=tmp[i]/100000; -tmp[i]%=100000; -     } in      while(tmp[tmp[0]+1]) -     { totmp[0]++; +tmp[tmp[0]+1]+=tmp[tmp[0]]/100000; -tmp[tmp[0]]%=100000; the     } *      for(intI=0;i< -; i++) x[i]=Tmp[i]; $     return ;Panax Notoginseng } - voidADD (int*x,int*y,int*z) the { +memset (TMP,0,sizeof(TMP)); A     if(y[0]>z[0]) tmp[0]=y[0]; the     Elsetmp[0]=z[0]; +      for(intI=1; i<=tmp[0];i++) -     { $tmp[i]=y[i]+Z[i]; $     } -      for(intI=1; i<=tmp[0];i++) -     { thetmp[i+1]+=tmp[i]/100000; -tmp[i]%=100000;Wuyi     } the     if(tmp[tmp[0]+1]) tmp[0]++; -      for(intI=0;i< -; i++) x[i]=Tmp[i]; Wu     return ; - } About BOOLMax (int*x,int*y) $ { -     if(x[0]>y[0])return 1; -     Else if(y[0]>x[0])return 0; -     Else  A     { +          for(inti=x[0];i>=1; i--) the         { -             if(X[i]>y[i])return 1; $             Else if(X[i]<y[i])return 0; the         } the     } the     return 1; the } - intMain () in { thescanf"%d%d",&n,&M); theys[0][0]=1; Aboutys[0][1]=1; the      for(intI=1; i<=m;i++) the     { theMulti (ys[i],ys[i-1],2); +     } -ans[0]=1; ans[1]=0; the      for(intI=1; i<=n;i++)Bayi     { the          for(intj=1; j<=m;j++) the         { -scanf"%d",&a[j]); - Multi (Dp[j][j],ys[m],a[j]); the         } the          for(intk=2; k<=m;k++) the         { the              for(intx=1; x<=m-k+1; x + +) -             { the                 inty=x+k-1; the                 intxm[ to],ym[ to],cm[ to]; theMulti (cm,ys[m-k+1],a[x]);94ADD (xm,cm,dp[x+1][y]); theMulti (cm,ys[m-k+1],a[y]); theADD (ym,cm,dp[x][y-1]); the                 if(Max (xm,ym))98                 { Aboutmemcpy (DP[X][Y],XM,sizeof(int)* -); -                 }101                 Else 102                 {103memcpy (Dp[x][y],ym,sizeof(int)* -);104                 } the             }106         }107ADD (ans,ans,dp[1][m]);108     }109printf"%d", ans[ans[0]]); the      for(inti=ans[0]-1; i>=1; i--)111     { theprintf"%05d", Ans[i]);113     } theprintf"\ n"); the     return 0; the}

Matrix Fetch Game NOIP 2007

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.