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

山东省17年省赛A:http://exam.upc.edu.cn/problem.php?id=3391

这题用了两种博弈一种是尼姆博弈针对        n==2时    而威佐夫博弈即n>=3时

一开始没看出来,这题不错,综合了两种博弈。

尼姆博弈是        if(       两堆差值×  (sqrt(5)+1)/2    ==    最小堆   )    后手赢,否则    先手赢;

威佐夫博弈是    if(n堆异或后的值==0)    后手赢,否则  先手赢。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        if(n==2){
            int a,b;
            scanf("%d%d",&a,&b);
            if(a>b)swap(a,b);
            int temp=(b-a)*(sqrt(5.0)+1)/(2.0);
            if(a==temp)printf("Watson\n");
            else{
                printf("Sherlock\n");
            }
        }else{
            int a[10004];
            scanf("%d",&a[0]);
            int temp=a[0];
            for(int i=1;i<n;i++){
                scanf("%d",&a[i]);
                temp^=a[i];
            }
            if(temp==0)printf("Watson\n");
            else{
                printf("Sherlock\n");
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/z_sea/article/details/80224193