【LeetCode 488】 Zuma Game

本文深入探讨了Zuma游戏的算法实现,旨在找到移除所有彩球所需的最少操作数。通过对不同策略的分析,包括剪枝优化和深度优先搜索,文章提供了一种有效的解决方案,并附带详细的代码示例。

题目描述

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;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值