Grundy value of Poj 3537 crosses and crosses game theory

Source: Internet
Author: User

Test instructions

To 1*n lattice, take turns on the fork in the top, the first to draw 3 consecutive fork to win, ask the winning or will be defeated.

Analysis:

The Grundy value of the state (that is, the SG value), specifically how to find out the code, why so ask for their own thinking, imaginative achievement here (others say to see the game theory, hehe).

Code:

POJ 3537
//sep9
#include <iostream>
#include <set>
using namespace std;
int grundy[2048];
int h[2048];
int Get_grundy (int n)
{
	if (n<0)
		return 0;
	if (grundy[n]!=-1)
		return grundy[n];	
	int h[2048];
	memset (h,0,sizeof (h));
	for (int i=1;i<=n;++i) {
		int t=get_grundy (i-3) ^get_grundy (n-i-2);
		h[t]=1;
	}
	int i;
	for (i=0;h[i];++i);
	return grundy[n]=i;			
}
int main ()
{
	int n;
	Memset (Grundy,-1,sizeof (Grundy));
	Grundy[0]=0,grundy[1]=1,grundy[2]=1,grundy[3]=1;
	while (scanf ("%d", &n) ==1)
		if (Get_grundy (n)!=0)
			puts ("1");
		Else
			puts ("2");
	return 0;	


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.