leetcode-292-Nim Game(搬石子)
题目描述:
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(搬石子)的更多相关文章
- 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个石子之后都是必胜,则当前必败 ...
- 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 ...
- Java实现 LeetCode 292 Nim游戏
292. Nim 游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...
- 【算法功底】LeetCode 292 Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- LeetCode 292. Nim Game (取物游戏)
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- LeetCode 292. Nim Game
Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...
- Java [Leetcode 292]Nim Game
问题描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...
- LeetCode 292 Nim Game 解题报告
题目要求 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...
- LeetCode 292 Nim Game(Nim游戏)
翻译 你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头.每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮. 你们中的每个人都有着聪明的头脑和绝佳的策略.写一个函数来确 ...
- [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 ...
随机推荐
- Linux内核分析 - 网络[十四]:IP选项
Linux内核分析 - 网络[十四]:IP选项 标签: linux内核网络structsocketdst 2012-04-25 17:14 5639人阅读 评论(1) 收藏 举报 分类: 内核协议栈 ...
- python教学视频
3 X王的 链接: http://pan.baidu.com/s/1bW2UrO 密码: quit4 django 链接: http://pan.baidu.com/s/1o8UsNDS 密码: 8x ...
- JQuery 对象和事件
JQuery 对象和事件 一:JQuery 对象和 Dom 对象 在使用 JQuery 过程中,我们一般(也是绝大多数情况下,除非我们使用了第二个框架)只有两类对象,即:JQuery 对象和 Dom ...
- Template Method Design Pattern in Java
Template Method is a behavioral design pattern and it’s used to create a method stub and deferring s ...
- R12 查询EBS用户相关SQL
--R12查询EBS在线用户SQL SELECT U.USER_NAME, APP.APPLICATION_SHORT_NAME, FAT.APPLICATION_NAME, ...
- Android-应用安装/替换/卸载/广播监听
在上一篇博客Android-开关机的广播,中介绍了,如何订阅接收者,去接收系统发送的开机/关机广播, 而这篇博客是订阅接收者 去接收应用的(安装/替换/卸载) 三种广播 订阅 接收者 去接收 应用的 ...
- 作业二:注册软件github
注册Github
- Java和.net对比分析
.Net和Java是国内市场占有率最高的两门技术,对于准备学习编程语言的初学者来说,.Net和Java是初学者首先考虑的两门技术,因此很多人一遍遍的问“学.Net还是学Java”,社区中也每天都有“. ...
- Ubuntu 16.04.2 LTS 安装 jdk1.6 和 tomcat6 (一)
java和tomcat环境配置已经有很多教程和文章,最近项目需要配置Ubuntu 16.04.2下的古老的java6和tomcat 6,遇到小坑,特记录和分享. 网上的教程不是太新,就是太老,还有一些 ...
- subprocess.Popen命令如何隐藏弹框
在用PYQT编写GUI界面时,代码中有用到subprocess.Popen(),打包exe后每次遇到subprocess语句是就会弹出命令框,很是头疼, 下面是解决的办法 import subproc ...