题目链接:https://leetcode.com/problems/zuma-game/description/

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 -> WWInput: "WWRRBBWW", "WRBRW"
Output: 2
Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> emptyInput:"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:

  1. 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.
  2. The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input.
  3. The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input.
  4. Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.

方法一:(dfs)

class Solution {
public:int findMinStep(string board, string hand) {list<Node> board_list;vector<int> hand_count(CHAR_MAX, 0);int remain_in_hand = 0;for (int i = 0; i < board.length(); ++i) {board_list.push_back(Node(board[i], 1));if (i != board.length() - 1 && board[i] == board[i + 1]) {board_list.back().count++;i++;}}for (char ch : hand) {hand_count[ch]++;remain_in_hand ++;}int ret = DFS(board_list, hand_count, remain_in_hand);return ret == kMaxNeed ? -1 : ret;}
private:// the kMaxNeed need to  be greater than 5; const int kMaxNeed=6;struct Node{char ch;int count;Node(char c,int i):ch(c),count(i){}};void CleanBoard(list<Node> &board){auto it = board.begin();while (it != board.end()) {if (it->count >= 3) {it = board.erase(it);// Ensure iterator != end(), before you use the it// Ensure iterator != begin(), before invoking prev(it)if (it != board.begin() &&it != board.end() &&it->ch == prev(it)->ch){it->count += prev(it)->count;board.erase(prev(it));}} else {it++;}}}int DFS(list<Node> board, vector<int> &hand, int remain_in_hand) {CleanBoard(board);if (board.size() == 0)  return 0;//cannot remove all the ballsif (remain_in_hand <= 0)  return kMaxNeed;int min_need = kMaxNeed;for (auto it = board.begin(); it != board.end(); ++it) {int need = 3 - it->count;if (hand[it->ch] >= need) {hand[it->ch] -= need;it->count += need;list<Node> next_board = board;int ret = DFS(next_board, hand, remain_in_hand - need);min_need = min(min_need, ret + need);it->count -= need;hand[it->ch] += need;}}return min_need;}
};

[leetcode]488. Zuma Game相关推荐

  1. LeetCode 488 Zuma Game 解题报告

    原文链接: http://hankerzheng.com/blog/Leetcode-Zuma-Game- Problem Description LeetCode 488 Zuma Game Thi ...

  2. Java实现 LeetCode 488 祖玛游戏

    488. 祖玛游戏 回忆一下祖玛游戏.现在桌上有一串球,颜色有红色®,黄色(Y),蓝色(B),绿色(G),还有白色(W). 现在你手里也有几个球. 每一次,你可以从手里的球选一个,然后把这个球插入到一 ...

  3. 488 Zuma Game 祖玛游戏

    回忆一下祖玛游戏.现在桌上有一串球,颜色有红色(R),黄色(Y),蓝色(B),绿色(G),还有白色(W). 现在你手里也有几个球. 每一次,你可以从手里的球选一个,然后把这个球插入到一串球中的某个位置 ...

  4. Leetcode 488.祖玛游戏

    祖玛游戏 回忆一下祖玛游戏.现在桌上有一串球,颜色有红色(R),黄色(Y),蓝色(B),绿色(G),还有白色(W). 现在你手里也有几个球. 每一次,你可以从手里的球选一个,然后把这个球插入到一串球中 ...

  5. LeetCode 488. 祖玛游戏

    题目描述 回忆一下祖玛游戏.现在桌上有一串球,颜色有红色(R),黄色(Y),蓝色(B),绿色(G),还有白色(W). 现在你手里也有几个球. 每一次,你可以从手里的球选一个,然后把这个球插入到一串球中 ...

  6. taoqick 搜索自己CSDN博客

    L1 L2正则化和优化器的weight_decay参数 kaiming初始化的推导 Pytorch动态计算图 Pytorch自动微分机制 PyTorch中在反向传播前为什么要手动将梯度清零? 通俗讲解 ...

  7. LeetCode github集合,附CMU大神整理笔记

    Github LeetCode集合 本人所有做过的题目都写在一个java项目中,同步到github中了,算是见证自己的进步.github目前同步的题目是2020-09-17日之后写的题.之前写过的题会 ...

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

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  9. 算法细节系列(16):深度优先搜索

    算法细节系列(16):深度优先搜索 详细代码可以fork下Github上leetcode项目,不定期更新. 题目均摘自leetcode: 1. 329 Longest Increasing Path ...

最新文章

  1. 华硕路由器安装aria2_网易UU加速器联动华硕,瞄准主机玩家联网刚需
  2. Eclipse中最常用的热键
  3. Java 多态、抽象类 笔记
  4. MVC源码分析 - Action查找和过滤器的执行时机
  5. proxytable代理不生效_民法典房屋买卖合同卖方代签合同生效吗
  6. mybatis中的#{}和${}区别,和使用场景
  7. 学习网站的闪卡暴露了美军核机密
  8. 一天一个小技巧(4)——利用Python和MATLAB进行图片二值化
  9. Linux之chrony时间同步服务、ntp协议
  10. MATLAB中的概率函数
  11. 51单片机LCD12864程序移植到STM32F103C
  12. matlab波导色散,有效折射率法求矩形波导色散曲线(附Matlab程序)
  13. eclipse中文语言包安装
  14. 好看的HTML登录模板
  15. 自学编程的六种方法,你必须知道?
  16. HTML5+CSS3小实例:发光文字悬停特效
  17. OpenGL画蜗型线、心形线、三叶曲线、四叶曲线、螺旋线
  18. 编程式路由导航连续跳转出现NavigationDuplicated报错的问题
  19. python//Jan.17th,2020//类
  20. StarGAN v2 : Diverse Image Synthesis for Multiple Domains 不同图像多领域合成阅读理解

热门文章

  1. 【英语四六级-必背单词】高中英语单词(C - 2)MP3试听与下载
  2. php股票t+0,股票t文档教材+0操作宝典.pdf
  3. OS轮转调度算法RR的C++实现
  4. 数学建模之MATLAB编程
  5. 做伦敦银,这两大要点容易被忽视
  6. Linux 基础之虚拟机创建与系统安装
  7. 相机光学(二十)——三原色与白平衡
  8. ac1900 linksys 恢复_AC1900路由器怎么恢复出厂设置?
  9. 多商户商城系统方案分析
  10. 基于3个操作系统的靶场,从零开始做安全渗透工程师