【题目】

Description

The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2, ..., n − 1. Two
persons pick stones in turn. In every turn, each person selects three piles of stones numbered i, j, k
(i < j, j ≤ k and at least one stone left in pile i). Then, the person gets one stone out of pile i, and
put one stone into pile j and pile k respectively. (Note: if j = k, it will be the same as putting two
stones into pile j). One will fail if he can’t pick stones according to the rule.
David is the player who first picks stones and he hopes to win the game. Can you write a program
to help him?
The number of piles, n, does not exceed 23. The number of stones in each pile does not exceed 1000.
Suppose the opponent player is very smart and he will follow the optimized strategy to pick stones.

Input
Input contains several cases. Each case has two lines. The first line contains a positive integer n
(1 ≤ n ≤ 23) indicating the number of piles of stones. The second line contains n non-negative integers
separated by blanks, S0, . . . , Sn−1 (0 ≤ Si ≤ 1000), indicating the number of stones in pile 0 to pile
n − 1 respectively.
The last case is followed by a line containing a zero.
Output
For each case, output a line in the format ‘Game t: i j k’. t is the case number. i, j and k indicates
which three piles David shall select at the first step if he wants to win. If there are multiple groups of
i, j and k, output the group with the minimized lexicographic order. If there are no strategies to win
the game, i, j and k are equal to ‘-1’.

Sample Input
4
1 0 1 100
3
1 0 5
2
2 1
0
Sample Output
Game 1: 0 2 3
Game 2: 0 1 1
Game 3: -1 -1 -1

【题目翻译】

  David 玩一个石子游戏。游戏中,有n堆石子,被编号为0..n-1。两名玩家轮流取石子。每一轮游戏,每名玩家选取3堆石子i,j,k(i<j,j<=k,且至少有一枚石子在第i堆石子中),从i中取出一枚石子,并向j,k中各放入一枚石子(如果j=k则向k中放入2颗石子)。最先不能取石子的人输。

请编程帮助David。

石子堆的个数不会超过23,每一堆石子不超过1000个。

【分析】

  首先,假设第i堆有xi个石子,那么可以先把xi%2。

  因为在同一堆中的两颗石子是一模一样的。对方对这颗石子做什么,你就可以对另外一颗石子做同样的事,相当于这两颗一样的石子不存在。

  因为每颗石子可以变成两颗放到后面去,也就是说它的转移状态和它的编号有关。

  可以将每一颗石子看作是一堆石子,如果它是第p堆中的石子,把么它所代表的这堆石子的个数为n-1-p。

  因为石子堆是互不干扰的,因此这个游戏可以看作由若干个只有一堆石子的游戏组成。(把其单独考虑开来)

  求它能到达的子状态的尼姆和更新自己的sg值即可。跟扫楼梯一题差不多,即使这堆石子的个数为偶数个,他可能还是有用的,即可以拆分也把状态改变为平衡状态的,要把这个也考虑上。(好像说得不是很清楚,具体看代码吧~~)

代码如下:(看错数据范围了,懒得改了,就酱吧~)

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 1010 int n;
int a[*Maxn],b[*Maxn],sg[*Maxn];
bool vis[*Maxn]; void get_sg(int x)
{
memset(vis,,sizeof(vis));
for(int i=;i<x;i++)
for(int j=i;j<x;j++)
{
vis[sg[i]^sg[j]]=;
}
for(int i=;i<=;i++)
if(vis[i]==) {sg[x]=i;break;}
} bool output(int x,int now)
{
for(int i=x-;i>=;i--)
for(int j=i;j>=;j--)
if((sg[i]^sg[j])==now)
{
printf("%d %d %d\n",n-x,n-i,n-j);
return ;
}
return ;
} int main()
{
int kase=;
for(int i=;i<=;i++) get_sg(i);
while()
{
scanf("%d",&n);
if(n==) break;
int ans=;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=n;i++) b[i]=a[n-i+];
for(int i=;i<=n;i++)
{
if(b[i]%==) ans^=sg[i];
}
printf("Game %d: ",++kase);
if(ans==) {printf("-1 -1 -1\n");continue;}
int mx=;
for(int i=;(<<i)<=ans;i++)
if((<<i)&ans) mx=(<<i);
for(int i=n;i>=;i--)
if(b[i]!=) {if(output(i,ans^sg[i])) break;}
}
return ;
}

[UVA1378]

2016-04-17 17:12:38

【UVA1378】A Funny Stone Game (博弈-求SG值-输出方案)的更多相关文章

  1. hdu 1536 S-Nim_求sg值模版

    题意:给你很n堆石头,k代表你有k种拿法,然后给出没堆石头的数量,求胜负 直接套用模版 找了好久之前写的代码贴上来 #include<iostream> #include<algor ...

  2. HDU 1729 类NIM 求SG

    每次有n个盒子,每个盒子有容量上限,每次操作可以放入石头,数量为不超过当前盒子中数量的平方,不能操作者输. 一个盒子算一个子游戏. 对于一个盒子其容量为s,当前石子数为x,那么如果有a满足 $a \t ...

  3. 题解报告:hdu 1847 Good Luck in CET-4 Everybody!(入门SG值)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1847 Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧 ...

  4. UVA1378 A Funny Stone Game —— SG博弈

    题目链接:https://vjudge.net/problem/UVA-1378 题意: 两个人玩游戏,有n堆石子,两人轮流操作:于第i堆石子中取走一块石子,然后再往第j.k堆中各添加一块石子.其中 ...

  5. 博弈之——SG模板

    很久没搞博弈了.先来写个模板: 现在我们来研究一个看上去似乎更为一般的游戏:给定一个有向无环图和一个起始顶点上的一枚棋子,两名选手交替的将这枚棋子沿有向边进行移动,无法移动者判负.事实上,这个游戏可以 ...

  6. 简单易懂的博弈论讲解(巴什博弈、尼姆博弈、威佐夫博弈、斐波那契博弈、SG定理)

    博弈论入门: 巴什博弈: 两个顶尖聪明的人在玩游戏,有一堆$n$个石子,每次每个人能取$[1,m]$个石子,不能拿的人输,请问先手与后手谁必败? 我们分类讨论一下这个问题: 当$n\le m$时,这时 ...

  7. Gym 101246D Fire in the Country(dfs求SG函数)

    http://codeforces.com/gym/101246/problem/D 题意: 给定一个无向有环图,大火从1点开始,每个时间点与它相邻的点也将会着火,现在有两个人轮流操作机器人,机器人从 ...

  8. 学习笔记--博弈组合-SG函数

    fye学姐的测试唯一的水题.... SG函数是一种游戏图每个节点的评估函数 具体定义为: mex(minimal excludant)是定义在整数集合上的操作.它的自变量是任意整数集合,函数值是不属于 ...

  9. 博弈的SG函数理解及模板

    首先定义mex(minimal excludant)运算,这是施加于一个集合的运算,表示最小的不属于这个集合的非负整数.例如mex{0,1,2,4}=3.mex{2,3,5}=0.mex{}=0. 对 ...

随机推荐

  1. Java基础知识强化之IO流笔记24:FileInputStream / FileOutputStream 复制文本文件案例2

    1. 需求:把d盘下的a.txt的内容复制到f盘下的b.txt中: 代码示例: package com.himi.filecopy; import java.io.FileInputStream; i ...

  2. 【原创教程】虎咽CSS

      上节课我们讲了HTML基础,回顾的事我不干了,因为你可以回头看很多遍,这节课我们来学习下CSS这门艺术的基础知识,一直以来我们都是CSS,CSS那么CSS到底是什么呢,惯例,我又来一层一层把CSS ...

  3. EventBus3 简单使用及注意点

    博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 # EventBus3 简介 EventBus Android 发布/订阅事件总线,可简化 Activities, Fr ...

  4. 一个自定义线程池的小Demo

    在项目中如果是web请求时候,IIS会自动分配一个线程来进行处理,如果很多个应用程序共享公用一个IIS的时候,线程分配可能会出现一个问题(当然也是我的需求造成的) 之前在做项目的时候,有一个需求,就是 ...

  5. IE9透明filter和opacity同时生效的解决办法 IE9 hack only

    转载请注明:来自于http://www.cnblogs.com/bluers/ 问题: 假设结构如下: <div class="wrapper"> <p clas ...

  6. postman本地或远程访问不了

    问题postman: 问题描述: 1.手动测试过不去http://121.199.54.31:8081/suppliers/Split 2.localhost:8080/microshop-settl ...

  7. QTableWidget中添加按钮

    添加按钮 void QTableWidget::setCellWidget ( int row, int column, QWidget * widget ) widget可以是自己定义的按钮 cla ...

  8. jQuery实现商品楼层的感觉

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. WF学习笔记(四)

    1.使用WorkflowApplication.OnUnhandledException 处理Code异常 ,用于产生异常的Activity: public class ExceptionActivi ...

  10. 经典SQL练习题

    题目地址:http://blog.csdn.net/qaz13177_58_/article/details/5575711 1. 查询Student表中的所有记录的Sname.Ssex和Class列 ...