动态规划动态规划

动态规划解决博弈问题(1)

2017-09-07  本文已影响0人  小双2510

今天听了北美培训机构九章算法的一节公开课,做点心得笔录。

1.首先介绍什么是博弈问题,面试中博弈问题的特点

生活中的博弈问题

数学中的博弈问题

面试中的博弈问题

换句话说 就是:

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 级别,意味着一定会采取最聪明的策略
- 先手,后手 的概念
- 必胜 ? 必败?

先手后手是对当时的一个局面来说的,一个人在不同的局面下先手后手会转换

Screen Shot 2017-09-07 at 10.24.19 AM.png

这里的三个概念的理解非常重要:


WechatIMG1.jpeg

在这个图中,剩3个硬币是先手必败状态,因为对方一定会选择可以取胜的策略。

博弈问题,如果与动态规划的三个组成取得对应:状态表示,状态转移,状态初始化


WechatIMG2.jpeg

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个硬币时,先手能获得的最大值。


Screen Shot 2017-09-07 at 12.19.54 PM.png

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?

这个题的思路和上一题还是一样,

"我一出手后, 就可以置你于先手必败“ == "我先手必胜”

WechatIMG4.jpeg

4.4

WechatIMG5.jpeg

如果在其四个子状态中,能找到一个先手必败状态,那其就是先手必胜状态
DP[i][j] = !(DP[i-2][j-1]&&DP[i-1][j-2]&&DP[i+1][j-2]&&DP[i-2][j+1])

上一篇 下一篇

猜你喜欢

热点阅读