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. phpcms:三、头部包含

    1‘标题:{if isset($SEO['title']) && !empty($SEO['title'])}{$SEO['title']}{/if}{$SEO['site_title ...

  2. C# - 使用ConfigurationManager保存数据到App.config

    1. ConfigurationManager的命名空间:using System.Configuration; 2. To be able to save you have to use a con ...

  3. input文本框获取焦点和失去焦点判断

    onBlur:当输入框失去焦点后 onFocus:当输入框获得焦点后 这两个JavaScript事件是写在html标签中的例如: <input type="text" onB ...

  4. CF Zepto Code Rush 2014 B. Om Nom and Spiders

    Om Nom and Spiders time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  5. python 间谍程序传输文件 socket编程

    本程序实现了,把目标机器的某个目录(可控)的所有的某种类型文件(可控)全部获取并传到己方的机器上. 1.用了base64的encode(infile,outfile)加密,以及decode(infil ...

  6. windows 删除多层文件夹

    眼下目录d:\clu_1下有99个目录,名字为0,1....99,每一个目录下又有25个目录,目录名为0,1,....24,其下其下又有以日期命名的目录(如20140521),最后是zip文件.如一个 ...

  7. Oracle 用户、对象权限、系统权限

    --================================ --Oracle 用户.对象权限.系统权限 --================================  一.用户与模式 ...

  8. java基础之代理

    代理的定义,代理的应用,代理的特性

  9. React初步

    今天整理一下自己关于react的学习笔记. 什么是React? 学习某一个框架首先得知道这个框架是干什么的,它的特点是什么,有哪些优点和缺点. React有4个特点 组件化 虚拟DOM 单项数据流 j ...

  10. WPF DataGrid 之数据绑定--实例2

    1.前台Grid定义 <!--数据绑定--> <DataGrid Grid.Row="1" Name="gridOne" Margin=&qu ...