algorithm - Calculate the winning strategy of a subtraction game -


problem:
given 100 stones, 2 players alternate take stones out. 1 can take number 1 15; however, 1 cannot take number taken. if in end of game, there k stones left, 1 through k have been taken, 1 can take k stones. 1 takes last stone wins. how can first player win?

my idea
use recursion (or dynamic programming). base case 1, player 1 has winning strategy. reducing: n stones left, if palyer 1 takes m1 stones, has ensure options player 2 has (m2), has winning strategy. problem reduced (n - m1 - m2).

follow question:
if 1 uses dp, potential number of tables filled large (2^15), since available options left depend on history, has 2^15 possibilities.
how can optimize?

assuming set of numbers remaining can represented r, highest number remaining after selection can represented rh , lowest number remaining can rl, trick use second-to-last move raise number <100-rh, >100-rh-rl. forces opponent take number put in winning range.

the final range of winning, total number create second-to-last move, is:

n < 100-rh n > 100-rh-rl 

by observation noted rh can high 15 , low 8. rl can low 1 , high 13. range evaluated equations.

n < 100-[8:15]         => n < [92:85] n > 100-[8:15]-[1:13]  => n > [92:85] - [1:13] => n > [91:72] 

other considerations can narrow gap. rl, instance, 13 in edge circumstance results in loss player a, true range between 72 , 91. there similar issue rh , low end of it, final ranges , calculations are:

n < 100-[9:15]         => n < [91:85] n > 100-[9:15]-[1:12]  => n > [91:85] - [1:12] => n > [90:73] [90:73] < n < [91:85] 

before this, however, possibilities explode. remember, after choose second-to-last number, not before. @ point forced choose number allow win.

note 90 not valid choice win with, though might exist. thus, maximum can 89. real range of n is:

[88:73] < n < [90:85] 

it is, however, possible calculate range of number you're using put opponent in no-win situation. in situation find in, lowest number or highest number might 1 chose, if rhc highest number can pick , rlc lowest number can pick, then

rhc = [9:15] rlc = [1:12] 

with information, can begin constructing relative algorithm starting end of game.

n*p* - rhp - rlp < np < n*p* - rhp, p = iteration , *p* = iteration + 1 rhp = [8+p:15] rlp = [1:13-p] p = -1 winning move p = 0 opponent's helpless move p = 1 set-up move np sum of round. 

thus, solving algorithm set-up move, p=1, get:

n*p* - [9:15] - [1:12] < np < n*p* - [9:15] 100 <= n*p* <= 114 

i'm still working out math this, expect adjustments. if see error, please let me know , i'll adjust appropriately.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -