Skip to content

Commit ae5c47b

Browse files
committed
Solve 292 Nim Game
1 parent a106441 commit ae5c47b

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

solutions/292.Nim_Game/AC_nim_1.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Author: illuz <iilluzen[at]gmail.com>
3+
* File: AC_nim_1.cpp
4+
* Create Date: 2016-02-22 19:44:50
5+
* Descripton:
6+
*/
7+
8+
#include <bits/stdc++.h>
9+
10+
using namespace std;
11+
const int N = 0;
12+
13+
class Solution {
14+
public:
15+
bool canWinNim(int n) {
16+
return n % 4 != 0;
17+
}
18+
};
19+
20+
int main() {
21+
22+
return 0;
23+
}
24+

solutions/292.Nim_Game/readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## 292. Nim Game (Easy)
2+
3+
### **链接**
4+
题目:https://leetcode.com/problems/nim-game/
5+
代码(github):https://github.com/illuz/leetcode
6+
7+
### **题意**
8+
很经典的博弈游戏,两个人从一堆石头中轮流取出 1-3 个石头,谁先取完谁就赢了。
9+
你是先手,现在问如果石头有 n 个,你能不能赢。
10+
11+
### **分析**
12+
13+
> 这个游戏其实不管从推理还是从结论上说都和文章开头的游戏一样,不过这次我们尝试找出更普遍的规律。因为石子的数量总是递减的,所以这个游戏必将在有限步(15步)内结束。我们可以用余下石子的数目来表示局面,于是一共有16个局面。因为规定了拿走最后一个石子的人赢,换句话说当石子个数为0时,就是一个“轮到谁谁就输”的局面,我们通常叫这种局面为必败态。既然0是必败态,那么当局面为1,2,3时,先手就可以采取规则所允许的策略(拿走1个,2个,或是3个)来把局面变成0,于是称1,2,3为胜态;而当局面为4时,不论采取何种策略,局面都将走向胜态,从而4是一个必败态。
14+
15+
可以去了解下,入门:[博弈入门:从数学游戏开始](http://www.guokr.com/article/500/),进阶的就去看 [Nim 的 Wiki](https://en.wikipedia.org/wiki/Nim) 吧。
16+
17+

0 commit comments

Comments
 (0)