【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 488. Zuma Game 祖玛游戏 + 递归做法
- 488. Zuma Game(leetcode DFS hard)
- [Leetcode] 488. Zuma Game 解题报告
- LeetCode "488. Zuma Game" !
- [leetcode]488. Zuma Game
- leetcode 488. Zuma Game(祖玛游戏)
- [Leetcode] Nim Game 尼姆游戏
- Leetcode_jump-game (updated c++ and python version)
- leetcode:Jump Game II
- leetcode45 Jump Game II
- leetcode笔记:Nim Game
- LeetCode174. Dungeon Game
- Leetcode 292[easy]---Nim Game
- [Leetcode] 293. Flip Game 解题报告
- leetcode Jump Game II
- leetcode:Jump Game II 【Java】
- LeetCode 293. Flip Game(反转游戏)
- 【Leetcode】Jump Game II (DP)
- LeetCode 292. Nim Game
- [LeetCode] Zuma Game