Description
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand.
Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed.
Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1.
Example

分析
这道题让我重温了小时候经常玩的祖玛游戏,游戏画面是这样子的:

怎么样,是不是让大家充满了回忆~
如果玩过这个游戏,那么对题意理解起来应该非常简单。这道题给我们限定了5种颜色的球,并且提前分配给我们数量不同的各种颜色的球,让我们找出通过手中的球让轨道中的球全部碰撞掉(相同颜色的球相邻不小于3个)的最小球数。
首先,我们看一个combo碰撞的例子(在游戏中,combo次数越多越加分哦)
如果轨道中的球:WBBWW
手中的球:B
那么我将B碰撞掉之后,轨道中的球变成了WWW,可以继续自动碰撞,最终全部消耗。
因此我们需要定义一个模块,这个模块的作用就是每次消耗手里的球都要去看是否可以达到combo的效果,我们将这个模块命名为shrink。

剩下的工作就是主体了:
首先我们要通过字典记录手中每种颜色球对应的个数;然后通过DFS遍历每一个球,当我们手中某种颜色的球可以触发碰撞的时候,就通过shrink模块使其最大化碰撞,并返回每次消耗球的个数。

AC



















