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函数)的更多相关文章

  1. LA 7278 Game of Cards(SG函数)

    https://vjudge.net/problem/UVALive-7278 题意: 两个人玩游戏,现在有n堆牌,轮到自己时,先在牌堆中选一堆牌,先在牌堆中选择拿走0~k张牌(至少得剩下一张),然后 ...

  2. Game of Cards Gym - 101128G (SG函数)

    Problem G: Game of Cards \[ Time Limit: 1 s \quad Memory Limit: 256 MiB \] 题意 题意就是给出\(n\)堆扑克牌,然后给出一个 ...

  3. HDU 5795 A Simple Nim 打表求SG函数的规律

    A Simple Nim Problem Description   Two players take turns picking candies from n heaps,the player wh ...

  4. 【转】博弈—SG函数

    转自:http://chensmiles.blog.163.com/blog/static/12146399120104644141326/ http://blog.csdn.net/xiaofeng ...

  5. 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)运算 ...

  6. POJ2425 A Chess Game[博弈论 SG函数]

    A Chess Game Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 3917   Accepted: 1596 Desc ...

  7. bzoj1188 [HNOI2007]分裂游戏 博弈论 sg函数的应用

    1188: [HNOI2007]分裂游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 973  Solved: 599[Submit][Status ...

  8. BZOJ1188 [HNOI2007]分裂游戏(SG函数)

    传送门 拿到这道题就知道是典型的博弈论,但是却不知道怎么设计它的SG函数.看了解析一类组合游戏这篇论文之后才知道这道题应该怎么做. 这道题需要奇特的模型转换.即把每一个石子当做一堆石子,且原来在第i堆 ...

  9. sg函数与博弈论2

    参考链接: http://blog.sina.com.cn/s/blog_51cea4040100h3l9.html 这篇主要就是讲anti-sg.multi-sg和every-sg的. 例1 poj ...

随机推荐

  1. Zookeeper、HBase的伪分布

    1.Zookeeper伪分布的部署(3个节点) 所谓的“伪分布式集群”就是在一台服务器中,启动多个Zookeeper实例.“完全分布式集群”是每台服务器,启动一个Zookeeper实例. 1.1.解压 ...

  2. ExtJs自学教程(2):从DOM看EXTJS

    <二> 从DOM看EXTJS 看标题可能有人会问,为啥好好的例子不看,得从DOM看起呢?答案是目标只为了一个:自运行结果把EXTJS看得更清楚点 首先,要看得靠点工具,带点放大镜什么吧?对 ...

  3. 制作SM2证书

    前段时间将系统的RSA算法全部升级为SM2国密算法,密码机和UKey硬件设备大都同时支持RSA和SM2算法,只是应用系统的加解密签名验证需要修改,这个更改底层调用的加密动态库来,原来RSA用的对称加密 ...

  4. Eclipse中设置在创建新类时自动生成注释

    方法一:Eclipse中设置在创建新类时自动生成注释 windows-->preference Java-->Code Style-->Code Templates code--&g ...

  5. hdu4939 动态规划

    经典动态规划 无需单独枚举最后红塔的数量,因为对于dp[i][j],对于红塔的影响仅局限于i,j两个变量,与其前面塔排列无关,故二维动态规划即可. #include <cstdio> #i ...

  6. svn: E230001: Server SSL certificate verification failed

    TortoiseSvn是好的 命令行svn 的时候 有问题 ,也加了--no-auth-cache --non-interactive参数 svn list 地址 选下p 就好. http://sta ...

  7. 关于<img>标签与文字垂直居中

    要让左边的图片与后面的文字居中,如下效果 HTML代码: <img class="iconCls" alt="最新客户端" src="${bas ...

  8. tomcat部署两个相同的项目报错不能访问

    需要在同一个tomcat上搭建一个项目的两个版本,都要能跑起来   直接复制两个项目部署,会出现两个错误: 1,webAppKey 冲突 2,tomcat启动会有内存溢出(OutOfMemoryErr ...

  9. asp.net实现GZip压缩和GZip解压

    最近在开发一个网站doc.115sou.com,使用到了GZip压缩技术,经过多次搜索找到asp.net中用GZip对数据压缩和解压缩非常方便,当我第一次拿到这个类的时候却感觉很迷茫,无从下手.主要是 ...

  10. UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)

    前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...