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 ...
随机推荐
- IPMI设置与使用(远程控制服务器)
如果服务器crash了或者就hang住了,我们不必要跑到机房去按电源键的,因为我们也想“运筹帷幄之中,决胜千里之外”嘛.我们可以用IPMI,它可以让我们远程用一条命令开启(关闭.重启)一台服务器,也可 ...
- bzr: ERROR: No push location known or specified.
出现这种错误,要先uncommit,然后拉带最新版本,再commit最后push
- Hbase 系列(一)基本概念
Hbase 系列(一)基本概念 HBase 是 Apache 旗下一个高可靠性.高性能.面向列.可伸缩的分布式存储系统.利用 HBase 技术可在廉价 PC 服务器上搭建起大规模的存储化集群.使用 H ...
- jvm编译环境搭建 Debina篇
这里参考了 <Java虚拟机精讲> <深入理解Java虚拟机 JVM高级特性与最佳实践> http://www.cnblogs.com/zxfdream/p/5411511.h ...
- Android 上传文件到XP
Android部分: AsyncHttpClient client = new AsyncHttpClient(); RequestParams requestParams = new Request ...
- [operator]windows10 + zookeeper + kafka
软件版本 jdk-8u131-windows-x64 zookeeper-3.4.10.tar.gz kafka_2.11-2.0.1.tgz jdk直接就有exe安装版本,不做介绍 安装zookee ...
- 推送安霸A7L实时视频至RTMP服务器(1)
使用librtmp进行H264与AAC直播 (转:http://www.codeman.net/2014/01/439.html) 1.帧的划分 1.1 H.264帧 对于H.264而言每帧的界定符为 ...
- GNOME桌面的安装
首先搭建yum仓库 http://www.cnblogs.com/jw35/p/5967677.html yum grouplist #列出yum仓库里的软件组 ...
- 为Visual Studio添加一个“编码的UI测试生成器”的快捷方式
在添加CodedUI测试用例时,经常需要查看捕获控件的属性.按照常规的方式,只有在添加一个全新的CodedUI编码测试时才能查看捕获控件的属性,这样很不方便. 下面介绍在Visual Studio工具 ...
- PostgreSQL创建表及约束
创建表 语法: create table table_name ( column_name type column_constraint, table_constraint table_constra ...