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 ...
随机推荐
- 内核MKDEV(MAJOR, MINOR)宏
版本:linux-2.6.24.4宏: MKDEV(MAJOR, MINOR); 说明: 获取设备在设备表中的位置. MAJOR 主设备号 MINOR 次设 ...
- C#获取本机IP以及无线网ip
1 private void GetIP() 2 { 3 string hostName = Dns.GetHostName();//本机名 4 //System.Net.IPAddress ...
- BestCoder Round #2 1001 (简单处理)
题目链接 题意:给N条信息,每个信息代表有x个人从开始的时间 到 结束的时间在餐厅就餐, 问最少需要多少座位才能满足需要. 分析:由于时间只有24*60 所以把每个时间点放到 数组a中,并标记开始的时 ...
- bzoj1564
嗯,这是一道简单题 注意二叉搜索树的子树中序一定是连续的 又因为取值修改是任意的并且修改代价与权值无关 不难想到把权值离散化,然后按找数据值排序,然后dp f[i][j][w]表示从i~j的节点构成一 ...
- 通过org.springframework.web.filter.CharacterEncodingFilter定义Spring web请求的编码
通过类org.springframework.web.filter.CharacterEncodingFilter,定义request和response的编码.具体做法是,在web.xml中定义一个F ...
- android错误 Android NAND: nand_dev_load_disk_state,
Android NAND: nand_dev_load_disk_state, 总是提示上面这个错误,我以为是内存分配不足呢,但是内存足够用也不行,后来在国外的网站上看到这个问题的解答,说是把用到的A ...
- JAVA安卓和C# 3DES加密解密的兼容性问题(2013年8月修改版)
近 一个项目.net 要调用JAVA的WEB SERVICE,数据采用3DES加密,涉及到两种语言3DES一致性的问题, 下面分享一下, 这里的KEY采用Base64编码,便用分发,因为Java的By ...
- angularjs指令中的compile与link函数详解(转)
http://www.jb51.net/article/58229.htm 通常大家在使用ng中的指令的时候,用的链接函数最多的是link属性,下面这篇文章将告诉大家complie,pre-link, ...
- IPicture、BITMAP、HBITMAP和CBitmap的关系
1.有关IPicture加载图片后直接Render到内存DC的问题(HBITMAP转换IPicture)Picture的方法get_Handle可以直接得到图片的句柄 IPicture *pIPict ...
- U1 - A 留在电脑里的字体
U1系列新篇章,实战派!说说常用的字体! U1系列新篇章,实战派!更多干货更多关于软件的使用等即将放出,大家敬请期待!!