# 488. 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.

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.
• `1 <= board.length <= 16`
• `1 <= hand.length <= 5`
• 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) {
int res = INT_MAX;
unordered_map<char, int> m;
for (char c : hand) ++m[c];
res = helper(board, m);
return res == INT_MAX ? -1 : res;
}
int helper(string board, unordered_map<char, int>& m) {
board = removeConsecutive(board);
if (board.empty()) return 0;
int cnt = INT_MAX, j = 0;
for (int i = 0; i <= board.size(); ++i) {
if (i < board.size() && board[i] == board[j]) continue;
int need = 3 - (i - j);
if (m[board[j]] >= need) {
m[board[j]] -= need;
int t = helper(board.substr(0, j) + board.substr(i), m);
if (t != INT_MAX) cnt = min(cnt, t + need);
m[board[j]] += need;
}
j = i;
}
return cnt;
}
string removeConsecutive(string board) {
for (int i = 0, j = 0; i <= board.size(); ++i) {
if (i < board.size() && board[i] == board[j]) continue;
if (i - j >= 3) return removeConsecutive(board.substr(0, j) + board.substr(i));
else j = i;
}
return board;
}
};
``````

``````class Solution {
public:
int findMinStep(string board, string hand) {
int res = INT_MAX;
unordered_set<char> s;
for (int i = 0; i < hand.size(); ++i) {
if (s.count(hand[i])) continue;
s.insert(hand[i]);
for (int j = 0; j < board.size(); ++j) {
if (board[j] != hand[i]) continue;
string newBoard = board, newHand = hand;
newBoard.insert(j, 1, hand[i]);
newBoard = removeConsecutive(newBoard);
if (newBoard.size() == 0) return 1;
newHand.erase(i, 1);
int cnt = findMinStep(newBoard, newHand);
if (cnt != -1) res = min(res, cnt + 1);
}
}
return res == INT_MAX ? -1 : res;
}
string removeConsecutive(string board) {
for (int i = 0, j = 0; i <= board.size(); ++i) {
if (i < board.size() && board[i] == board[j]) continue;
if (i - j >= 3) return removeConsecutive(board.substr(0, j) + board.substr(i));
else j = i;
}
return board;
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/488

Burst Balloons

Strange Printer

https://leetcode.com/problems/zuma-game/

https://leetcode.com/problems/zuma-game/discuss/97031/bfs

https://leetcode.com/problems/zuma-game/discuss/97039/simplest-method

https://leetcode.com/problems/zuma-game/discuss/97010/short-java-solution-beats-98

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation