https://leetcode.com/problems/zuma-game/description/
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.
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
// Author: Huahua // Runtime: 3 ms class Solution { public: int findMinStep(string board, string hand) { vector<int> h(128, 0); for (char color : hand) ++h[color]; return dfs(board, h); } private: // Return the min # of balls needed in hand to clear the board. // Returns -1 if not possible. int dfs(const string& board, vector<int>& hand) { if (board.empty()) return 0; int ans = INT_MAX; int i = 0; int j = 0; while (i < board.size()) { while (j < board.size() && board[i] == board[j]) ++j; // board[i] ~ board[j - 1] have the same color const char color = board[i]; // Number of balls needed to clear board[i] ~ board[j - 1] const int b = 3 - (j - i); // Have sufficient balls in hand if (hand[color] >= b) { // Remove board[i] ~ board[j - 1] and update the board string nb = update(board.substr(0, i) + board.substr(j)); // Find the solution on new board with updated hand hand[color] -= b; int r = dfs(nb, hand); if (r >= 0) ans = min(ans, r + b); // Recover the balls in hand hand[color] += b; } i = j; } return ans == INT_MAX ? -1 : ans; } // Update the board by removing all consecutive 3+ balls. // "YWWRRRWWYY" -> "YWWWWYY" -> "YYY" -> "" string update(string board) { int i = 0; while (i < board.size()) { int j = i; while (j < board.size() && board[i] == board[j]) ++j; if (j - i >= 3) { board = board.substr(0, i) + board.substr(j); i = 0; } else { ++i; } } return board; } }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
[…] (Solution) : http://zxi.mytechroad.com/blog/searching/leetcode-488-zuma-game/ 题目列表: * 动态规划 (Dynamic programming) […]