POJ 1704 Georgia and Bob (Variations of the nim game)

The meaning of the question : There are n chess pieces on the grid arranged in a straight line. The i-th chess piece is on the grid of p[i]. G and B select a chess piece to move to the left, and each time can move any square, but cannot overtake the others A square, and two pieces cannot be placed in one square, G first, the one that cannot move loses, and who will win?

Idea : We regard the difference of each grid as the number of piles of stones in the nim, then every time we move the stones to the left, we can regard the left side of the current stone as picking out any stone from each pile of stones Now, for the right side of the current stone, it is actually adding any stone to the pile of stones, then if it is solved, it is simple, just say that he moves the same grid, and it becomes the same state as before. Then in fact, it is just to differentiate the p array, and then nim it.

The code above puts:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 10000+10;
int a[maxn];
intmain()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		memset(a,0,sizeof(a));
		for(int i = 0 ; i < n ; i++)
		{
			scanf("%d",&a[i]);
		}
		if(n&1) a[n++] = 0;//If it is an odd number, we add a 0
		sort(a,a+n);
		int years = 0;
		for(int i = 0 ; i+1 < n ; i = i+2)
		{
			years = years^(a[i+1] - a[i] - 1);
		}
		if(ans==0) puts("Bob will win");
		else puts("Georgia will win");
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324808762&siteId=291194637
Recommended