博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Easy Game(记忆化搜索)
阅读量:5010 次
发布时间:2019-06-12

本文共 2119 字,大约阅读时间需要 7 分钟。

You are playing a two player game. Initially there are n integer numbers in an array and player 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

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the size of the array. The next line contains Nspace separated integers. You may assume that no number will contain more than 4 digits.

Output

For each test case, print the case number and the maximum difference that the first player obtained after playing this game optimally.

Sample Input

2

 

4

4 -10 -20 7

 

4

1 2 3 4

Sample Output

Case 1: 7

Case 2: 10

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define Inf 0x3f3f3f3fconst int maxn=1e5+5;typedef long long ll;using namespace std;int dp[205][205];int sum[205];int n;int dfs(int l,int r){ if(dp[l][r]!=-Inf) { return dp[l][r]; } if(l>r) { return 0; } int ans=-Inf; for(int t=1;t<=n;t++) { if(l+t<=r+1) ans=max(ans,sum[l+t-1]-sum[l-1]-dfs(l+t,r)); } for(int t=1;t<=n;t++) { if(r-t>=l-1) ans=max(ans,sum[r]-sum[r-t]-dfs(l,r-t)); }// cout<
<<" "<
<<" "<
<
>T; int cnt=1; while(T--) { scanf("%d",&n); for(int t=0;t<=n;t++) { for(int j=0;j<=n;j++) { dp[t][j]=-Inf; } } memset(sum,0,sizeof(sum)); int x; for(int t=1;t<=n;t++) { scanf("%d",&x); dp[t][t]=x; sum[t]=sum[t-1]+x; } dfs(1,n); printf("Case %d: %d\n",cnt++,dp[1][n]); } return 0;}

 

转载于:https://www.cnblogs.com/Staceyacm/p/11248746.html

你可能感兴趣的文章
iview树的修改某个节点,树刷新后自动展开你刚才展开的所有节点
查看>>
oracle服务起不来以及无法监听问题解决
查看>>
Mvc--Html.ActionLink()的用法
查看>>
delphi 基础书籍推荐
查看>>
《面向对象程序设计》2018年春学期寒假及博客作业总结
查看>>
iOS开发UI之KVC(取值/赋值) - KVO (观察某个对象的某个属性的改变)
查看>>
1.7 将一个MxN矩阵所有为0的元素所在行和列全部置0
查看>>
删除U盘时提示无法停止‘通用卷’设备的解决方法!!不要每次都硬拔了,对电脑有不小的损害!!!...
查看>>
Java中接口与接口和类之间的关系
查看>>
芯片TPS70925
查看>>
linux shell 发送email 附件
查看>>
人群密度估计 CrowdCount
查看>>
JSON.parse()和JSON.stringify()
查看>>
.net 常用正则表达式
查看>>
Java泛型中的标记符含义:
查看>>
初遇GitHub
查看>>
[C# 网络编程系列]专题八:P2P编程
查看>>
Jsの练习-数组常用方法 -forEach()
查看>>
动态绑定treeview的方法
查看>>
jvm参数
查看>>