Return of the Nim(威佐夫加尼姆博弈)

Return of the Nim

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

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

  • There are n piles of stones denoted as ,,...,, 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≤kthe 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 ,,...,.

  • 1≤g≤15
  • 2≤n≤30, where n is a prime.
  • 1≤pilesi where 0≤in−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

两个操作:

1:从任意一堆中拿走k个石头;

2:在每堆中同时取走k个石头;

只看操作1, 就是一个裸的尼姆博弈, 当有两堆时是一个裸的威佐夫博弈, 两个博弈套在一起,  当n=2时就用威佐夫博弈, 反之就用尼姆博弈;

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
	int g;
	scanf("%d", &g);
	int p[100];
	while(g--){
		int n;
		scanf("%d", &n);
		for(int i=0; i<n; i++){
			scanf("%d", &p[i]);
		}
		if(n==2){
			if(p[0]>p[1]) swap(p[0], p[1]);
			int tmp = floor((p[1]-p[0])*(1+sqrt(5.0))/2.0);
			if(tmp==p[0]) printf("Watson\n");
			else printf("Sherlock\n");
		}
		else{
			int tmp=0;
			for(int i=0; i<n; i++){
				tmp^=p[i];
			}
			if(tmp==0) printf("Watson\n");
			else printf("Sherlock\n");
		}
	}
	return 0;
}



猜你喜欢

转载自blog.csdn.net/sirius_han/article/details/80182146