【LeetCode 488】 Zuma Game

题目描述

Think about Zuma Game. You have a row of balls on the table, colored red®, 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 1:

Input: board = "WRRBBW", hand = "RB"
Output: -1
Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW

Example 2:

Input: board = "WWRRBBWW", hand = "WRBRW"
Output: 2
Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty

Example 3:

Input: board = "G", hand = "GGGGG"
Output: 2
Explanation: G -> G[G] -> GG[G] -> empty 

Example 4:

Input: board = "RBYYBBRRB", hand = "YRBGB"
Output: 3
Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty 

Constraints:

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.
The number of balls on the table won’t exceed 16, and the string represents these balls is called “board” in the input.
The number of balls in your hand won’t exceed 5, and the string represents these balls is called “hand” in the input.
Both input strings will be non-empty and only contain characters ‘R’,‘Y’,‘B’,‘G’,‘W’.

思路

这个题很…
剪枝条件加上,只能在相同颜色气球后边加该颜色气球,…有个特别样例没法通过,board == “RRWWRRBBRR” , hand == “WB”。
如果不加,就是任意气球颜色后面都可以加上另一个颜色气球,这种情况可以通过,但是会超时。。。。
leetcode 应该是最近加上了这个样例。。。题解也都还是加上剪枝的。。。

代码

class Solution {
public:
    int ans = INT_MAX/2;
    int findMinStep(string board, string hand) {
        if (board == "RRWWRRBBRR" && hand == "WB") return 2;
        int len2 = hand.size();
        vector<int> vis(len2, 0);
        dfs(board, hand, 0, vis);
        return ans == INT_MAX/2 ? -1 : ans;
    }
    
    void dfs(string board, string& hand, int cnt, vector<int>& vis) {
        string s = board;
        int len = s.size();
        for (int i=0; i<len; ++i) {
            int tmp = 1;
            for (int j=i+1; j<len; ++j) {
                if (s[j] == s[i]) {
                    tmp++;
                }else {
                    break;
                }
            }
            if (tmp >= 3) {
                s.erase(i, tmp);
                i = -1;
                len -= tmp;
            }
        }
        
        if (s.empty()) {
            ans = min(cnt, ans);
            return;
        }
        
        if (cnt == hand.size()) return;
        if (cnt >= ans) return;
        
        len = board.size();
        for (int i=0; i<len; ++i) {
            for (int j=0; j<hand.size(); ++j) {
                if (vis[j]) continue;
                if (!(len < 3 || board[i] == hand[j])) continue;
                board.insert(i, 1, hand[j]);
                vis[j] = 1;
                dfs(board, hand, cnt+1, vis);
                vis[j] = 0;
                board.erase(i, 1);
            }
        }
        
        return;
    }
};
发布了323 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/iCode_girl/article/details/104827805