[LeetCode]Zuma Game

题目描述:

LeetCode 488. Zuma Game

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.

Examples:

Input: "WRRBBW", "RB" Output: -1 Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW Input: "WWRRBBWW", "WRBRW" Output: 2 Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty Input:"G", "GGGGG" Output: 2 Explanation: G -> G[G] -> GG[G] -> empty Input: "RBYYBBRRB", "YRBGB" Output: 3 Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty

Note:

  1. You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color.
  2. The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input.
  3. The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input.
  4. Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.

题目大意:

考虑祖玛游戏。桌子上有一排颜色可能为R(红), Y(黄), B(蓝), G(绿), W(白)的小球。你的手中也有若干个球。

每一次,从手中挑选1个球插入桌子上的小球(包括最左边和最右边)。如果出现连续3个或3个以上同色小球,就将它们移除。重复此过程直到不能移除更多的小球。

计算最少需要插入多少个球才可以移除桌子上的所有小球。如果做不到则返回-1。

注意:

  1. 你可以假设初始桌子上不包含3个或者3个以上连续相同颜色的小球。
  2. 桌子上小球的数目不会超过20,输入字符串记为board。
  3. 手中的小球数目不会超过5,输入字符串记为hand。
  4. 输入字符串非空,并且只包含'R','Y','B','G','W'。

解题思路:

深度优先搜索 + 记忆化

每一次尝试在board中插入若干个小球,达到消去小球的目的。

插入小球的原则如下:

1. 每一次插入小球(1个或者2个相同颜色球)至少引发一次“消除动作”,否则就不插入

2. 对于board中出现的连续相同颜色,只在最右侧进行插入

Python代码:

class Solution(object):
    def __init__(self):
        self.cache = dict()

    def findMinStep(self, board, hand):
        """
        :type board: str
        :type hand: str
        :rtype: int
        """
        if not board: return 0
        if not hand: return -1
        if board in self.cache: return self.cache[board]
        cnt = collections.Counter(hand)
        size = len(board)
        ans = -1
        num = 0
        for x in range(1, len(board) + 1):
            if x < len(board) and board[x] == board[x - 1]:
                num += 1
            else:
                if cnt[board[x - 1]] + num > 1:
                    step = max(0, 2 - num)
                    cnt[board[x - 1]] -= step
                    nhand = ''.join(k * v for k, v in cnt.iteritems())
                    nboard = self.simplify(board[:x - num -1] + board[x:])
                    nans = self.findMinStep(nboard, nhand)
                    cnt[board[x - 1]] += step
                    if nans != -1 and (ans == -1 or nans + step < ans):
                        ans = nans + step
                num = 0
        self.cache[board] = ans
        return ans

    def simplify(self, board):
        ptn = r'R{3,}|Y{3,}|B{3,}|G{3,}|W{3,}'
        while re.search(ptn, board):
            board = re.sub(ptn, '', board)
        return board

 

本文链接:http://bookshadow.com/weblog/2017/01/15/leetcode-zuma-game/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。

如果您喜欢这篇博文,欢迎您捐赠书影博客: ,查看支付宝二维码

Pingbacks已关闭。

暂无评论

张贴您的评论