题目描述:

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. 网页截图API接口,一键自动生成网页截图

    背景 最近在开发一个小程序,其中有一个帮助模块,内容为帮助文章列表,文章内容为网站后台编辑的富文本格式.鉴于小程序的特殊性,其对html格式的富文本支持并不友好. 刚开始有人开发了wxparse插件, ...

  2. [leetcode]273. Integer to English Words 整数转英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  3. Spring Boot 简单入门

    添加相关依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

  4. WebSocket 资料搜索

    http://jwebsocket.org/ http://zh.wikipedia.org/wiki/WebSocket http://www.infoq.com/cn/news/2013/07/e ...

  5. 安装python-empy

    sudo python setup.py install

  6. 怎样application不被第三方应用杀掉--Android

    方法: 对于放在/system/app下的应用,需要在其Manifest.xml文件中设置persistent属性,如应用程序'Phone'的AndroidManifest.xml文件: <ap ...

  7. java类的泛型DAO

    @Transactional public abstract class DAOSupport<T> implements DAO<T> { protected Class&l ...

  8. CodeForces 518B Tanya and Postcard (题意,水题)

    题意:给定两个字符串,然后从第二个中找和第一个相同的,如果大小写相同,那么就是YAY,如果大小写不同,那就是WHOOPS.YAY要尽量多,其次WHOOPS也要尽量多. 析:这个题并不难,难在读题懂题意 ...

  9. 解决CentOS7虚拟机无法上网并设置CentOS7虚拟机使用静态IP上网

    最近在VMware虚拟机里玩Centos,装好后发现上不了网.经过一番艰辛的折腾,终于找到出解决问题的方法了.最终的效果是无论是ping内网IP还是ping外网ip,都能正常ping通.方法四步走: ...

  10. (转)C# .net微信开发,开发认证,关注触发消息,自动应答,事件响应,自定义菜单

    原文地址:http://www.cnblogs.com/qidian10/p/3492751.html 成为开发者 string[] ArrTmp = { "token", Req ...