[LeetCode] 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
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'.
这道题说的就是著名的祖玛游戏了,让我想起了以前玩过的泡泡龙,也是一种祖玛游戏,在QQ上也有泡泡龙的游戏,还可以使用各种道具害其他玩家,相当有趣。那么这道题是一种简化版的祖玛游戏,只是一个一维数组,而且通过限定桌面上的球不超过20个,手里的球不超过5个来降低来难度,貌似是在暗示我们可以用暴力搜索法来做。这道题比较使用递归的方法来做,通过遍历所有可能的情况来找出最优解,题目希望我们用最少的球来消掉桌上所有的球,如果不能完全消掉,返回-1。我们使用哈希表来统计手中每种球的个数,然后我们遍历桌上的球,我们找连续相同球的个数,在没有可以消除的情况下,连续的个数只能是1个或2个,然后我们用3减去连续个数,就是我们需要补充的球数以使其可以被消除,那么我们在哈希表表中看我们手中的该类型的球够不够,如果够就表示可以消除,我们在哈希表中减去需要使用掉的球数,然后将消掉的球移除,对新的字符串调用递归,如果可以成功消除,会返回一个结果,该结果加上之前需要的球数用来更新结果res,注意调用完递归要恢复哈希表的状态。还有就是在刚进入递归函数时,我们要检测字符串,去除连续3个相同球的情况,这个去除函数也是个递归函数,写起来很简洁,但是很强大,参见代码如下:
解法一:
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 ? - : res;
}
int helper(string board, unordered_map<char, int>& m) {
board = removeConsecutive(board);
if (board.empty()) return ;
int cnt = INT_MAX, j = ;
for (int i = ; i <= board.size(); ++i) {
if (i < board.size() && board[i] == board[j]) continue;
int need = - (i - j);
if (m[board[j]] >= need) {
m[board[j]] -= need;
int t = helper(board.substr(, 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 = , j = ; i <= board.size(); ++i) {
if (i < board.size() && board[i] == board[j]) continue;
if (i - j >= ) return removeConsecutive(board.substr(, j) + board.substr(i));
else j = i;
}
return board;
}
};
下面这种解法也是递归解法,但是思路和上面略有不同,这里我们不使用哈希表,而是使用一个集合,我们遍历手中的所有小球,如果某个小球已经在集合中存在了,说明我们已经处理过该小球了,直接跳过,否则就将该小球加入集合中。然后我们遍历桌上的小球,寻找和当前手中小球一样的位置,然后将手中小球加入当前位置,调用去除重复3个小球的函数,如果此时字符串为0了,说明当前桌上小球已经完全消掉了,返回1,因为我们此时只使用了一个小球;否则就将手中的当前小球去掉,对新的桌面和剩余手中的小球调用递归,如果得到的结果不是-1,我们用此结果加1来更新结果res,参见代码如下:
解法二:
class Solution {
public:
int findMinStep(string board, string hand) {
int res = INT_MAX;
unordered_set<char> s;
for (int i = ; i < hand.size(); ++i) {
if (s.count(hand[i])) continue;
s.insert(hand[i]);
for (int j = ; j < board.size(); ++j) {
if (board[j] != hand[i]) continue;
string newBoard = board, newHand = hand;
newBoard.insert(j, , hand[i]);
newBoard = removeConsecutive(newBoard);
if (newBoard.size() == ) return ;
newHand.erase(i, );
int cnt = findMinStep(newBoard, newHand);
if (cnt != -) res = min(res, cnt + );
}
}
return res == INT_MAX ? - : res;
}
string removeConsecutive(string board) {
for (int i = , j = ; i <= board.size(); ++i) {
if (i < board.size() && board[i] == board[j]) continue;
if (i - j >= ) return removeConsecutive(board.substr(, j) + board.substr(i));
else j = i;
}
return board;
}
};
类似题目:
参考资料:
https://discuss.leetcode.com/topic/76360/bfs
https://discuss.leetcode.com/topic/75578/simplest-method/2
https://discuss.leetcode.com/topic/79820/short-java-solution-beats-98
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Zuma Game 祖玛游戏的更多相关文章
- 488 Zuma Game 祖玛游戏
回忆一下祖玛游戏.现在桌上有一串球,颜色有红色(R),黄色(Y),蓝色(B),绿色(G),还有白色(W). 现在你手里也有几个球.每一次,你可以从手里的球选一个,然后把这个球插入到一串球中的某个位置上 ...
- [Swift]LeetCode488. 祖玛游戏 | Zuma Game
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), gre ...
- Leetcode 488.祖玛游戏
祖玛游戏 回忆一下祖玛游戏.现在桌上有一串球,颜色有红色(R),黄色(Y),蓝色(B),绿色(G),还有白色(W). 现在你手里也有几个球. 每一次,你可以从手里的球选一个,然后把这个球插入到一串球中 ...
- Java实现 LeetCode 488 祖玛游戏
488. 祖玛游戏 回忆一下祖玛游戏.现在桌上有一串球,颜色有红色,黄色(Y),蓝色(B),绿色(G),还有白色(W). 现在你手里也有几个球. 每一次,你可以从手里的球选一个,然后把这个球插入到一串 ...
- T4310 祖玛游戏
题目描述 祖玛是一款曾经风靡全球的游戏,其玩法是:在一条轨道上初始排列着若干 个彩色珠子,其中任意三个相邻的珠子不会完全同色.此后,你可以发射珠子到 轨道上并加入原有序列中.一旦有三个或更多同色的珠子 ...
- [LeetCode] Elimination Game 淘汰游戏
There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number ...
- [LeetCode] Flip Game 翻转游戏
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [LeetCode] Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- [poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...
- python 函数基础2
一.什么是命名关键字参数? 格式: 在*后面参数都是命名关键字参数. 特点:1.约束函数的调用者必须按照Kye=value的形式传值. 2,.约束函数的调用者必须用我们指定的Key名. def aut ...
- Django—templates系统:模版语言
常用语法 只需要记两种特殊符号: {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 {{ 变量名 }} 变量名由字母数字和下划线组成. 点(.)在模板语言中有特殊的含义, ...
- NVisionXR引擎基本介绍
NVisionXR引擎基本介绍 一. 介绍 1.1 NVisionXR是什么? NVisionXR引擎是全球首款跨平台多兼容的原生AR应用开发引擎,让AR应用开发更高效. 1. ...
- Alpha第十天
Alpha第十天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- 《高级软件测试》11.16.Jira使用说明的撰写和操作视频的录制
今日任务完成情况如下: 小王:完成了测试管理工具jira的使用手册中,基本情况介绍.下载安装部分的撰写工作:小高:参考官方手册,结合自己的实际使用体会,对jira的基本组成及其工作流程进行了介绍:小陈 ...
- 美团点餐—listview内部按钮点击事件
PS:长时间不写博客了,今天来写一下美团的这个点餐界面,今天先写一个加号减号的接口调用,下一篇是整体,有点菜,评价,商家,还有左边的listview和右边的展示项.进入这篇正题,像listview,G ...
- [UWP] Custom Capability的使用
Custom Capability 是uwp开发中普通开发者较为不常用的内容,但是在一些OEM和驱动厂商,使用频率比较高 Custom Capability 有两种用户: 1.普通应用程序开发者: 2 ...
- ELK学习总结(4-2)关于导入数据
用REST API的_bulk来批量插入,可以达到5到10w条每秒 把数据写进json文件,然后再通过批处理,执行文件插入数据: 1.先定义一定格式的json文件,文件不能过大,过大会报错 2.后用c ...
- python入门(14)定义函数和接收返回值
定义函数: 定义一个求绝对值的my_abs函数为例: def my_abs(x): if x >= 0: return x else: return -x 如果没有return语句,函数执行完毕 ...