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 ...
随机推荐
- Java中JNI的使用详解第二篇:JNIEnv类型和jobject类型的解释
上一篇说的是一个简单的应用,说明JNI是怎么工作的,这一篇主要来说一下,那个本地方法sayHello的参数的说明,以及其中方法的使用 首先来看一下C++中的sayHello方法的实现: JNIEXPO ...
- CSS在不同浏览器兼容问题,margin偏移/offset溢出等
margin在垂直取值时取最大值 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- android动画坐标定义
这段时间一直在忙Android的项目,总算抽出点时间休息一下,准备把一些项目用到的Android经验分享一下. 在Android开发过程中,经常会碰到Activity之间的切换效果的问题,下面介绍一下 ...
- Ext.Net学习笔记01:在ASP.NET WebForm中使用Ext.Net
Ext.Net是一个对ExtJS进行封装了的.net控件库,可以在ASP.NET WebForm和MVC中使用.从今天开始记录我的学习笔记,这是第一篇,今天学习了如何在WebForm中使用Ext.Ne ...
- Linux Shell编程(2): for while
; i < ; i++)) do echo "current number is $i" done SERVICES="80 22 25 110 8000 23 2 ...
- 【转】Android Studio简单设置
原文网址:http://ask.android-studio.org/?/article/14 Android Studio 简单设置 界面设置 默认的 Android Studio 为灰色界面,可以 ...
- 一个响应式框架——agera
Google在上周开源了一个响应式框架——agera,相信它会慢慢地被广大程序员所熟知.我个人对这样的技术是很感兴趣的,在这之前也研究过RxJava,所以在得知Google开源了这样的框架之后第一时间 ...
- GitHub开源库排名一百的简单介绍,值得收藏!
GitHub Android Libraries Top 100 简介 本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据 GitHub ...
- js获取客户端IP及地理位置
php获取方法: 1.<?php 2.function get_ip_place(){ 3.$ip=file_get_contents("http://fw.qq.com/ipaddr ...
- java classpath、path用法
java环境配置classpath和path变量的作用: path:指定cmd中命令执行文件所在的路径.比如javac.java两个可执行文件在jdk的bin目录下,如果path值含有这个bin目录, ...