超超超超级详细的多边形游戏问题分析(动态规划)
多边形游戏
问题简介
首先呢,介绍一下多边形游戏是个什么东东
多边形游戏是一个单人玩的游戏,开始时有一个由 n 个顶点构成的多边形。每个顶点被赋予一个整数值,每条边被赋予一个运算符“+”或“*”。所有边依次用整数从 1 到 n 编号。
游戏步骤:
将一条边删除
选择一条边 E 及由 E 连接的 2 个顶点 V1 和 V2
用一个新的顶点取代边 E 以及由 E 连接着的 2 个顶点 V1 和 V2。将由顶点 V1 和 V2 的整数值通过边 E 上的运算得到的结果赋予新顶点
重复以上步骤,直到所有边都被删除,游戏结束。游戏的得分就是所剩顶点上的整数值
我们的问题是: 根据给定的多边形,计算最高分和最底分
问题分析
最优子结构性质
这里呢,它是满足最优子结构性质的,我们不做过多的解释,直接看它的求解方法
递归求解
首先,我们在 p(i, j)在 op[i+s]处断开, 最大值记为 maxf(i,j,s),最小值记为 minf(i,j,s)
因为只有加法和乘法,加法比较简单。对于乘法,可能存在负数,所以我们要对所有可能的结果做讨论,为了计算方便,我们可以定义如下表示 a=m[i,i+s,0] b=m[i,i+s,1]c=m[i+s,j-s,0] d=m[i+s,j-s,1]
当 op[i+s]=‘+’m[i,j,0]=a+c m[i,j,1]=b+d
当 op[i+s]=‘*’m[i,j,0]=min{ac,ad,bc,bd} m[i,j,1]=max{ac,ad,bc,bd}
因此可有如下公式
至于 s 的断开位置,可以取 1 到 j - 1, 如下
接下来,就是超超超超级详细的解题步骤
超详细解题步骤
这里,我们举了这样一个栗子
详细步骤如下
m(1,1,1) = 9m(1,1,0) = 9m(2,1,1) = -4m(2,1,0) = -4m(3,1,1) = 9m(3,1,0) = 9m(4,1,1) = 10m(4,1,0) = 10m(5,1,1) = -10m(5,1,0) = -10
m(1,2) = m(1,1)op(2)m(2,1)op(2) = "+"m(1,2,1) = m(1,1,1) + m(2,1,1) = 5m(1,2,0) = m(1,1,0) + m(2,1,0) = 5
m(2,2) = m(2,1)op(3)m(3,1)op(3) = "+"m(2,2,1) = m(2,1,1) + m(3,1,1) = 5m(2,2,0) = m(2,1,0) + m(3,1,0) = 5
m(3,2) = m(3,1)op(4)m(4,1)op(4) = "+"m(3,2,1) = m(3,1,1) + m(4,1,1) = 19m(3,2,0) = m(3,1,0) + m(4,1,0) = 19
m(4,2) = m(4,1)op(5)m(5,1)op(5) = "*"m(4,2,1) = max{m(4,1,1)*m(5,1,1), m(4,1,1)*m(5,1,0), m(4,1,0)*m(5,1,1), m(4,1,0)*m(5,1,0)} = -100m(4,2,0) = min{m(4,1,1)*m(5,1,1), m(4,1,1)*m(5,1,0), m(4,1,0)*m(5,1,1), m(4,1,0)*m(5,1,0)} = -100
m(5,2) = m(5,1)op(1)m(1,1)m(5,2,1) = m(5,1,1) + m(1,1,1) = -1m(5,2,0) = m(5,1,0) + m(1,1,0) = -1
op(2) = "+"op(3) = "+"m(1,3,1) = max{m(1,1,1)+m{2,2,1}, m(1,2,1)+m(3,1,1)} = 14 (m(1,1,1)+m(2,2,1))m(1,3,0) = min{m(1,1,0)+m{2,2,0}, m(1,2,0)+m(3,1,0)} = 14 (m(1,1,0)+m(2,2,0))
op(3) = "+"op(4) = "+"m(2,3,1) = max{m(2,1,1)+m(3,2,1), m(2,2,1)+m(4,1,1)} = 15 (m(2,1,1)+m(3,2,1))m(2,3,0) = min{m(2,1,0)+m(3,2,0), m(2,2,0)+m(4,1,0)} = 15 (m(2,1,0)+m(3,2,0))
op(4) = "+"op(5) = "*"m(3,3,1) = max{m(3,1,1)+m(4,2,1), max{m(3,2,1)*m(5,1,1), m(3,2,1)*m(5,1,0), m(3,2,0)*m(5,1,1), m(3,2,0)*m(5,1,0)}} = -91 (m(3,1,1)+m(4,2,1))m(3,3,0) = min{m(3,1,0)+m(4,2,0), min{m(3,2,1)*m(5,1,1), m(3,2,1)*m(5,1,0), m(3,2,0)*m(5,1,1), m(3,2,0)*m(5,1,0)}} = -190 (m(3,2,1)*m(5,1,1))
op(5) = "*"op(1) = "+"m(4,3,1) = max{max{m(4,1,1)*m(5,2,1), m(4,1,1)*m(5,2,0), m(4,1,0)*m(5,2,1), m(4,1,0)*m(5,2,0)}, m(4,2,1)+m(1,1,1)} = -10 (m(4,1,1)*m(5,2,1))m(4,3,0) = min{min{m(4,1,1)*m(5,2,1), m(4,1,1)*m(5,2,0), m(4,1,0)*m(5,2,1), m(4,1,0)*m(5,2,0)}, m(4,2,0)+m(1,1,0)} = -91 (m(4,2,0)+m(1,1,0))
op(1) = "+"op(2) = "+"m(5,3,1) = max{m(5,1,1)+m(1,2,1), m(5,2,1)+m(2,1,1)} = -5 (m(5,1,1)+m(1,2,1))m(5,3,0) = min{m(5,1,0)+m(1,2,0), m(5,2,0)+m(2,1,0)} = -5 (m(5,1,0)+m(1,2,0))
op(2) = "+"op(3) = "+"op(4) = "+"m(1,4,1) = max{m(1,1,1)+m(2,3,1), m(1,2,1)+m(3,2,1), m(1,3,1)+m(4,1,1)} = 24 (m(1,1,1)+m(1,2,1))m(1,4,0) = min{m(1,1,0)+m(2,3,0), m(1,2,0)+m(3,2,0), m(1,3,0)+m(4,1,0)} = 24 (m(1,1,0)+m(2,3,0))
op(3) = "+"op(4) = "+"op(5) = "*"m(2,4,1) = max{m(2,1,1)+m(3,3,1), m(2,2,1)+m(4,2,1), max{m(2,3,1)*m(5,1,1), m(2,3,1)*m(5,1,0), m(2,3,0)*m(5,1,1), m(2,3,0)*m(5,1,0)}} = -95 (m(2,1,1)+m(3,3,1))m(2,4,0) = min{m(2,1,0)+m(3,3,0), m(2,2,0)+m(4,2,0), min{m(2,3,1)*m(5,1,1), m(2,3,1)*m(5,1,0), m(2,3,0)*m(5,1,1), m(2,3,0)*m(5,1,0)}} = -194 (m(2,1,0)+m(3,3,0))
op(4) = "+"op(5) = "*"op(1) = "+"m(3,4,1) = max{m(3,1,1)+m(4,3,1), max{m(3,2,1)*m(5,2,1), m(3,2,1)*m(5,2,0), m(3,2,0)*m(5,2,1), m(3,2,0)*m(5,2,0)}, m(3,3,1)+m(1,1,1)} = -1 (m(3,1,1)+m(4,3,1))m(3,4,0) = min{m(3,1,0)+m(4,3,0), min{m(3,2,1)*m(5,2,1), m(3,2,1)*m(5,2,0), m(3,2,0)*m(5,2,1), m(3,2,0)*m(5,2,0)}, m(3,3,0)+m(1,1,0)} = -181 (m(3,3,0)+m(1,1,0))
op(5) = "*"op(1) = "+"op(2) = "+"m(4,4,1) = max{max{m(4,1,1)*m(5,3,1), m(4,1,1)*m(5,3,0), m(4,1,0)*m(5,3,1), m(4,1,0)*m(5,3,0)}, m(4,2,1)+m(1,2,1), m(4,3,1)+m(2,1,1)} = -14 (m(4,3,1)+m(2,1,1))m(4,4,0) = min{min{m(4,1,1)*m(5,3,1), m(4,1,1)*m(5,3,0), m(4,1,0)*m(5,3,1), m(4,1,0)*m(5,3,0)}, m(4,2,0)+m(1,2,0), m(4,3,0)+m(2,1,0)} = -95 (m(4,2,0)+m(1,2,0))
op(1) = "+"op(2) = "+"op(3) = "+"m(5,4,1) = max{m(5,1,1)+m(1,3,1), m(5,2,1)+m(2,2,1), m(5,3,1)+m(3,1,1)} = 4 (m(5,1,1)+m(1,3,1))m(5,4,0) = min{m(5,1,0)+m(1,3,0), m(5,2,0)+m(2,2,0), m(5,3,0)+m(3,1,0)} = 4 (m(5,1,0)+m(1,3,0))
op(2) = "+"op(3) = "+"op(4) = "+"op(5) = "*"m(1,5,1) = max{m(1,1,1)+m(2,4,1), m(1,2,1)+m(3,3,1), m(1,3,1)+m(4,2,1), max{m(1,4,1)*m(5,1,1), m(1,4,1)*m(5,1,0), m(1,4,0)*m(5,1,1), m(1,4,0)*m(5,1,0)}} = -86 (m(1,1,1)+m(2,4,1))m(1,5,0) = min{m(1,1,0)+m(2,4,0), m(1,2,0)+m(3,3,0), m(1,3,0)+m(4,2,0), min{m(1,4,1)*m(5,1,1), m(1,4,1)*m(5,1,0), m(1,4,0)*m(5,1,1), m(1,4,0)*m(5,1,0)}} = -240 (m(1,4,1)*m(5,1,1))
op(3) = "+"op(4) = "+"op(5) = "*"op(1) = "+"m(2,5,1) = max{m(2,1,1)+m(3,4,1), m(2,2,1)+m(4,3,1), max{m(2,3,1)*m(5,2,1), m(2,3,1)*m(5,2,0), m(2,3,0)*m(5,2,1), m(2,3,0)*m(5,2,0)}, m(2,4,1)+m(1,1,1)} = -5 (m(2,1,1)+m(3,4,1))m(2,5,0) = min{m(2,1,0)+m(3,4,0), m(2,2,0)+m(4,3,0), min{m(2,3,1)*m(5,2,1), m(2,3,1)*m(5,2,0), m(2,3,0)*m(5,2,1), m(2,3,0)*m(5,2,0)}, m(2,4,0)+m(1,1,0)} = -185 (m(2,1,0)+m(3,4,0))
op(4) = "+"op(5) = "*"op(1) = "+"op(2) = "+"m(3,5,1) = max{m(3,1,1)+m(4,4,1), max{m(3,2,1)*m(5,3,1), m(3,2,1)*m(5,3,0), m(3,2,0)*m(5,3,1), m(3,2,0)*m(5,3,0)}, m(3,3,1)+m(1,2,1), m(3,4,1)+m(2,1,1)} = -5 (m(3,1,1)+m(4,4,1))m(3,5,0) = min{m(3,1,0)+m(4,4,0), min{m(3,2,1)*m(5,3,1), m(3,2,1)*m(5,3,0), m(3,2,0)*m(5,3,1), m(3,2,0)*m(5,3,0)}, m(3,3,0)+m(1,2,0), m(3,4,0)+m(2,1,0)} = -185 (m(3,3,0)+m(1,2,0))
op(5) = "*"op(1) = "+"op(2) = "+"op(3) = "+"m(4,5,1) = max{max{m(4,1,1)*m(5,4,1), m(4,1,1)*m(5,4,0), m(4,1,0)*m(5,4,1), m(4,1,0)*m(5,4,0)}, m(4,2,1)+m(1,3,1), m(4,3,1)+m(2,2,1), m(4,4,1)+m(3,1,1)} = 40 (m(4,1,1)*m(5,4,1))m(4,5,0) = min{min{m(4,1,1)*m(5,4,1), m(4,1,1)*m(5,4,0), m(4,1,0)*m(5,4,1), m(4,1,0)*m(5,4,0)}, m(4,2,0)+m(1,3,0), m(4,3,0)+m(2,2,0), m(4,4,0)+m(3,1,0)} = -86 (m(4,2,0)+m(1,3,0))
op(1) = "+"op(2) = "+"op(3) = "+"op(4) = "+"m(5,5,1) = max{m(5,1,1)+m(1,4,1), m(5,2,2)+m(2,3,1), m(5,3,1)+m(3,2,1), m(5,4,1)+m(4,1,1)} = 14 (m(5,1,1)+m(1,4,1))m(5,5,0) = min{m(5,1,0)+m(1,4,0), m(5,2,0)+m(2,3,0), m(5,3,0)+m(3,2,0), m(5,4,0)+m(4,1,0)} = 14 (m(5,1,0)+m(1,4,0))
至此,终于计算完毕,那么:max = max{m(1,5,1), m(2,5,1), m(3,5,1), m(4,5,1), m(5,5,1)} = 40max = min{m(1,5,0), m(2,5,0), m(3,5,0), m(4,5,0), m(5,5,0)} = -240
最后,是 Java 代码的实现过程
Java 代码实现
好了,到此结束。欢迎大家关注我😀
版权声明: 本文为 InfoQ 作者【若尘】的原创文章。
原文链接:【http://xie.infoq.cn/article/c524111ca74b45f7afc4d2527】。文章转载请联系作者。
评论