标签:base iostream namespace ace include case names main turn
每次取最左端或最右端的石子,谁取得多谁胜利
#include<iostream>
using namespace std;
const int N = 100;
int piles[N],dp[N][N][2];//0.表先手,1.表后手
int main()
{
int n;
cin>>n;
for(int i=0;i<n;++i)cin>>piles[i];
//base case
for(int i=0;i<n;++i){
dp[i][i][0]=piles[i];
dp[i][i][1]=0;
}
//dp[i][j][0] i-j堆的先手情况
//dp[i][j][0]=piles[i]+dp[i+1][j][1]
//dp[i][j][0]=piles[j]+dp[i][j-1][1]
//dp[i][j][1]=dp[i+1][j][0]
//dp[i][j][1]=dp[i][j-1][0];
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
cout<<dp[i][j][0]<<" "<<dp[i][j][1]<<"------";
}
cout<<endl;
}
cout<<endl;
for(int l=1;l<n;++l){
for(int i=0;i<n;++i){
int j=l+i;
int left=piles[i]+dp[i+1][j][1];
int right=piles[j]+dp[i][j-1][1];
if(left>right){
dp[i][j][0]=left;
dp[i][j][1]=dp[i+1][j][0];
}
else{
dp[i][j][0]=right;
dp[i][j][1]=dp[i][j-1][0];
}
}
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
cout<<dp[i][j][0]<<" "<<dp[i][j][1]<<"------";
}
cout<<endl;
}
cout<<endl;
}
cout<<dp[0][n-1][0]-dp[0][n-1][1]<<endl;
return 0;
}
标签:base iostream namespace ace include case names main turn
原文地址:https://www.cnblogs.com/clear-love/p/11335330.html