↓ 跳过正文
  1. Posts/

hdu 1849Rabbit and Grass(一维nim游戏,sg函数)

·1290 字·3 分钟

Rabbit and Grass
#

**Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3058 Accepted Submission(s): 2261 **

Problem Description

大学时光是浪漫的,女生是浪漫的,圣诞更是浪漫的,但是Rabbit和Grass这两个大学女生在今年的圣诞节却表现得一点都不浪漫:不去逛商场,不去逛公园,不去和AC男约会,两个人竟然猫在寝食下棋…… 说是下棋,其实只是一个简单的小游戏而已,游戏的规则是这样的: 1、棋盘包含1*n个方格,方格从左到右分别编号为0,1,2,…,n-1; 2、m个棋子放在棋盘的方格上,方格可以为空,也可以放多于一个的棋子; 3、双方轮流走棋; 4、每一步可以选择任意一个棋子向左移动到任意的位置(可以多个棋子位于同一个方格),当然,任何棋子不能超出棋盘边界; 5、如果所有的棋子都位于最左边(即编号为0的位置),则游戏结束,并且规定最后走棋的一方为胜者。

对于本题,你不需要考虑n的大小(我们可以假设在初始状态,棋子总是位于棋盘的适当位置)。下面的示意图即为一个1*15的棋盘,共有6个棋子,其中,编号8的位置有两个棋子。

大家知道,虽然偶尔不够浪漫,但是Rabbit和Grass都是冰雪聪明的女生,如果每次都是Rabbit先走棋,请输出最后的结果。

Input

输入数据包含多组测试用例,每个测试用例占二行,首先一行包含一个整数m(0<=m<=1000),表示本测试用例的棋子数目,紧跟着的一行包含m个整数Ki(i=1…m; 0<=Ki<=1000),分别表示m个棋子初始的位置,m=0则结束输入。

Output

如果Rabbit能赢的话,请输出“Rabbit Win!”,否则请输出“Grass Win!”,每个实例的输出占一行。

Sample Input

2 3 5 3 3 5 6 0

Sample Output

Rabbit Win! Grass Win!

Author

lcy

Source

ACM Short Term Exam_2007/12/13

是一个略有变形的nim游戏模型

如果在i位置上有 k个棋子

我们可以看做有k堆数量为i的堆

可以向左移动1~i步

就可以看做每次从数量为 i 的堆中拿走数量 1~i

这样就是 nim 游戏。

而 nim 游戏的结论是:异或和为 0 必败。

代码实现
 1/*************************************************************************
 2	> File Name: code/hdu/1849.cpp
 3	> Author: 111qqz
 4	> Email: rkz2013@126.com
 5	> Created Time: 2015年09月22日 星期二 20时37分46秒
 6 ************************************************************************/
 7
 8#include<iostream>
 9#include<iomanip>
10#include<cstdio>
11#include<algorithm>
12#include<cmath>
13#include<cstring>
14#include<string>
15#include<map>
16#include<set>
17#include<queue>
18#include<vector>
19#include<stack>
20#include<cctype>
21#define y1 hust111qqz
22#define yn hez111qqz
23#define j1 cute111qqz
24#define ms(a,x) memset(a,x,sizeof(a))
25#define lr dying111qqz
26using namespace std;
27#define For(i, n) for (int i=0;i<int(n);++i)
28typedef long long LL;
29typedef double DB;
30const int inf = 0x3f3f3f3f;
31int m;
32int main()
33{
34  #ifndef  ONLINE_JUDGE
35   freopen("in.txt","r",stdin);
36  #endif
37    while (scanf("%d",&m)!=EOF&&m)
38    {
39	int sum = 0 ;
40	for ( int i = 0 ;i < m ; i++)
41	{
42	    int x;
43	    scanf("%d",&x);
44	    sum ^= x;
45	}
46	//cout<<sum<<endl;
47	if (sum)
48	{
49	    puts("Rabbit Win!");
50	}
51	else
52	{
53	    puts("Grass Win!");
54	}
55    }
56
57 #ifndef ONLINE_JUDGE
58  fclose(stdin);
59  #endif
60	return 0;
61}

同样,我们可以用sg函数来解决。 ** m个棋子可以认为是m个1个棋子的游戏的和。** ** 而游戏的和的sg值就是所有子游戏的sg值的异或。**

代码实现
 1/*************************************************************************
 2	> File Name: code/hdu/1849.cpp
 3	> Author: 111qqz
 4	> Email: rkz2013@126.com
 5	> Created Time: 2015年09月22日 星期二 20时37分46秒
 6 ************************************************************************/
 7
 8#include<iostream>
 9#include<iomanip>
10#include<cstdio>
11#include<algorithm>
12#include<cmath>
13#include<cstring>
14#include<string>
15#include<map>
16#include<set>
17#include<queue>
18#include<vector>
19#include<stack>
20#include<cctype>
21#define y1 hust111qqz
22#define yn hez111qqz
23#define j1 cute111qqz
24#define ms(a,x) memset(a,x,sizeof(a))
25#define lr dying111qqz
26using namespace std;
27#define For(i, n) for (int i=0;i<int(n);++i)
28typedef long long LL;
29typedef double DB;
30const int inf = 0x3f3f3f3f;
31const int N=1E3+7;
32bool vis[N];
33int sg[N];
34int m;
35
36
37void sg_init()
38{
39    for ( int i = 1 ; i < N ; i++)
40    {
41	ms(vis,false);
42	for ( int j = 0 ; j <= i ; j++)
43	    vis[sg[i-j]] = true;
44
45	for ( int j = 0 ; ; j++)
46	    if (!vis[j])
47	    {
48		sg[i] = j;
49		break;
50	    }
51    }
52}
53int main()
54{
55  #ifndef  ONLINE_JUDGE
56   freopen("code/in.txt","r",stdin);
57  #endif
58
59   sg_init();
60    while (scanf("%d",&m)!=EOF&&m)
61    {
62	int sum = 0 ;
63	for ( int i = 0 ;i < m ; i++)
64	{
65	    int x;
66	    scanf("%d",&x);
67	    sum ^= sg[x];
68	}
69	//cout<<sum<<endl;
70	if (sum)
71	{
72	    puts("Rabbit Win!");
73	}
74	else
75	{
76	    puts("Grass Win!");
77	}
78    }
79
80 #ifndef ONLINE_JUDGE
81  fclose(stdin);
82  #endif
83	return 0;
84}

相关文章

hdu 2149Public Sale(博弈论 巴什博奕)

·218 字·1 分钟
hdu 2149题目链接 题意&思路:巴什博奕,点m是n点。。。然后往前画即可。。。 代码实现 1/************************************************************************* 2 > File Name: code/hdu/2149.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年09月22日 星期二 20时18分02秒 6 ************************************************************************/ 7 8#include<iostream> 9#include<iomanip> 10#include<cstdio> 11#include<algorithm> 12#include<cmath> 13#include<cstring> 14#include<string> 15#include<map> 16#include<set> 17#include<queue> 18#include<vector> 19#include<stack> 20#include<cctype> 21#define y1 hust111qqz 22#define yn hez111qqz 23#define j1 cute111qqz 24#define ms(a,x) memset(a,x,sizeof(a)) 25#define lr dying111qqz 26using namespace std; 27#define For(i, n) for (int i=0;i<int(n);++i) 28typedef long long LL; 29typedef double DB; 30const int inf = 0x3f3f3f3f; 31int n,m; 32int main() 33{ 34 #ifndef ONLINE_JUDGE 35 freopen("in.txt","r",stdin); 36 #endif 37 while (scanf("%d %d",&m,&n)!=EOF) 38 { 39 if (m<n+1) 40 { 41 printf("%d",m); 42 for ( int i = m+1 ; i <= n; i++) 43 printf(" %d",i); 44 printf("\n"); 45 continue; 46 } 47 int tmp = m%(n+1); 48 if (tmp>0) 49 { 50 printf("%d\n",tmp); 51 } 52 else 53 { 54 puts("none"); 55 } 56 57 } 58 59 #ifndef ONLINE_JUDGE 60 fclose(stdin); 61 #endif 62 return 0; 63}

hdu 2188 悼念512汶川大地震遇难同胞——选拔志愿者 (巴什博奕)

·195 字·1 分钟
题目链接:hdu 2188题目链接 题意&思路:巴什博奕。。画n点p点。。。 代码实现 1/************************************************************************* 2 > File Name: code/hdu/2188.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年09月22日 星期二 20时08分08秒 6 ************************************************************************/ 7 8#include<iostream> 9#include<iomanip> 10#include<cstdio> 11#include<algorithm> 12#include<cmath> 13#include<cstring> 14#include<string> 15#include<map> 16#include<set> 17#include<queue> 18#include<vector> 19#include<stack> 20#include<cctype> 21#define y1 hust111qqz 22#define yn hez111qqz 23#define j1 cute111qqz 24#define ms(a,x) memset(a,x,sizeof(a)) 25#define lr dying111qqz 26using namespace std; 27#define For(i, n) for (int i=0;i<int(n);++i) 28typedef long long LL; 29typedef double DB; 30const int inf = 0x3f3f3f3f; 31void solve() 32{ 33 int n,m; 34 scanf("%d %d",&n,&m); 35 if (n%(m+1)!=0) 36 { 37 puts("Grass"); 38 } 39 else 40 { 41 puts("Rabbit"); 42 } 43} 44int main() 45{ 46 #ifndef ONLINE_JUDGE 47 freopen("in.txt","r",stdin); 48 #endif 49 int T; 50 cin>>T; 51 while (T--) 52 { 53 solve(); 54 } 55 #ifndef ONLINE_JUDGE 56 fclose(stdin); 57 #endif 58 return 0; 59}

acm博弈论

·4614 字·10 分钟
**序:**博弈是信息学和数学试题中常会出现的一种类型,算法灵活多变是其最大特点,而其中有一类试题更是完全无法用常见的博弈树来进行解答。 寻找必败态即为针对此类试题给出一种解题思路。

nim遊戲的必勝策略(博弈論)

·734 字·2 分钟
假设有n堆石子,每堆石子的个数分别如下 a1, a2, a3, … an 定义nim-sum为a1^a2^a3…an 可以证明 1. 若a1^a2^a3…an != 0 则经过一次合法的移动之后必定可变成