Easy Game(记忆化搜索)

本文探讨了一个二人轮流从数组两端取数的游戏策略。通过动态规划算法,计算了在双方都采用最优策略的情况下,先手玩家相较于后手玩家最多能获得多少额外的分数。文章提供了详细的算法实现代码,并通过样例输入输出展示了算法的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#define Inf 0x3f3f3f3f
const 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<<l<<" "<<r<<" "<<ans<<endl;
    return dp[l][r]=ans;
}
int main()
{
   int T;
   cin>>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

#include <iostream> #include <cstdlib> #include <ctime> #include <conio.h> #include <windows.h> // 补充Sleep函数声明[^2] using namespace std; #define ROWS 4 #define COLS 4 // 卡牌状态枚举 enum CardState { BACK, FRONT, MATCHED }; // 卡片结构 struct Card { char symbol; bool isFlipped; bool isMatched; }; // 计分模块 int score = 0; void updateScore(int points) { score += points; cout << "当前得分:" << score << endl; } // 初始化卡牌数字 void initCards(int cards[][COLS]) { int pairs = (ROWS*COLS)/2; for(int i=0; i<pairs; i++){ cards[i/COLS][i%COLS] = i+1; cards[(i+pairs)/COLS][(i+pairs)%COLS] = i+1; } } // 随机打乱卡牌位置 void shuffleCards(int cards[][COLS]) { srand(time(0)); for(int i=0; i<ROWS; i++){ for(int j=0; j<COLS; j++){ int x = rand()%ROWS; int y = rand()%COLS; swap(cards[i][j], cards[x][y]); } } } // 显示游戏界面 void display(CardState state[][COLS], int cards[][COLS]) { system("cls"); cout<<" "; for(int j=0; j<COLS; j++) cout<<j+1<<" "; cout<<"\n"; for(int i=0; i<ROWS; i++){ cout<<i+1<<" "; for(int j=0; j<COLS; j++){ switch(state[i][j]){ case BACK: cout<<"■ "; break; case FRONT: cout<<cards[i][j]<<" "; break; case MATCHED: cout<<"★ "; break; } } cout<<"\n"; } } int main() { int cards[ROWS][COLS]; CardState state[ROWS][COLS] = {BACK}; int firstX = -1, firstY = -1; int matchedCount = 0; initCards(cards); shuffleCards(cards); while(matchedCount < ROWS*COLS) { display(state, cards); // 获取玩家输入 int x,y; cout<<"输入坐标(行 列):"; cin>>x>>y; x--; y--; if(x<0 || x>=ROWS || y<0 || y>=COLS){ cout<<"无效坐标!"; continue; } if(state[x][y] != BACK) continue; state[x][y] = FRONT; display(state, cards); if(firstX == -1) { firstX = x; firstY = y; } else { // 判断匹配 if(cards[firstX][firstY] == cards[x][y]){ state[firstX][firstY] = MATCHED; state[x][y] = MATCHED; matchedCount += 2; } else { display(state, cards); Sleep(1000); state[firstX][firstY] = BACK; state[x][y] = BACK; } firstX = -1; } } cout<<"游戏通关!"; return 0; }给我润色并且添加一些其他功能
最新发布
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值