Zuma Game

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'.
解析:

递归遍历每种情况,找到最小满足条件的解,给的一些条件提示可以使用暴力的解法。

代码:

class Solution {
public:
    int findMinStep(string board, string hand) {
        
        unordered_mapm;
        for (int i=0; im)
    {
        int ans=INT_MAX;
        
        board=CancelCon(board);
        if (board.empty())
        return 0;
        
        int j=0;
        for (int i=0; i<=board.size(); i++)
        {
            if (i=3)
            {
                return CancelCon(board.substr(0,j)+board.substr(i));
            }
            else
            {
                j=i;
            }
        }
        return board;
    }
    
};



你可能感兴趣的:(leetcode,ACM)