Dec 30, 2019 488. Zuma Game (Leetcode Hard) Leetcode Live with Larry. Unsubscribe from Leetcode Live with Larry? Cancel Unsubscribe. Subscribe Subscribed Unsubscribe 489. Jan 14, 2017 quize 1: Coin Game LeetCode 486. Predict the Winner LeetCode 310. Minimum Height Trees LeetCode 307. Range Sum Query - Mutable 306. Additive Number 301. Remove Invalid Parentheses 287. Find the Duplicate Number 290. Word Pattern 486. Max Consecutive Ones II 485. Max Consecutive Ones 488. Zuma Game 260. Single Number III 240.
Problem:
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.
Zuma Deluxe
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:
- 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 20, 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’.
Idea: Search
Solution1: C++ / Search
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 | // Runtime: 3 ms public: vector<int>h(128,0); returndfs(board,h); private: // Return the min # of balls needed in hand to clear the board. intdfs(conststring&board,vector<int>&hand){ inti=0; while(i<board.size()){ // board[i] ~ board[j - 1] have the same color // Number of balls needed to clear board[i] ~ board[j - 1] // Have sufficient balls in hand // Remove board[i] ~ board[j - 1] and update the board stringnb=update(board.substr(0,i)+board.substr(j)); // Find the solution on new board with updated hand intr=dfs(nb,hand); // Recover the balls in hand } } } // Update the board by removing all consecutive 3+ balls. stringupdate(stringboard){ while(i<board.size()){ while(j<board.size()&&board[i]board[j])++j; board=board.substr(0,i)+board.substr(j); }else{ } returnboard; }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
您可以通过在亚马逊上购物(任意商品)来支持我们
488. Zuma Game Download
huahualeetcode
Zuma Game Free Download
Zuma Gold Strike is a new game on this website. Have you ever played Zuma before? I think you know the rules of game. It’s simple. You should destroy all enemies and then the next level will be available. Shoot in balls, as fast as you can. Because you have a short time and need become winner. Are you ready? Explore magical worlds and get bonuses. The more the merrier. If you like adventures, then you will like this game. There are many levels in there and each level has a nice story. Gamers find Zuma Gold Strike interesting, and they are playing Zuma now. It helps you relax and get energy and it helps you protect against depression. Flash games are useful for health. I recommend to play it every day. Good luck.
See also: zuma deluxe free online