Game of Sum 记忆化搜索/递推
来源:互联网 发布:java解决死锁的方法 编辑:程序博客网 时间:2024/05/16 19:47
Game of Sum
Input File: e.in
Output: Standard Output
This is a two player game. Initially there are n integer numbers in an array and players A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at a time. He can take as many consecutive numbers as he wants during his time. The game ends when all numbers are taken from the array by the players. The point of each player is calculated by the summation of the numbers, which he has taken. Each player tries to achieve more points from other. If both players play optimally and player A starts the game then how much more point can player A get than player B?
Input
The input consists of a number of cases. Each case starts with a line specifying the integer n (0 < n ≤100), the number of elements in the array. After that, nnumbers are given for the game. Input is terminated by a line where n=0.
Output
For each test case, print a number, which represents the maximum difference that the first player obtained after playing this game optimally.
Sample Input Output for Sample Input
4
4 -10 -20 7
4
1 2 3 4
0
解决方案:可得递推公式:d[i][j]=sum[j]-sum[i-1]-min(d[i][j],d[i+1][j],.....,d[j][j],d[i][j-1],d[i][j-2],.....d[i][i],0);d[i][j]表示i--j的范围,先手能拿到最大的和。递推时,可从左往右,按第一个,拿前两个,前三,。。。,从右往左,拿后1个,后2个,。。。。,还有全拿的。0就表示全拿,这样有了隐式的边界条件,就不用显式边界条件了。代码一是用记忆化搜索的方式。代码二:把方程变形了。f[i][j]=min(d[i][j],d[i+1][j],....d[j][j]),g[i][j]=min(d[i][j],d[i][j-1],....,d[i][i]);
状态可写成d[i][j]=sum[j]-sum[i-1]-min(f[i+1][j],d[i][j-1],0).f[i][j]=min{f[i+1][j],d[i][j]},g[i][j]=min{f[i][j-1],d[i][j]};这样时间复杂度降了很多。
7
10
code1:#include<iostream>#include<cstdio>#include<cstdio>#include<cstring>using namespace std;int d[103][103];int sum[103];bool vis[103][103];int n;int dp(int i,int j){ if(vis[i][j]) return d[i][j]; vis[i][j]=1; int m=0; for(int k=i+1;k<=j;k++) m=min(m,dp(k,j));///从左边取 for(int k=i;k<j;k++) m=min(m,dp(i,k));///从右边取 d[i][j]=sum[j]-sum[i-1]-m; return d[i][j];}///记忆化搜索int main(){ while(~scanf("%d",&n)&&n){ memset(sum,0,sizeof(sum)); for(int i=1;i<=n;i++){ int a; scanf("%d",&a); sum[i]=sum[i-1]+a; } memset(vis,false,sizeof(vis)); printf("%d\n",2*dp(1,n)-sum[n]); } return 0;}
code2:<pre name="code" class="cpp">#include<iostream>#include<cstdio>#include<cstdio>#include<cstring>using namespace std;int d[103][103];int sum[103];int f[103][103],g[103][103];int n,a[103];int main(){ while(~scanf("%d",&n)&&n){ memset(sum,0,sizeof(sum)); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); sum[i]=sum[i-1]+a[i]; } for(int i=1;i<=n;i++) g[i][i]=f[i][i]=d[i][i]=a[i];///边界条件 for(int L=1;L<n;L++){///按L=j-i递增计算 for(int i=1;i+L<=n;i++){ int j=i+L; int m=0; m=min(m,f[i+1][j]); m=min(m,g[i][j-1]); d[i][j]=sum[j]-sum[i-1]-m; f[i][j]=min(d[i][j],f[i+1][j]);///为下一次结果更新g g[i][j]=min(d[i][j],g[i][j-1]);///为下一次结果更新f } } printf("%d\n",2*d[1][n]-sum[n]); } return 0;}
- Game of Sum 记忆化搜索/递推
- UVa 10891(记忆化搜索,递推)Game of Sum
- Uva 10891 Game of Sum - 区间DP..记忆化搜索
- UVA 10891 Game of Sum dp(记忆化搜索)
- UVA 10891 Game of Sum 记忆化搜索
- UVa 10891 Game of Sum / 记忆化搜索
- UVA 10891 Game of Sum(记忆化搜索+博弈)
- 区间dp||记忆化搜索 Game of Sum UVA
- sum游戏 Game of sum uva 10891 动态规划 备忘录(记忆化搜索)
- 28.uva 10891 Game of Sum 记忆化dp
- 记忆化搜索与递推
- 【记忆化搜索】 HDU-4155-The Game of 31
- poj1101The Game(记忆化搜索)
- hdu4597Play Game 记忆化搜索
- POJ 2506 高精度+递推+记忆化搜索
- 数字三角形——递归、递推、记忆化搜索
- HDU4919 Exclusive or(递推+记忆化搜索+大数)
- sdutoj 3062 蝴蝶效应 (记忆化搜索/递推)
- IOS UIView 之属性篇
- C语言的本质(36)——makefile基础
- Editpad
- CSDN带引 南京一老师举报校长侵占学生伙食费 纪委介入 Q1458910822
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
- Game of Sum 记忆化搜索/递推
- loadrunner 自定义request,参数化
- 分析和解决mnesia过载问题
- 黑马程序员 Foundation Kit中一些常用的类和结构体
- 常用正则表达式
- Android 手机震动功能实现
- 导出导入Oracle数据
- NYOJ-491 幸运三角形
- C++ STL学习之stack。