POJ Game Theory

poj1704 Georgia and Bob

Topic links: http://poj.org/problem?id=1704

The meaning of problems: As shown, two people are playing a game, there are n pieces aligned on the grid, in turn, two pieces can move leftward lattice, but can not exceed the front piece, does not allow the two pieces on the same square inside, unable to move one side of the defeat, asked whether the upper hand to win a state.

Analysis: If n is even, then the pieces twenty-two into one group, into Nim, the piece is the grid between each number, the right to the left if the grid can be considered to take away the stones, if the grid on the left left, as long as the second person can be increased by subtracting the grid returns to its original state; if n is an odd number increased to a zero-zero coordinates pawn

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){
int a[1005],n,x;
cin>>x;
    while(x--){
        int n,x=0;
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i];
        if(n%2==1)
            a[n++]=0;
        sort(a,a+n);
        for(int i=0;i<n-1;i=i+2)
           x^=(a[i+1]-a[i]-1);
        if(x==0)
            cout<<"Bob will win"<<endl;
         else cout<<"Georgia will win"<<endl;
    }
}

poj2234 Matches Game Game Theory

Topic links: http://poj.org/problem?id=2234

Meaning of the questions: There are M pile of stones, a known quantity of each pile of stones, there are two players take turns selecting from a pile of stones from which it took a number of these, at least get one can take away all, ask whether the upper hand win

The most basic game of Nim, direct XOR operation

#include<cstdio>
#include<iostream>
using namespace std;
int main(){
    int n,a[25];
    while(cin>>n){
        for(int i=1;i<=n;i++)
            cin>>a[i];
            int x=a[1];
        for(int i=2;i<=n;i++)
            x=x^a[i];
        if(x!=0)
            cout<<"Yes"<<endl;
        else cout<<"No"<<endl;

    }
}

  

 

Guess you like

Origin www.cnblogs.com/dlutjwh/p/10988179.html