[U]3.3.5 博弈原理,DP实现

想了很久,看了下题解顿悟了!赤裸裸的使用的博弈的原理,但是却用DP的方法记录状态。收获颇多。

/*
ID:bysen
LANG:C++
PROG:game1
*/
#include<stdio.h>
using namespace std;

int sum[101][101];
int dp[101][101];
int num[101];

int max( int a,int b ){ return a>b?a:b; }
int min( int a,int b ){ return a<b?a:b; }

int main()
{
 	freopen( "game1.in","r",stdin );
 	freopen( "game1.out","w",stdout );
 	int n;
 	scanf( "%d",&n );
 	for( int i=1;i<=n;i++ )
 	{
 		 scanf( "%d",&num[i] );
 		 dp[i][i]=sum[i][i]=num[i];
	}
	
	for( int i=1;i<=n;i++ )
	for( int j=i+1;j<=n;j++ )
	 	 sum[i][j]=sum[i][j-1]+sum[j][j];
 	
 	for( int i=n-1;i>=1;i-- )
	for( int j=i+1;j<=n;j++ ) 
		 dp[i][j]=sum[i][j]-min( dp[i+1][j],dp[i][j-1] );
    /*
    for( int i=1;i<=n;i++ )
    {
     	 for( int j=i+1;j<=n;j++ )
		 	  printf( "%d-%d:%d  ",i,j,dp[i][j] );
		 printf( "\n" );
    }
    */
	printf( "%d %d\n",dp[1][n],sum[1][n]-dp[1][n] );
 	
	return 0;
}


你可能感兴趣的:(c)