Nim Game 解答
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:
- 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 解答的更多相关文章
- 编程之美----NIM游戏
: 博弈游戏·Nim游戏 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 今天我们要认识一对新朋友,Alice与Bob.Alice与Bob总是在进行各种各样的比试,今天他 ...
- 第1章 游戏之乐——NIM(2)“拈”游戏分析
NIM(2)“拈”游戏分析 1. 问题 有N块石头和两个玩家A和B,玩家A先将石头分成若干堆,然后按照BABA……的顺序不断轮流取石头,能将剩下的石头一次取光的玩家获胜.每次取石头时,每个玩家只能从若 ...
- 第1章 游戏之乐——NIM(1)一排石子的游戏
NIM(1)一排石子的游戏 转载:编程之美-MIN(1)一排石头的游戏 1. 原题 1.1 题目 N块石头排成一行,每块石头有各自固定的位置.两个玩家依次取石头,每个玩家每次可以取其中任意一块石头,或 ...
- (转载)Nim游戏博弈(收集完全版)
Nim游戏的概述: 还记得这个游戏吗?给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠,但不能在两列中取.最后拿光珍珠的人输.后来,在一份资料上看到,这种游戏称为“拈(Nim)”.据说,它源 ...
- Nim游戏博弈
Nim游戏的概述: 还记得这个游戏吗? 给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠,但不能在两列中取.最后拿光珍珠的人输. 后来,在一份资料上看到,这种游戏称为"拈(Nim) ...
- 博弈论之Nim
博弈论(一):Nim游戏 重点结论:对于一个Nim游戏的局面(a1,a2,...,an),它是P-position当且仅当a1^a2^...^an=0,其中^表示位异或(xor)运算. Nim游戏是博 ...
- 【01_292】Nim Game
Nim Game Total Accepted: 25342 Total Submissions: 50672 Difficulty: Easy You are playing the followi ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- acm之简单博弈 Nim Bash Wythoff
前些日子我打算开了博弈基础,事后想进行总结下 一句话就是分析必胜或必败,异或为0. 以下内容来自转载: Nim游戏的概述: 还记得这个游戏吗?给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠 ...
随机推荐
- UVA514 Rails
铁轨 PopPush城市有一座著名的火车站.这个国家到处都是丘陵.而这个火车站是建于上一个世纪.不幸的是,那时的资金有限.所以只能建立起一条路面铁轨.而且,这导致这个火车站在同一个时刻只能一个轨道投 ...
- c语言typedef之数组运用
#include <stdio.h> #include <stdlib.h> typedef ];//int arr[4]取一个别名 arr b c就是int b[4] int ...
- c语言结构体4之结构体引用
struct mystruct{ char str[23];}; 1结构体变量不能整体引用 struct data m: printf("%s",m);//m是结构体变量 2 st ...
- 关于bootstrap--表单(按钮<button>效果、大小、禁用)
1.各种标签实现按钮效果: <button class="btn btn-default" type="button">button标签按钮< ...
- jQuery 各种选择器 $.()用法
jQuery 元素选择器jQuery 使用 CSS 选择器来选取 HTML 元素. $("p") 选取 <p> 元素. $("p.intro") 选 ...
- C# 插入排序算法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- XMPP通讯开发-服务器好友获取以及监听状态变化
在 XMPP通讯开发-好友获取界面设计 我们设计了放QQ的列表功能,这里我们获取我们服务器上的 数据. 这一部分知识我们可以查看smack_3_3_0/smack_3_3_0/documentat ...
- 转载: Javah生成JNI头文件出现找不到类的错误
错误: 找不到 'com.chnic.jni.SayHellotoCPP' 的类文件. 上图可以看到错误和解决办法. 不要忘记那个点 javah -classpath . -jni com.chnic ...
- JS高级程序设计学习笔记之RegExp类型
创建正则表达式: 字面量形式定义正则表达式: Var expression = / pattern /flags ;pattern部分可以使任意简单或复杂的正则表达式.每个正则表达式可以带有一个或多个 ...
- android default_workspace.xml
//default_workspace.xml中,支持的标签有: favorite:应用程序快捷方式. shortcut:链接,如网址,本地磁盘路径等. search:搜索框. clock:桌面上的钟 ...