您的位置:首页 > 其它

端午个人赛-A return of Nim (Nim+威左夫,拓:巴什)

2017-06-27 17:18 211 查看


Problem Description

Sherlock and Watson are playing the following modified version of Nim game:

· There are n piles of stones denoted as pileS0, pileS1,...,ileSn-1, and n is a prime number;

· Sherlock always plays first, and Watson and he move in alternating turns. During each turn, the current player must perform either of the following two kinds of moves:

1. Choose one pile and remove k(k >0) stones from it;

2. Remove k stones from all piles, where 1≤k≤the size of the smallest pile. This move becomes unavailable if any pile is empty.

· Each player moves optimally, meaning they will not make a move that causes them to lose if there are still any better or winning moves.

Giving the initial situation of each game, you are required to figure out who will be the winner

Input

The first contains an integer, g, denoting the number of games. The 2×g subsequent lines describe each game over two lines:

1. The first line contains a prime integer, n, denoting the number of piles.

2. The second line contains n space-separated integers describing the respective values of pileS0, pileS1,...,pileSn-1.

· 1≤g≤15

· 2≤n≤30, where n is a prime.

· 1≤pilesi≤1e5 where 0≤i≤n−1

Output

For each game, print the name of the winner on a new line (i.e., either “Sherlock” or “Watson”)

Sample Input
2
3
2 3 2
2
2 1

Sample Output
Sherlock
Watson

题意:
Sherlock和Watson轮流取,Sharlock为先手。
一共有n堆石子(n为质数),两种取法,

1、从任意一堆取任意k个石子。

2、从每一堆取k个石子(k<石子数最小的一堆)

先取完者为胜,输出胜出者。

输入:
注意g也是多组,g也是多组,g也是多组

思路:
1、当n==2时, 是裸的威佐夫,用 差值 乘以 (sqrt(5)+1)/2.0 并向下取整, 所得若等于较小的石子数,则是必败态
具体分析见http://blog.csdn.net/y990041769/article/details/21694007

2、当n==3时,比一般的Nim多了一种操作, 可以在所有堆中取石子
     
a、证明必胜态一定可以转化成必败态:
与典型的Nim博弈一样,设异或值为1的最高位为 m,取第m位也为1的一堆,一定可以通过取走一定的数目使局势转变成必败态。
   
 b、证明必败态不能转移成必败态:
(1)若采用第一种取法,不管怎么取最后的异或值都不可能为0;
(2)若采用第二种取法,因为n是质数,操作后异或值肯定为1,举个栗子~:
1
1 1
1
0
1 0
1

0 1
0
那么在任意一列中拿走1, 必定有奇数个0变为1, 所以异或值最后一定不为0,不可能转移成必败态。

总结与拓展:
1、注意输入是多组数据*多组数据。
2、博弈中威佐夫,Nim,巴什 的特点:
   巴什: 一堆石子,限定操作数范围1~n, 将n+1个石子捆绑在一起,你差多少个我都可以补上,反正取到最后的都是我噗哈哈

         威佐夫:两堆石子,两种取法,用归纳和数学分析的方法得出规律和公式,套公式即可
       
   Nim:  三堆石子,一种取法,异或值为1 一定可以转移成 异或值为0的必败态, 到必败态就是翻不了身的咸鱼啦【微笑脸】2333

#include <bits/stdc++.h>
using namespace std;

int main()
{
//freopen("1.txt","r",stdin);
int g;
while(cin>>g)
{
while(g--)
{
int n;
cin>>n;
int pile[100]={0};
for(int i=0;i<n;i++) cin>>pile[i];

int flag;
if(n==2)
{
int a0=min(pile[0],pile[1]);
int a1=max(pile[0],pile[1]);
int dis=a1-a0;
double suf=(sqrt(5.0)+1.0)/2.0;
if(floor((a1-a0)*suf)==a0) cout<<"Watson"<<endl;
else cout<<"Sherlock"<<endl;
}
else
{
int ans=0;
for(int i=0;i<n;i++) ans^=pile[i];
if(ans==0) cout<<"Watson"<<endl;
else cout<<"Sherlock"<<endl;
}
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: