HDU 4111 Alice and Bob 【博弈】

本文介绍了一个基于博弈论的游戏问题,通过分析游戏规则和策略,利用动态规划思想解决了谁将在游戏中获胜的问题。给出了AC代码实现,并解释了核心算法思想。

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


传送门:HDU 4111


Alice and Bob
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description
Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The most amazing thing is that they always find the best strategy, and that’s why they feel bored again and again. They just invented a new game, as they usually did.
The rule of the new game is quite simple. At the beginning of the game, they write down N random positive integers, then they take turns (Alice first) to either:
1.Decrease a number by one.
2. Erase any two numbers and write down their sum.
Whenever a number is decreased to 0, it will be erased automatically. The game ends when all numbers are finally erased, and the one who cannot play in his(her) turn loses the game.
Here’s the problem: Who will win the game if both use the best strategy? Find it out quickly, before they get bored of the game again!

Input
The first line contains an integer T(1 <= T <= 4000), indicating the number of test cases.
Each test case contains several lines.
The first line contains an integer N(1 <= N <= 50).
The next line contains N positive integers A1 …AN(1 <= Ai <= 1000), represents the numbers they write down at the beginning of the game.

Output
For each test case in the input, print one line: “Case #X: Y”, where X is the test case number (starting with 1) and Y is either “Alice” or “Bob”.

Sample Input
3
3
1 1 2
2
3 4
3
2 3 5

Sample Output
Case #1: Alice
Case #2: Bob
Case #3: Bob




题意:
有N堆石子,每堆石子有一个数目,现有两个人博弈,每个人每次可以进行两个操作中的一个:
1、从某堆拿掉一个石子(若某堆石子为0了,那么这堆就不存在了);2、合并两堆石子
没有操作的就输。问是哪个赢.



AC代码:

/*
思想:
如果每堆石子数都大于1,那么最后结果肯定相当于所有的堆合并成一堆后,然后再一个一个拿掉的结果。
因为如果那种情况是赢的人一定会不断合并堆来确保他是赢的。又因为所有堆的石子数都大于1,所以输的人无法阻止他这么干。
而有些堆石子数等于1的话,就不一定是所有的合并的结果了,因为输的人可以直接把等于1的堆去掉,就破坏了结构
(合并相当于2步,去掉只需要1步)。
sg[i][j]表示有i个石子数为1的堆数,其它堆合并再取完的步数为j。若值为1则先取者胜,为0为先取者输
 */
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=55;
int sg[N][N*1000];
int n,t;
int a[N];
int getsg(int one,int sum)
{
  if(sg[one][sum]!=-1) return sg[one][sum];
  if(sum==1) return sg[one][sum]=getsg(one+1,0);//步数只需要1,放入其他1
  sg[one][sum]=0;
  if(one>=1&&!getsg(one-1,sum))//去掉一个1
  {
    sg[one][sum]=1;
  }
  if(one>=1&&sum&&!getsg(one-1,sum+1))//将一个1合到其他数
  {
    sg[one][sum]=1;
  }
  if(sum>=2&&!getsg(one,sum-1))//非1数减1
  {
    sg[one][sum]=1;
  }
  if(one>=2&&((sum==0&&!getsg(one-2,sum+2))||(sum&&!getsg(one-2,sum+3))))//将两个1合并
  {
    sg[one][sum]=1;
  }
  return sg[one][sum];
}

int main()
{
  memset(sg,-1,sizeof(sg));
  int ca=0;
  scanf("%d",&t);
  while(t--)
  {
    int one=0,sum=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
      scanf("%d",&a[i]);
      if(a[i]==1)
      {
        one++;//计算1的个数
      }
      else
      {
        sum+=a[i]+1;//计算步数
      }
    }
    if(sum) sum--;//多合并一次
    getsg(one,sum);
    if(sg[one][sum]) printf("Case #%d: Alice\n",++ca);
    else printf("Case #%d: Bob\n",++ca);
  }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值