题目描述:

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.

Example:

Input: 4
Output: false
Explanation: 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.

要完成的函数:

bool canWinNim(int n)

说明:

1、假设有一堆石头,你或者你的朋友每次能拿走1个或2个或3个石头,谁拿走了最后一个石头谁就赢。你是第一个拿的,你和你的朋友都非常聪明,每次做出的都是最优决策。现在给一个整数,表示石头的数量,返回你能不能赢。比如n=4,那么你无论如何都不能赢,因为无论你是拿了1个石头还是2个石头还是3个石头,你的朋友都能拿走最后的1个石头。

2、这道题笔者最开始以为又是一道动态规划的题目,但尝试了几个数之后,发现了规律。

n=1时,先出手的赢,所以你赢了。

n=2时,先出手的赢,所以你赢了

n=3时,先出手的赢,所以你赢了

n=4时,对方赢了,你输了。先出手的输了。

n=5时,你先出手,拿了1个石头,然后对方进入n=4这种状态,而且轮到对方出手了,所以他先出手就输了。所以你赢了,先出手的赢了。

n=6时,你先出手,拿了2个石头,然后对方进入n=4这种状态,而且轮到对方出手了,所以他先出手就输了。所以你赢了,先出手的赢了。

n=7时,你先出手,拿了3个石头,然后对方进入n=4这种状态,而且轮到对方出手了,所以他先出手就输了。所以你赢了,先出手的赢了。

n=8时,你先出手,无论你是拿了1/2/3个石头,对方都会进入n=7/6/5的状态,而且轮到对方出手了,他先出手就他赢了。你输了,先出手的输了。

n=9时,你先出手,拿了1个石头,然后对方进入n=8这种状态,而且轮到对方出手了,所以他先出手就输了。所以你赢了,先出手的赢了。

n=10时,你先出手,拿了2个石头,然后对方进入n=8这种状态,而且轮到对方出手了,所以他先出手就输了。所以你赢了,先出手的赢了。

n=11时,你先出手,拿了3个石头,然后对方进入n=8这种状态,而且轮到对方出手了,所以他先出手就输了。所以你赢了,先出手的赢了。

n=12时,你先出手,无论你是拿了1/2/3个石头,对方都会进入n=11/10/9的状态,而且轮到对方出手了,他先出手就他赢了。你输了,先出手的输了。

……

不知大家观察出来没有,只要n不是4的整数倍,你都可以控制对方进入n是4的整数倍,比如n=4或者n=8这种状态,然后对方先出手就对方输了。

而如果n是4的整数倍,那么就轮到对方控制你进入n是4的整数倍的状态,比如n=12,然后无论你怎么拿,对方都可以控制你进入n=8的状态,然后无论你再怎么拿,对方又控制你进入n=4的状态,然后n=4时,谁出手谁输。

所以代码很简单,如下:

    bool canWinNim(int n)
{
if(n%4==0)
return false;
return true;
}

上述代码实测2ms,beats 100.00% of cpp submissions。

leetcode-292-Nim Game(搬石子)的更多相关文章

  1. lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II

    变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...

  2. LN : leetcode 292 Nim Game

    lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...

  3. Java实现 LeetCode 292 Nim游戏

    292. Nim 游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...

  4. 【算法功底】LeetCode 292 Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  5. LeetCode 292. Nim Game (取物游戏)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  6. LeetCode 292. Nim Game

    Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...

  7. Java [Leetcode 292]Nim Game

    问题描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...

  8. LeetCode 292 Nim Game 解题报告

    题目要求 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...

  9. LeetCode 292 Nim Game(Nim游戏)

    翻译 你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头.每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮. 你们中的每个人都有着聪明的头脑和绝佳的策略.写一个函数来确 ...

  10. [LeetCode] 292. Nim Game_Easy tag: Math

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

随机推荐

  1. Visual Studio 2010 常用快捷方式

    调试快捷键 F6:           生成解决方案 Ctrl+F6:   生成当前项目 F7:           查看代码 Shift+F7:  查看窗体设计器 F5:           启动调 ...

  2. Castle ActiveRecord学习(七)使用日志

    暂无 参考:http://terrylee.cnblogs.com/archive/2006/04/14/374829.html

  3. 在JS中使用全局变量

    在JS中使用全局变量不像C++,也不像PHP. 先摆出错的代码: var globalvar = 'a'; function test(){ alert(globalvar); } 这里弹出的是und ...

  4. kafka分布式虚拟机群部署配置方法

    1 配置jdk8 假设安装(解压)路径:jdk1.8.0 修改/etc/profile,增加以下设置并保存 Export JAVA_HOME=jdk1.8.0 Export PATH=$JAVA_HO ...

  5. Golang template和junit xml report转html工具

    最近刚好有个task是要用Golang把Junit的XML格式report转换成HTML格式,便学习了Golang的template包. 基于template做的那个junit XML转HTML工具. ...

  6. Laravel 在哪些地方使用了 trait ?

    laravel 框架大量使用了traits. 简单举几个例子: 在Eloquent中使用了trait .然后在model初始化的时候,有个boot方法,会自动判断当前的类用了哪些trait.然后得到一 ...

  7. python 中numpy的var,std及cov

    var:表示方差, 即各项-均值的平方求和后再除以N , std:表示标准差,是var的平方根. cov:协方差 ,与var类似,但是除以(N-1)

  8. cannot be cast to

    java.lang.ClassCastException: com.service.impl.OrderPlanServiceImpl cannot be cast to com.provider.s ...

  9. 面试经验之——HE集团,YZ科技

    上周去了HE集团面试,该集团是做车辆辅助驾驶系统的,最终目标瞄准的是自动驾驶,加上再之前在YZ科技的面试经验,有些关于车辆驾驶上的心得想聊聊. 首先说说两个企业的面试情况吧.YZ科技中的两个技术聊天都 ...

  10. 编译语言 vs 解释语言

    编译语言 vs 解释语言 阅读:  评论:  作者:Rybby  日期:  来源:rybby.com 一直以为,编译语言的性能绝对比解释语言快,因为就理论而言,解释语言要一边解释(将脚本语言翻译成计算 ...