UVALive 7278 Game of Cards (sg函数)
Game of Cards
题目链接:
http://acm.hust.edu.cn/vjudge/contest/127406#problem/G
Description
```
Alice and Bob created a new game while at the beach this
summer. All they need is a set of numbered playing cards.
They start by creating P piles with all cards face-up and
select a non-negative number K. After that, they take turns
like this:
1. A player starts by selecting one of the piles.
2. Then, he removes from 0 up to K cards from the top of
that pile, leaving at least one card in the pile.
3. Next, he looks at the card left at the top of the pile and
must remove a number of cards equal to its value (from
the top of the same pile).
Whoever doesn’t have more cards to remove, or whoever
is forced to remove more cards than those available on a pile, loses the game.
In the figure, you can see an example with
two piles and K = 1. The player to move might:
a) Select the first pile and 0 cards to remove,
being forced to remove 1 card from the top
next.
b) Select the second pile and 0 cards to remove,
having to remove 1 card from the
top next.
c) Select the second pile and 1 card to remove,
having to remove 2 cards from the
top next.
Alice has realized that Bob is very good at
this game and will always win if he has the
chance. Luckily, this time Alice is first to play.
Is Alice able to win this game?
Given the description of the piles with all
the cards and the maximum number of cards
they can start to remove, your goal is to find
out whether Alice can win the game if she is the
first to play.
```
Input
The input file contains several test cases, each of them as described below.
The first line contains 2 space separated integers, P, the number of piles, and K, the maximum
number of cards they can start to remove on their turn. The next P lines start with an integer N,
indicating the number of cards on a pile. N space separated integers follow, representing the cards on
that pile from the bottom to the top.
Constraints:
1 ≤ P ≤ 100 Number of piles.
1 ≤ K ≤ 10 Maximum number of cards a player can start to remove.
1 ≤ c ≤ 10 Number on each card.
1 ≤ N ≤ 1 000 Size of each pile.
Output
For each test case, a single string, stating ‘Alice can win.’ or ‘Bob will win.’, as appropriate.
Notes:
Explanation of output 1. The piles are the same, so Bob will always be able to mirror whatever
move Alice makes.
Explanation of output 2. Alice can start by removing 0 cards from the second pile and then 1 card
from its top. Two legal moves will be possible next, Bob will make one and Alice the other.
Sample Input
4 1
4 1 1 1 1
6 2 1 2 1 2 1
4 1 1 1 1
6 2 1 2 1 2 1
2 1
1 1
3 1 2 1
2 2
5 3 2 1 2 1
5 5 4 3 2 1
Sample Output
Bob will win.
Alice can win.
Alice can win.
##题意:
有n堆牌,每回合可以从一堆中先抽[0,k]张牌,但是这一抽后必须使得这一堆非空. 然后看牌堆顶的数字,再抽这么多的牌.
不能操作者输.
##题解:
典型的sg函数. 单独考虑每一堆.
以长度为牌堆的状态. sg[x] 表示牌堆中后x张牌的sg值.
对于每个状态,枚举取的个数[0,k]来得到拓展状态.
对于不可达到的状态,这里可以将其sg值设为-1. (如果后继状态都是-1那当前状态为0,即必败).
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 1010
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;
int p,k;
int sg[maxn];
bool vis[maxn];
int pile[maxn], cnt;
int sg_check(int size, int take) {
int top = pile[size - take];
if(take + top > size) return -1;
return sg[size-take-top];
}
void get_sg() {
memset(sg, 0, sizeof(sg));
for(int i=1; i<=cnt; i++) {
/*误把初始化放到外面导致WA一发*/
memset(vis, 0, sizeof(vis));
for(int j=0; j<=k&&j<i; j++) {
int tmp = sg_check(i, j);
if(tmp != -1) vis[tmp] = 1;
}
for(int j=0; j<maxn; j++) if(!vis[j]) {
sg[i] = j; break;
}
}
}
int main(int argc, char const *argv[])
{
//IN;
while(scanf("%d %d", &p,&k) != EOF)
{
int ans = 0;
while(p--) {
scanf("%d", &cnt);
for(int i=1; i<=cnt; i++)
scanf("%d", &pile[i]);
get_sg();
ans ^= sg[cnt];
}
if(ans) puts("Alice can win.");
else puts("Bob will win.");
}
return 0;
}
UVALive 7278 Game of Cards (sg函数)的更多相关文章
- LA 7278 Game of Cards(SG函数)
https://vjudge.net/problem/UVALive-7278 题意: 两个人玩游戏,现在有n堆牌,轮到自己时,先在牌堆中选一堆牌,先在牌堆中选择拿走0~k张牌(至少得剩下一张),然后 ...
- Game of Cards Gym - 101128G (SG函数)
Problem G: Game of Cards \[ Time Limit: 1 s \quad Memory Limit: 256 MiB \] 题意 题意就是给出\(n\)堆扑克牌,然后给出一个 ...
- HDU 5795 A Simple Nim 打表求SG函数的规律
A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player wh ...
- 【转】博弈—SG函数
转自:http://chensmiles.blog.163.com/blog/static/12146399120104644141326/ http://blog.csdn.net/xiaofeng ...
- HDU 1848 Fibonacci again and again【SG函数】
对于Nim博弈,任何奇异局势(a,b,c)都有a^b^c=0. 延伸: 任何奇异局势(a1, a2,… an)都满足 a1^a2^…^an=0 首先定义mex(minimal excludant)运算 ...
- POJ2425 A Chess Game[博弈论 SG函数]
A Chess Game Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 3917 Accepted: 1596 Desc ...
- bzoj1188 [HNOI2007]分裂游戏 博弈论 sg函数的应用
1188: [HNOI2007]分裂游戏 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 973 Solved: 599[Submit][Status ...
- BZOJ1188 [HNOI2007]分裂游戏(SG函数)
传送门 拿到这道题就知道是典型的博弈论,但是却不知道怎么设计它的SG函数.看了解析一类组合游戏这篇论文之后才知道这道题应该怎么做. 这道题需要奇特的模型转换.即把每一个石子当做一堆石子,且原来在第i堆 ...
- sg函数与博弈论2
参考链接: http://blog.sina.com.cn/s/blog_51cea4040100h3l9.html 这篇主要就是讲anti-sg.multi-sg和every-sg的. 例1 poj ...
随机推荐
- Android的NDK开发(4)————JNI数据结构之JNINativeMethod
转至:http://blog.csdn.net/conowen/article/details/7524744 1.JNINativeMethod 结构体的官方定义 typedef struct { ...
- HDU 4549 M斐波那契数列(矩阵幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4549 题意:F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]. 思路:手算一下可以发现 ...
- ASP.NET MVC 学习4、Controller中添加SearchIndex页面,实现简单的查询功能
参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-method ...
- 苹果官方 Crash文件分析方法 (iOS系统Crash文件分析方法)
对于提交的苹果官方的app,在审核的时候会给我们一些crash文件,对于这些有用的文件,里面是关于我们的bug的一些信息,那么该如何去调试呢 第一步:在任意目录创建一个目录,用来调试crash,我这里 ...
- LT1619EMS8 锂电池 升压电路分析
LT1619EMS8 锂电池 升压电路分析 本文主要是分析LT1619EMSB锂电池升压芯片电路,知道其大致是怎么工作的,其中的一些电阻该如何配置. 2016-1-23 深圳 南山平山村 曾剑锋 一. ...
- .Net中的各种序列化
我们知道将对象的状态保持在存储媒体中,以便可以在以后重新创建精确的副本这正是数据持久化所要做的.而且,不同应用程序之间的通讯需要相互传输数据.那么序列化和反序列化正是为此而生. 序列化和反序列化 所谓 ...
- ORACLE之手动注册监听listener。alter system set local_listener="XXX"
记录下刚刚做的一个为一个数据库(t02)配置多个监听(listener)的实验,过程有点小曲折. (1)新增两个测试的监听,listener.ora的配置内容(可纯手动编辑该文件或使用netca)如下 ...
- memcached 最大连接数及其内存大小的设置
memcached的基本设置: -p 监听的端口-l 连接的IP地址, 默认是本机-d start 启动memcached服务-d restart 重起memcached服务-d stop|shutd ...
- jsp防盗链代码
// 禁止缓存 response.setHeader("Cache-Control", "no-store"); response.setHeader( ...
- Windows Phone 离主流系统还很远
调查机构 Kantar Worldpanel 在本月发布全球智能手机份额报告.报告显示,五月份除德国和澳大利亚出现下滑,Windows Phone 的市场份额在不少国家都实现增长. 英国,4.1% 升 ...