HDU 1846 Brave Game 公平组合博弈
最近学了组合博弈的内容挺有意思的,小时牛刀,AC,(*^__^*) 嘻嘻……
0是P状态,从1~M是N状态(至少有一种方法可以进入P状态),m+1是p状态(只能进入N状态)。。。。推理可知,m+1的倍数都是P 状态
方法一,简单推理:
View Code
/*
* Author:lonelycatcher
* Problem:hdu 1846
* Type:博弈
*/
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
int N,M;
int main()
{
setbuf(stdout,NULL);
int T;
cin>>T;
while(T--)
{
cin>>N>>M;
if(M>=N)
{
printf("first\n");
continue;
}
if(N%(M+1)==0)
{
printf("second\n");
}
else printf("first\n");
}
return 0;
}
方法二:GS函数
View Code
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
int N,M;
int GS[1010];
int visited[1010];
int main()
{
setbuf(stdout,NULL);
int T,i,j;
cin>>T;
while(T--)
{
cin>>N>>M;
if(M>=N)
{
printf("first\n");continue;
}
GS[0]=0;
for(i=1;i<=N;i++)
{
memset(visited,0,sizeof(visited));
for(j=i-1;j>=i-M&&j>=0;j--)
{
visited[GS[j]]=1;
}
j=0;
while(true)
{
if(visited[j]==0)
{
GS[i]=j;break;
}
j++;
}
}
if(GS[N])printf("first\n");
else printf("second\n");
}
return 0;
}
posted on 2011-08-19 14:41 lonelycatcher 阅读(268) 评论(0) 编辑 收藏 举报