herrDeng網內搜尋

自訂搜尋

Ads

2023年5月28日 星期日

動態規劃C++解Leetcode 1406難題零和遊戲stone game 3


動態規劃C++解#Leetcode 1406難題零和遊戲#stonegame3。 這是零和賽局,表示所有賽局方的利益之和為一常數,即一方有所得,其他方必有所失。Alice 和 Bob輪流, Alice先開始。 在每位玩家的回合中,該玩家可以從該行剩餘的第一顆石頭中拿走 1、2 或 3 顆石頭。

每個玩家的得分是所取石頭值的總和。 解法先找出關係式,可以先有遞迴版,再加上有記憶體紀錄每次的狀態,其實就是動態規劃。紀錄石頭但不是石頭記 (紅樓夢)!
 
[LeetCode程式]https://www.youtube.com/watch?v=--vVXnaKPqI&list=PLYRlUBnWnd5IdDHk2BjqXwesydU17z_xk 
============
 Dynamic programming C++ solution Leetcode 1406 puzzle zero sum game stone game 3. This is a zero-sum game, which means that the sum of the interests of all parties in the game is a constant, that is, if one party gains, the other party must lose. 

Alice and Bob take turns, with Alice starting first. On each player's turn, that player may take 1, 2 or 3 stones from the first remaining stones in the row. Each player's score is the sum of the stone values taken. 

The solution is to first find out the relational expression, you can have a recursive version first, and then there is a memory to record each state, which is actually dynamic programming.  Record stones but not the Story of the Stone (Dream of the Red Chamber)!

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章