您的位置:首页 > 其它

【LeetCode】488. Zuma Game

2017-03-11 14:52 567 查看

【LeetCode】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.


【输入输出】

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

附加测例:

"RWRR", "WW" -> 2

"RWRW", "RW" -> -1

"RRWWRRW", "W" -> -1

"RWYWRRWRR", "YRY" -> 3

"BWYYBBYRRYWB", "BRBW" -> 4


【解题思路】

深度优先搜索(DFS)
void dfs(stack> pre, string left, map table, int cur)
其中,栈pre存储已压栈的board部分,left为未压栈部分,table为hand信息,cur为当前消耗球数
1. 遍历left,当pre为空或pre.top().first == left[i]时,将pre[i]压栈
2. 当pre非空且pre.top().first != left[i]时,若pre.top().second >= 3时,将pre栈顶元素弹出,递归调用dfs。
3. 当pre非空且pre.top().first != left[i]时,若pre.top().second + table.find(pre.top().first)->second >= 3时,将pre栈顶元素弹出,table.find(pre.top().first)->second -= (3 - pre.top().second), cur += (3 - pre.top().second),递归调用dfs。
4. 当left为空时,若pre.top().second >= 3或者pre.top().second + table.find(pre.top().first)->second >= 3时弹出栈顶元素,以此类推。
5. 若pre为空,则ans = min(ans, cur),否则ans = -1;
6. 返回ans。



【代码】

class Solution {
public:
int ans;
int findMinStep(string board, string hand) {
ans = 10000;
map table;
for (int i = 0; i < hand.length(); i++) {
if (table.find(hand[i]) == table.end()) table.insert(make_pair(hand[i], 1));
else table.find(hand[i])->second++;
}
dfs(stack>(), board, table, 0);
return (ans == 10000) ? -1 : ans;
}
void dfs(stack> pre, string left, map table, int cur) {
for (int i = 0; i < left.length(); i++) {
while (!pre.empty() && pre.top().first != left[i] && pre.top().second >= 3) {
pair top = pre.top();
dfs(stack>(), left.substr(0, i - top.second) + left.substr(i, left.length() - i), table, cur);
pre.pop();
}
if (pre.empty()) pre.push(make_pair(left[i], 1));
else if (pre.top().first == left[i]) pre.top().second++;
else if (pre.top().first != left[i]) {
pair top = pre.top();
if (table.find(top.first) != table.end() && table.find(top.first)->second + top.second >= 3) {
table.find(top.first)->second -= (3 - top.second);
dfs(stack>(), left.substr(0, i - top.second) + left.substr(i, left.length() - i), table, cur + 3 - top.second);
table.find(top.first)->second += (3 - top.second);
}
pre.push(make_pair(left[i], 1));
}
}
while (!pre.empty()) {
pair top = pre.top();
if (top.second >= 3) pre.pop();
else if (table.find(top.first) != table.end() && table.find(top.first)->second + top.second >= 3) {
table.find(top.first)->second -= (3 - top.second);
cur += (3 - top.second);
pre.pop();
}
else break;
}
if (pre.empty()) ans = min(ans, cur);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode