这可以通过动态编程来完成,我不确定是否有更好的方法,因为游戏是独立的。
有一个包含胜利、失败、平局和比赛的 4 维数组。您可以将赢/输/平局限制为您想要的数字(让这些为 W、L、T、W+L+T=G),时间复杂度将为 O(W*L*T*G),这是有界的由 O(G⁴)。
该算法基本上是:
A[][][][] = new double[G+1][W][T][L]
// A[g][w][t][l] is the probability of have w wins, t ties, l losses
// after g games. This can be computed from A[g-1].
// Let P[g][o] be the probability of outcome o for game g
//everything else is initially 0.
A[0][0][0][0] = 1
for g=1..G
for w=0..W
for t=0..T
for l=0..L
A[g][w][t][l] = A[g-1][w-1][t][l]*P[g][win] // assume out of bounds
+A[g-1][w][t-1][l]*P[g][tie] // reference returns 0
+A[g-1][w][t][l-1]*P[g][lose]
return A[G][W][T][L]
编辑)
我们可以在 O(W*L*T*G/max(W,L,T)) 中做到这一点,即 O(G³)。请注意,如果我们在 G 场比赛之后有 W 胜和 T 平局,那么我们必须有 L 场失利。
// we should pick the conditions we loop to be the smallest two.
// here we just use wins and ties.
A[][][][] = new double[G+1][W][T]
A[0][0][0] = 1
for g=1..G
for w=0..W
for t=0..T
A[g][w][t] = A[g-1][w-1][t]*P[g][win] // assume out of bounds
+A[g-1][w][t-1]*P[g][tie] // reference returns 0
+A[g-1][w][t]*P[g][lose]
return A[G][W][T]
通过分别计算 x 次获胜/平局/损失的概率 (O(G)),然后智能地添加/减去它们,也许可以显着更快地做到这一点,但我还没有找到一种方法来做到这一点。