[Leetcode] 488. Zuma Game 解题报告

本文详细介绍了Leetcode上的Zuma Game问题,解释了如何通过排序和深度优先搜索(DFS)结合回溯算法来解决这个问题。在解决过程中,先对手中球进行排序,然后对每个球尝试插入到所有可能的位置,如果形成3个及以上相同颜色的球,就进行消除。最终目标是找到使所有桌面上的球都被消除的最小插入次数。如果无法消除所有球,则返回-1。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

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'.

思路

这道题目的难度标记为了Hard,我开始以为有高级的算法,可是后来发现还是需要用DFS+BackTracking来进行暴力搜索。。。

我们首先对hand进行排序,这样如果手里有连续两个球,再碰到board中有一个同颜色的球就可以消掉了。然后暴力法就来了:对于手里的每个球,尝试着把它放在每个位置,然后shrink(如果可以的话)。当然之后还需要再进行DFS搜索,完了还需要回溯,以便于找到最小的移动次数。

代码

class Solution {
public:
    int findMinStep(string board, string hand) {
        sort(hand.begin(), hand.end()); 
        int res = helper(board, hand); 
        return res > hand.size() ? -1 : res;
    }
private:
    int helper(string b, string h) {
        if (b.empty()) {
            return 0;
        }
        if (h.empty()) {
            return MAX_STEP;
        }
        int res = MAX_STEP;
        for (int i = 0; i < h.size(); ++i) {
            int j = 0, n = b.size();
            while (j < n) {
                int k = b.find(h[i], j);
                if (k == string::npos) {            // do not need to shrink
                    break;
                }
                if (k < n-1 && b[k] == b[k+1]) {    // 2 consecutive balls with same color on board
                    // shrink the string until no 3 or more consecutive balls in same color
                    string nextb = shrink(b.substr(0, k) + b.substr(k+2));
                    if (nextb.empty()) {
                        return 1;                   // this is the best result for current board, no need to continue
                    }
                    string nexth = h;
                    nexth.erase(i, 1);              // remove the used ball from hand
                    res = min(res, helper(nextb, nexth) + 1);
                    k++;
                }
                else if (i > 0 && h[i] == h[i-1]) { // 2 balls with same color in hand
                    // shrink the string until no 3 or more consecutive balls in same color
                    string nextb = shrink(b.substr(0, k) + b.substr(k+1)); 
                    if (nextb.empty()) {
                        return 2;  // this is the best result for current board, no need to continue
                    }
                    string nexth = h;
                    nexth.erase(i, 1);              // remove the used balls from hand
                    nexth.erase(i-1, 1);
                    res = min(res, helper(nextb, nexth) + 2);
                }
                j = k + 1;
            }
        }
        return res;
    }
    string shrink(string s) {
        while(s.size() > 0) {
            int start = 0;
            bool done = true;
            for (int i = 0; i <= s.size(); ++i) {
                if (i == s.size() || s[i] != s[start]) {
                    if (i - start >= 3) {
                        s = s.substr(0, start) + s.substr(i);
                        done = false;
                        break;
                    }
                    start = i;
                }
            }
            if (done) {
                break;
            }
        }
        return s;
    }
    const int MAX_STEP = 6; 
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值