A Funny Game(POJ 2484)
- 原题如下:
A Funny Game
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7108 Accepted: 4464 Description
Alice and Bob decide to play a funny game. At the beginning of the game they pick n(1 <= n <= 106) coins in a circle, as Figure 1 shows. A move consists in removing one or two adjacent coins, leaving all other coins untouched. At least one coin must be removed. Players alternate moves with Alice starting. The player that removes the last coin wins. (The last player to move wins. If you can't move, you lose.)
Figure 1
Note: For n > 3, we use c1, c2, ..., cn to denote the coins clockwise and if Alice remove c2, then c1 and c3 are NOT adjacent! (Because there is an empty place between c1 and c3.)Suppose that both Alice and Bob do their best in the game.
You are to write a program to determine who will finally win the game.Input
There are several test cases. Each test case has only one line, which contains a positive integer n (1 <= n <= 106). There are no blank lines between cases. A line with a single 0 terminates the input.Output
For each test case, if Alice win the game,output "Alice", otherwise output "Bob".Sample Input
1
2
3
0Sample Output
Alice
Alice
Bob - 题解:
- n高达1000000,考虑到还有将连续部分分裂成几段等的情况,状态数非常地多,搜索和动态规划都难以胜任。需要更加巧妙地判断胜败关系。
首先,如果能把所有的硬币分成两个完全相同的组的状态,必败态显然。不论自己采取什么选取策略,对手只要在另一组采取相同的策略,就又回到了分成两个相同的组的状态,不断这样循环下去,总会在某次轮到自己时没有硬币了。也就是说,因为对手取走了最后一枚硬币而败北。
接下来,回到正题,Alice在第一步取走了一枚或两枚硬币,原本成圈的硬币就变成了长度为n-1或n-2的链,这样只要Bob在中间位置,根据链长的奇偶性,取走一枚或两枚硬币,就可以把所有硬币正好分成了两个长度相同的链。这正如之前讨论过的,是必败态,也就是说Alice必败,Bob必胜。只不过,当n≤2时,Alice可以在第一步取光,所以胜利的是Alice。 - 注:在这类游戏当中,作出对称的状态后再完全模仿对手的策略常常是有效的。
- n高达1000000,考虑到还有将连续部分分裂成几段等的情况,状态数非常地多,搜索和动态规划都难以胜任。需要更加巧妙地判断胜败关系。
- 代码:
#include<cstdio>
#include<algorithm> using namespace std; int main()
{
int n;
while (scanf("%d", &n))
{
if (n==) break;
if (n<=) puts("Alice");
else puts("Bob");
}
}
A Funny Game(POJ 2484)的更多相关文章
- POJ 2484(对称博弈)
题目链接:http://poj.org/problem?id=2484 这道题目大意是这样的,有n个硬币围成一圈,两个人轮流开始取硬币(假设他们编号从1到n),可以选择取一枚或者取相邻的两枚(相邻是指 ...
- POJ 2484 A Funny Game 博弈论 对称博弈
http://poj.org/problem?id=2484 1和2时Alice必胜,3时Bob必胜,其他情况下Bob只需要在Alice取过之后取一次将剩下的硬币链平均分为两份,然后Alice怎么取B ...
- POJ 2484 A Funny Game【博弈】
相比数据结构的题..感觉这种想啊想的题可爱多了~~~代码量还少.... 题目链接: http://poj.org/problem?id=2484 题意: 一圈n个硬币,两人轮流从中取一或两个硬币,(只 ...
- POJ 2484
A Funny Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3861 Accepted: 2311 Desc ...
- poj 2484 A Funny Game(博弈)
A Funny Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4639 Accepted: 2855 Desc ...
- POJ 2484 A Funny Game(智商博弈)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6397 Accepted: 3978 Description Alice ...
- POJ 2484 A Funny Game(博弈论)
题目链接: 传送门 A Funny Game Time Limit: 1000MS Memory Limit: 10000K Description Alice and Bob decide ...
- 【POJ 2484】A Funny Game
Description Alice and Bob decide to play a funny game. At the beginning of the game they pick n(1 &l ...
- POJ 2484 A Funny Game(神题!)
一开始看这道博弈题的时候我就用很常规的思路去分析了,首先先手取1或者2个coin后都会使剩下的coin变成线性排列的长条,然后无论双方如何操作都是把该线条分解为若干个子线条而已,即分解为若干个子游戏而 ...
- [原博客] POJ 2484 A Funny Game
题目链接题意:有n个硬币排成一圈,两个人轮流操作,每次可以取走一个或者相邻的连个硬币(只算最开始相邻的,取之后才相邻的不算),问先手必胜还是必败. 这个题可以证明若n>=3,则先手必败.对称博弈 ...
随机推荐
- Linux学习日志第一天——基础命令①
文章目录 前言 命令的作用及基本构成 关于路径 命令 ls (list) 命令 pwd (print working directory) 命令cd (change directory) 命令 mkd ...
- menset与fill
menset函数一般只对int型数组进行0.-1的赋值.原因:menset对数组是按字节赋值,对每个字节的赋值是相同的,故int的4个字节全部被赋相同的值,而0正好二进制编码全为0,-1的二进制编码全 ...
- 30分钟闲置服务器建站(gitlab为例)
前言 最近博主的阿里云主机又到了续费的时候了,刚买云主机的时候那是各种优惠各种打折,续费的时候只能当孙子了. 为了节省开支,又保证高性能的前提下,买了台10代NUC,内存和ssd自选,搭建一台个人服务 ...
- 【算法•日更•第二期】查找算法:三分VS二分
▎前言:函数 如果你已经上过初二的数学课了,那么你十有八九会被函数折磨到吐血,这是一种中考压轴题类的题目,往往分类讨论到你恶心.不过没学过也不打紧,现场讲解一下: ☞『数学中的函数』 一般地,如果在一 ...
- Android The layout "activity_main" in layout has no declaration in the base layout folder
报错: The layout "activity_main" in layout has no declaration in the base layout folder; thi ...
- Gitlab-CI/CD 2
Gitlab-Runner自动构建服务器搭建2 注册Runner 上一节我们创建了自己的gitlab-runner镜像,并使用docker-compose up -d --build命令运行了一个名为 ...
- Socket原理及实践(Java/C/C++)
原理 基本概念 什么是TCP/IP.UDP? 详细的可以看一下这个:学习计算机网络知识只要一篇就够了! TCP/IP(Transmission Control Protocol/Internet Pr ...
- Centos7修改用户名
系统原来的用户lou,改为scrapy,要改以下个地方,注:没有修改对应密码 1. # vi /etc/passwd 修改其中的用户名部分.用户组部分.主目录部分2. 修改用户组的配置文件 # ...
- failed to find romfile "efi-virtio.rom"
问题:failed to find romfile "efi-virtio.rom" 解决:apt-get install ipxe-qemu
- Reinforcement learning in artificial and biological systems
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! Abstract 在生物和人工系统的学习研究之间,已经有富有成果的概念和想法流.Bush and Mosteller,Rescorla a ...