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.
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?
分析
这是博弈论中极为经典的尼姆游戏。有总数为n的石头,每个人可以拿1~m个石头,两个人交替拿,拿到最后一个的人获胜。究竟是先手有利,还是后手有利?
1个石子,先手全部拿走;
2个石子,先手全部拿走;
3个石子,先手全部拿走;
4个石子,后手面对的是先手的第1,2,3情况,后手必胜;
5个石子,先手拿走1个让后手面对第4种情况,后手必败;
6个石子,先手拿走2个让后手面对第4种情况,后手必败;
……
容易看出来,只有当出现了4的倍数,先手无可奈何,其余情况先手都可以获胜。 (石子数量为4的倍数)
后手的获胜策略十分简单,每次取石子的数量,与上一次先手取石子的数量和为4即可; (石子数量不为4的倍数)先手的获胜策略也十分简单,每次都令取之后剩余的石子数量为4的倍数(4*0=0,直接拿光),他就处于后手的位置上,利用上一行的策略获胜。
AC代码
class Solution {
public:
bool canWinNim(int n) {
if (n % 4 == 0)
return false;
return true;
}
};
LeetCode(292) Nim Game的更多相关文章
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- DevExpress PivotGrid 使用记录
1.自定total值: 调试的时候,如果要定位,给一个index,然后,把e.CustomVale=index++;定位后,监视ds的值,每个ds的值不一样!
- undefined is not a function
具体报错 TypeError: c:\Users\Administrator\WebstormProjects\blogtest\views\index.ejs:1 >> 1| <% ...
- 【Java】 Maven多模块项目上传到Sonar扫描问题合集
上传到Soanr时,项目有单元测试数,但是覆盖率为0 修改pom.xml <plugin> <groupId>org.apache.maven.plugins</grou ...
- springboot集成shiro实现身份认证
github地址:https://github.com/peterowang/shiro pom文件 <dependencies> <dependency> <group ...
- 转:IOS程序之间的文件共享
原文 System-Declared Uniform Type Identifiers One of the common tasks that an iOS developer has to do ...
- ABAP扫雷游戏
. INCLUDE <icon>. CONSTANTS: " >> board cell values blank_hidden ', blank_marked TY ...
- window之间、iframe之间的JS通信
一.Window之间JS通信 在开发项目过程中,由于要引入第三方在线编辑器,所以需要另外一个窗口(window),而且要求打开的window要与原来的窗口进行js通信,那么如何实现呢? 1.在原窗口创 ...
- WinForm 公共控件和属性
Button 按钮 布局 AutoSize 内容超出部分是否扩展到适应尺寸大小 Location 位置坐标 Size 控件大小 行为 Enabled 控件是否启用 visible 控件 ...
- LibreOJ #100. 矩阵乘法
内存限制:256 MiB 时间限制:2000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 上传者: 匿名 模版 以前一直不过样例原来是读入优化没写负数.. 屠龙宝刀点击就送 #in ...
- Cocos2d-x——导入Cocostudio资源
(搬运自我在SegmentFault的博客) 目前正在和实训的小组成员一起做一款手机2D游戏,我们采用了Cocos2d-x进行开发.之前虽然早有耳闻,这次却是第一次认真地学习和使用Cocos2d-x. ...