[Algorithm] Dynamic Programming
2016-06-18 12:00
357 查看
Question 1.
Problem statement: Consider a row of n coins of values v1 . . . vn, where n is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin. Determine the maximum possible amount of money we can definitely win if we move first.
Solution from: http://www.geeksforgeeks.org/dynamic-programming-set-31-optimal-strategy-for-a-game/ by Aashish Barnwal. Accessed on 18th June, 2016.
There are two choices:
1. The user chooses the ith coin with value Vi: The opponent either chooses (i+1)th coin or jth coin. The opponent intends to choose the coin which leaves the user with minimum value.
i.e. The user can collect the value Vi + min(F(i+2, j), F(i+1, j-1) )
2. The user chooses the jth coin with value Vj: The opponent either chooses ith coin or (j-1)th coin. The opponent intends to choose the coin which leaves the user with minimum value.
i.e. The user can collect the value Vj + min(F(i+1, j-1), F(i, j-2) )
Following is recursive solution that is based on above two choices. We take the maximum of two choices.
Notes: 'minimisation' occurs because everyone wants to minmimise the opponent's total coin value. So you shall suppose that your opponent always takes a strategy that can minimise your total value of coins and then maximise his total value of coins, when it's your turn to choose the next coin form either the head or the tail of the list.
Problem statement: Consider a row of n coins of values v1 . . . vn, where n is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin. Determine the maximum possible amount of money we can definitely win if we move first.
Solution from: http://www.geeksforgeeks.org/dynamic-programming-set-31-optimal-strategy-for-a-game/ by Aashish Barnwal. Accessed on 18th June, 2016.
There are two choices:
1. The user chooses the ith coin with value Vi: The opponent either chooses (i+1)th coin or jth coin. The opponent intends to choose the coin which leaves the user with minimum value.
i.e. The user can collect the value Vi + min(F(i+2, j), F(i+1, j-1) )
2. The user chooses the jth coin with value Vj: The opponent either chooses ith coin or (j-1)th coin. The opponent intends to choose the coin which leaves the user with minimum value.
i.e. The user can collect the value Vj + min(F(i+1, j-1), F(i, j-2) )
Following is recursive solution that is based on above two choices. We take the maximum of two choices.
F(i, j) represents the maximum value the user can collect from i'th coin to j'th coin. F(i, j) = Max(Vi + min(F(i+2, j), F(i+1, j-1) ), Vj + min(F(i+1, j-1), F(i, j-2) )) Base Cases F(i, j) = Vi If j == i F(i, j) = max(Vi, Vj) If j == i+1
Notes: 'minimisation' occurs because everyone wants to minmimise the opponent's total coin value. So you shall suppose that your opponent always takes a strategy that can minimise your total value of coins and then maximise his total value of coins, when it's your turn to choose the next coin form either the head or the tail of the list.
相关文章推荐
- 【ppt入门教程】PPT2013如何加上自己公司LOGO PPT中加上LOGO操作方法
- go语言学习之路 一:开发环境配置
- go + LiteIDE +mac
- 搜索关注点--2014年的google关注点
- VR技术和Google的Demo
- mongo扩容
- 如何从官网下载 Google Chrome 离线安装包
- Mongoose Schemas定义中timestamps选项的妙用
- Django Hello World
- hrbust 1594 Gragon Ball【模拟】
- django后台启动+log重定向命令
- USACO 2008 Jan Gold 3.Cell Phone Network 树形dp
- 关于golang中map的key的类型
- golang函数传参方式总结
- Django 学习小组:博客开发实战第四周——标签云与文章归档
- go安装步骤
- NYOJ 635 Oh, my goddess (BFS + 优先队列)
- googleplay经验总结
- Goaccess 日志分析工具
- Visualizing Concurrency in Go · divan's blog