HDU 5795:A Simple Nim(博弈)

本文介绍了一种扩展版Nim游戏——ASimpleNim的策略解析,玩家可以选择拿取糖果或拆分堆叠。通过分析游戏状态的生成函数(SG函数),总结出了最优策略并提供了代码实现。

http://acm.hdu.edu.cn/showproblem.php?pid=5795

 

A Simple Nim

 

Problem Description
 
Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more interesting,players can separate one heap into three smaller heaps(no empty heaps)instead of the picking operation.Please find out which player will win the game if each of them never make mistakes.
 
Input
 
Intput contains multiple test cases. The first line is an integer  1T100, the number of test cases. Each case begins with an integer n, indicating the number of the heaps, the next line contains N integers s[0],s[1],....,s[n1], representing heaps with s[0],s[1],...,s[n1] objects respectively.(1n106,1s[i]109)
 
Output
 
For each test case,output a line whick contains either"First player wins."or"Second player wins".
 
Sample Input
 
2
2
4 4
3
1 2 4
 
Sample Output
 
Second player wins.
First player wins.
 
题意:和普通的Nim相似以外,还有一个操作,就是可以将一堆数量大于三的石头分成三份,每份可以不同(进行该操作的话该回合是不能取的)。
 1 #include <cstdio>
 2 #include <cstring>
 3 #define N 1000005
 4 int sg[N];
 5 /*
 6 官方题解:
 7 sg[0]=0
 8 
 9 当x=8k+7时sg[x]=8k+8,
10 
11 当x=8k+8时sg[x]=8k+7,
12 
13 其余时候sg[x]=x;(k>=0)
14 
15 打表找规律可得,数学归纳法可证。
16 */
17 void sg_do()
18 {
19     //学习打表找规律
20     sg[0] = 0;
21     bool vis[N];
22     for(int i = 1; i <= 100; i++) {
23         memset(vis, 0, sizeof(vis));
24         for(int j = 1; j <= i; j++) {
25             vis[sg[i-j]] = 1;
26             //拿走的情况
27         }
28         if(i >= 3) {
29             //只要对分成的三部分取异或,就代表x所能转移到的下一状态。
30             for(int j = 1; j <= i - 2; j++) {
31                 for(int k = 1; k <= i - 2; k++) {
32                     if(j + k < i) {
33                         vis[sg[i-j-k]^sg[j]^sg[k]] = 1;
34                         //将一堆拆成三份的情况
35                     }
36                 }
37             }
38         }
39         int j = 0;
40         while(vis[j]) j++;
41         sg[i] = j;
42         printf("%d : %d\n", i, sg[i]);
43     }
44 }
45 
46 int main()
47 {
48 //    sg_do();
49     int t;
50     scanf("%d", &t);
51     while(t--) {
52         int n, sg, x, ans = 0;
53         scanf("%d", &n);
54         for(int i = 1; i <= n; i++) {
55             scanf("%d", &x);
56             if((x+1) % 8 == 0) sg = x + 1;
57             else if(x % 8 == 0) sg = x - 1;
58             else sg = x;
59             ans ^= sg;
60         }
61         if(ans == 0) puts("Second player wins.");
62         else puts("First player wins.");
63     }
64     return 0;
65 }

 

转载于:https://www.cnblogs.com/fightfordream/p/5757171.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值