hdu4111 Alice and Bob 博弈论

2024-08-28 17:08
文章标签 alice bob 博弈论 hdu4111

本文主要是介绍hdu4111 Alice and Bob 博弈论,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

统计1的个数c,以及非1情况下的步数s,包括合并。
c为奇数,s不等于2:那么先手必胜。
s为2或者为0:c为3的倍数是先手必败。
否则的话,s为奇数时先手必胜。
首先没有1的情况下很好证明,就是总的步数和为奇数时为先手必胜态,N点。相反,如果为偶数就是P点。(这种情况
下必胜的人只要保证任意一堆石子的个数不会小于2即可,直到最后只剩下一堆,因为1是一个比较特殊的存在)
再者证明奇数个1且s不为2的时候为必胜态,从一个1的开始,假设现在有一个1和s,如果s为奇数,那么先手可以合并
两堆石子;如果s是偶数,先手可以拿走1;所以这种情况下一定是先手必胜,因为移动1可以选择少掉一步或者两步。
那么现在就是两个1和s的情况,如果其中一个人先消耗掉一个1,和s合并,那么剩下的状态是(1,s+1)的N点;取走1,
剩下(1,s)同样是N点;合并两个1,(2,s)==>(s+3)的状态,如果s为奇数,那么即为P态,对应的刚才先手就是必胜,反之
则是必败,这种情况归纳到结论中的第三条。那么三个1的情况下就可以消耗一个1更变s的奇偶性。
不过在s=2或者s=0的时候属于特殊情况,因为2去掉一个之后就变成了1,s为0时为全1,通过上面的规律,我们已经知

  

实在是没想到,大家都说是简单的题目我做了一下午也没A掉他看别人思路看了很长时间。。。。。。真的很巧妙

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 A  1 ....A  N(1 <= A  i <= 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


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int dp[55][55000];
int DP(int a,int b)
{if(dp[a][b]!=-1) return dp[a][b];if(b==1) return dp[a][b]=DP(a+1,0);//只剩一个单独的一,加入其它1中dp[a][b]=0;if(a>=1&&!DP(a-1,b))//直接去掉一个1dp[a][b]=1;if(a>=1&&b&&!DP(a-1,b+1))//将一个1合到其它数中dp[a][b]=1;if(a>=2&&((b&&!DP(a-2,b+3))||(b==0&&!DP(a-2,2))))//将2个1并起来dp[a][b]=1;if(b>=2&&!DP(a,b-1))//减小一dp[a][b]=1;return dp[a][b];
}
int main()
{int T;scanf("%d",&T);memset(dp,-1,sizeof(dp));for(int t=1; t<=T; t++){int n;scanf("%d",&n);int j,k,i;for( j=0,k=0,i=0; i<n; i++){int a;scanf("%d",&a);if(a==1)j++;elsek+=+a+1;}if(k);k--;DP(j,k);printf("Case #%d: ",t);if(dp[j][k]) printf("Alice\n");else printf("Bob\n");}return 0;
}


这篇关于hdu4111 Alice and Bob 博弈论的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1115373

相关文章

杨bob的技术之旅

杨bob今天正式入驻csdn,以后要把自己每一点滴写成文章,这也是冲高阶的毕竟之路

XTOJ 1168 Alice and Bob (记忆化搜索)

OJ题目 : click here ~~ 题意分析:给一个数n,Alice可取1,2 , 4 ……2的i次方 ,Bob可取1,3,9……3的i次方。Alice先取,后Bob。轮流来,每个人至少取1。求n变成0,至少需要取多少次。记忆化搜索 = 搜索 + dp 。 AC_CODE #define gril 0#define boy 1using namespace std;const

【每日一题】【博弈论】【思维】会赢的! 牛客周赛 Round 58 C题 C++

牛客周赛 Round 58 C题 会赢的! 题目背景 牛客周赛 Round 58 题目描述 样例 #1 样例输入 #1 31 11 0-1 -1 样例输出 #1 NOYESPING 做题思路 首先考虑到开始位置为 ( 0 , 0 ) (0,0) (0,0)并且只能使横纵坐标递增。所以如果终点的横纵坐标为负数的情况是不可能到达的。所以平局。 第一个点: x

Leetcode 3273. Minimum Amount of Damage Dealt to Bob

Leetcode 3273. Minimum Amount of Damage Dealt to Bob 1. 解题思路2. 代码实现 题目链接:3273. Minimum Amount of Damage Dealt to Bob 1. 解题思路 这一题代码并不复杂,关键就是想明白为啥。 我直接的一个思路就是贪婪算法,考察任意两个怪 i , j i, j i,j之间处理的先后关系,其结果

耶鲁大学《博弈论》公开课笔记

关于本文 该文章基于本人于当天白天看完耶鲁大学《博弈论》公开课后,于当晚记录下依然记得的白天所听内容。该文章为汇总,时间为记录一年后。 目录 关于本文2023.3.18 P103.19 P113.20 P123.21 P133.22 P143.23 P153.24 P163.25 P173.26 P183.27 P193.28 P203.29 P213.30 P223.31 P234.1

[H贪心] lc3273. 对 Bob 造成的最少伤害(贪心+排序+推公式+双周赛138_4)

文章目录 1. 题目来源2. 题目解析 1. 题目来源 链接:3273. 对 Bob 造成的最少伤害 题单: na na 2. 题目解析 略低于正常难度的 T4。 显然我们应该尽可能的将伤害高的先消掉,然后写完代码就会发现 WA 了。想太简单了,那就推推公式看看怎么回事吧。这里直接贴一下 蛙佬 的题解吧。简洁移动,也很容易能发现这个。 来自: 作者:TsRea

有向图游戏 SG函数【博弈论】C++

SG函数可以用来判断在一个给定的有向图游戏中,当前局面的胜负状态。 SG函数的定义如下: 设当前节点为v,那么SG(v)为当前局面的SG值。SG(v)的定义如下: - 如果当前节点v没有后继节点,则SG(v) = 0 - 如果当前节点v有若干个后继节点,分别为v1,v2,...,v_n,那么SG(v)为所有后继节点的SG值的异或和。即:SG(v) = SG(v1) XOR SG(v2) XOR

hdu 1524 A Chess Game 博弈论

题意: 两个人在一个有向五环图上面走棋子,每次只能走一步,最后谁 * 没有棋子可走就败,然后棋子可以重叠,并且有n个棋子。要求判断 * 先手的胜负。 纠结了好长时间一直在想为什么sg函数要呢么定义然后看了各种博客但是只是讲了,定义的内容却很少有讲为什么的。。。。 Description Let's design a new chess game. There are N

博弈论--巴什博弈——HDU1846

/*巴什博弈:HDU--1846*/ # include<iostream> using namespace std; int main() {     int t;     int n,m;     cin>>t;     while(t--)     {       cin>>n>>m;       if(n%(m+1)==0) cout<<"sec

Codeforces Round 916 (Div. 3) E1. Game with Marbles(博弈论*1400)

感觉很难想。 如果你直接想的话,你就会发现有很多做法可以选择,而你根本不知道应该选哪个。 这时候可以先假设鲍勃已经取走了爱丽丝的所有的颜色的弹珠,(并且以每个颜色一个弹珠的代价)。 这时候每一项得分就是 S i = − ( b i − 1 ) S_i = -(b_i - 1) Si​=−(bi​−1)。 然后我们使得这时候爱丽丝的操作为取回弹珠,即她可以选择一种颜色的弹珠,并且直接取回,鲍勃