Question

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.

Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.

For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.

Hint:

  1. If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?

Solution 1 -- DP

传统的方法是DP。我们认为比赛只有两个状态:win, not win

F(x) = (!F(x - 1)) || (!F(x - 2)) || (!F(x - 3))

当对手不是全赢时,玩家即是赢。

Time complexity O(n), space cost O(1)

 public class Solution {
public boolean canWinNim(int n) {
boolean first = true, second = true, third = true;
for (int i = 3; i < n; i++) {
boolean tmp = (!first) || (!second) || (!third);
first = second;
second = third;
third = tmp;
}
return third;
}
}

Solution 2 -- Math

这道题其实是个数学问题。根据Discuss上的帖子,有一个结论:

Theorem: The first one who got the number that is multiple of 4 (i.e. n % 4 == 0) will lost, otherwise he/she will win.

1. Base case

When number = 4, the player loses.

2. Induction

Hypothesis: we assume that number = 4 * k, the player will lose.

When number = 4 * (k + 1), after the player moves stones, the number becomes (4 * k + 3) or (4 * k + 2) or (4 * k + 3). So the advisor can always make number for next round to be 4 * k. Thus, the player will lose.

 public class Solution {
public boolean canWinNim(int n) {
return !((n % 4) == 0);
}
}

Nim Game 解答的更多相关文章

  1. 编程之美----NIM游戏

    : 博弈游戏·Nim游戏 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 今天我们要认识一对新朋友,Alice与Bob.Alice与Bob总是在进行各种各样的比试,今天他 ...

  2. 第1章 游戏之乐——NIM(2)“拈”游戏分析

    NIM(2)“拈”游戏分析 1. 问题 有N块石头和两个玩家A和B,玩家A先将石头分成若干堆,然后按照BABA……的顺序不断轮流取石头,能将剩下的石头一次取光的玩家获胜.每次取石头时,每个玩家只能从若 ...

  3. 第1章 游戏之乐——NIM(1)一排石子的游戏

    NIM(1)一排石子的游戏 转载:编程之美-MIN(1)一排石头的游戏 1. 原题 1.1 题目 N块石头排成一行,每块石头有各自固定的位置.两个玩家依次取石头,每个玩家每次可以取其中任意一块石头,或 ...

  4. (转载)Nim游戏博弈(收集完全版)

    Nim游戏的概述: 还记得这个游戏吗?给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠,但不能在两列中取.最后拿光珍珠的人输.后来,在一份资料上看到,这种游戏称为“拈(Nim)”.据说,它源 ...

  5. Nim游戏博弈

    Nim游戏的概述: 还记得这个游戏吗? 给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠,但不能在两列中取.最后拿光珍珠的人输. 后来,在一份资料上看到,这种游戏称为"拈(Nim) ...

  6. 博弈论之Nim

    博弈论(一):Nim游戏 重点结论:对于一个Nim游戏的局面(a1,a2,...,an),它是P-position当且仅当a1^a2^...^an=0,其中^表示位异或(xor)运算. Nim游戏是博 ...

  7. 【01_292】Nim Game

    Nim Game Total Accepted: 25342 Total Submissions: 50672 Difficulty: Easy You are playing the followi ...

  8. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  9. acm之简单博弈 Nim Bash Wythoff

    前些日子我打算开了博弈基础,事后想进行总结下 一句话就是分析必胜或必败,异或为0. 以下内容来自转载: Nim游戏的概述: 还记得这个游戏吗?给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠 ...

随机推荐

  1. centos Minicom通信终端

    minicom是linux下的串口通信软件,他使用完全使用键盘操作.它虽然没有windows下的超级终端好用,但是它也是一种串口通信的方法.一.minicom安装在超级终端中输入:sally@sall ...

  2. poj 3187 Backward Digit Sums(穷竭搜索dfs)

    Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...

  3. #python基础学习模块:marshal 对象的序列化

    #标准库地址:https://docs.python.org/2/library/marshal.html"""有时候,要把内存中一个对象持久化保存磁盘或者序列化二进制流 ...

  4. HLS直播和时移项目上线

    兄弟姐妹们: 天翼视讯的高清HLS直播和时移项目上线,测试观看地址为:http://118.85.192.228/live/,同时支持移动终端和pc终端.移动终端支持ios client和androi ...

  5. HashMap的存储结构及原理

    1.HashMap的数据结构(HashMap通过hashcode对其内容进行高速查找,是无序的) 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组 :数组的存储区是连续的,占 ...

  6. MongoDB[mark]总忘记它们是干啥的

    MongoDB集群包括一定数量的mongod(分片存储数据).mongos(路由处理).config server(配置节点).clients(客户端).arbiter(仲裁节点:为了选举某个分片存储 ...

  7. py爬取英文文档学习单词

    最近开始看一些整本整本的英文典籍,虽然能看个大概,但是作为四级都没过的我来说还是有些吃力,总还有一部分很关键的单词影响我对句子的理解,因为看的是纸质的,所以查询也很不方便,于是想来个突击,我想把程序单 ...

  8. CSS flex 布局 一些基本属性应用

    作用于伸缩盒元素上的属性 box-orient .box-pack.box-align.box-direction.box-lines box-orient box-orient:horizontal ...

  9. scala中的implict

    1.作为隐式参数 object Test { def main(args: Array[String]) { import FruitColor._ Fruit.get("apple&quo ...

  10. redis的备份和恢复

    Redis 数据备份与恢复 数据备份 语法 redis Save 命令基本语法如下: redis > SAVE 实例 redis > SAVE OK 该命令将在 redis 备份目录中创建 ...