[Leetcode] 488. Zuma Game 解题报告

来源:互联网 发布:js防水涂料做法 编辑:程序博客网 时间:2024/06/10 10:42

题目

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: -1Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WWInput: "WWRRBBWW", "WRBRW"Output: 2Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> emptyInput:"G", "GGGGG"Output: 2Explanation: G -> G[G] -> GG[G] -> empty Input: "RBYYBBRRB", "YRBGB"Output: 3Explanation: 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; };

原创粉丝点击