今天听了北美培训机构九章算法的一节公开课,做点心得笔录。
1.首先介绍什么是博弈问题,面试中博弈问题的特点
生活中的博弈问题
- 石头、剪刀、布
- tig \tag\toe
数学中的博弈问题
- 囚徒困境
面试中的博弈问题
- 这个游戏是两个人玩
- 这个游戏是两个人轮流玩
- 玩的时候两个人都要遵守一个规则
- 最后 告诉我他们谁先玩完
换句话说 就是:
- 两个玩家轮流游戏
- 每个人都基于局面有一个可以操作的集合(一般一致)
- 达到某个条件游戏结束
- 问是否必胜 或者 某位玩家的最大收益
2.引例 :Coins in a line(1)
There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins.
could you please decide the first player will win or lose?
稍加分析我们就会发现
n = 2 return true
n = 3 return false
n 是否是3的倍数
但是这是怎么来的呢?
3.重要的概念:
前提条件: - 游戏中的玩家是AlphaGo 级别,意味着一定会采取最聪明的策略
- 先手,后手 的概念
- 必胜 ? 必败?
- 先手必胜状态(= 后手必败状态,又叫N-position) next败
- 先手必败状态 (= 后手必胜状态,又叫P-position) previous败
先手后手是对当时的一个局面来说的,一个人在不同的局面下先手后手会转换
这里的三个概念的理解非常重要:
在这个图中,剩3个硬币是先手必败状态,因为对方一定会选择可以取胜的策略。
博弈问题,如果与动态规划的三个组成取得对应:状态表示,状态转移,状态初始化
4.例子
4.1取石子游戏
Two players(A and B) are playing a game with stones. Player A always plays first, and the two players move in alternating turns. In a single move , a player can remove 2,3,or 5 stones from the game board. If a player is unable to make a move, that player loses the game.
"我一出手后, 就可以置你于先手必败“ == "我先手必胜”
DP[i] = i 个石子是否先手必胜
DP[i] = !(DP[i-2]&&DP[i-3]&&DP[i-5])
4.2Coins in a line(2)
There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins.Could you please decide the first player will win or lose?
DP[i]准确描述应为 :还剩i个硬币时,先手能获得的最大值。
4.3Coins in a line(3)
There are n coins in a line.Two players take turns to take a coin from one of the ends of the line until there are no more coins. The player with the larger amount of money wins.Could you please decide the first player will win or lose?
这个题的思路和上一题还是一样,
"我一出手后, 就可以置你于先手必败“ == "我先手必胜”
4.4
如果在其四个子状态中,能找到一个先手必败状态,那其就是先手必胜状态
DP[i][j] = !(DP[i-2][j-1]&&DP[i-1][j-2]&&DP[i+1][j-2]&&DP[i-2][j+1])