zoukankan      html  css  js  c++  java
  • LeetCode 488. 祖玛游戏 BFS TLE边缘强过

    地址 https://leetcode-cn.com/problems/zuma-game/submissions/

    回忆一下祖玛游戏。现在桌上有一串球,颜色有红色(R),黄色(Y),蓝色(B),绿色(G),还有白色(W)。
    现在你手里也有几个球。
    
    每一次,你可以从手里的球选一个,然后把这个球插入到一串球中的某个位置上(包括最左端,最右端)。
    接着,如果有出现三个或者三个以上颜色相同的球相连的话,就把它们移除掉。
    重复这一步骤直到桌上所有的球都被移除。
    
    找到插入并可以移除掉桌上所有球所需的最少的球数。如果不能移除桌上所有的球,输出 -1 。
    
    示例:
    输入: "WRRBBW", "RB" 
    输出: -1 
    解释: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW
    (翻译者标注:手上球已经用完,桌上还剩两个球无法消除,返回-1)
    
    输入: "WWRRBBWW", "WRBRW" 
    输出: 2 
    解释: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty
    
    输入:"G", "GGGGG" 
    输出: 2 
    解释: G -> G[G] -> GG[G] -> empty 
    
    输入: "RBYYBBRRB", "YRBGB" 
    输出: 3 
    解释: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty 
    标注:
    
    你可以假设桌上一开始的球中,不会有三个及三个以上颜色相同且连着的球。
    桌上的球不会超过20个,输入的数据中代表这些球的字符串的名字是 "board" 。
    你手中的球不会超过5个,输入的数据中代表这些球的字符串的名字是 "hand"。
    输入的两个字符串均为非空字符串,且只包含字符 'R','Y','B','G','W'

    算法1
    由于是求最小步数,使用了BFS宽度搜索。
    好在数据不大 在TLE的边缘AC

     1 class Solution {
     2 public:
     3 
     4     struct ELE {
     5     string board;
     6     string hand;
     7     int step;
     8 };
     9 
    10 class CCC {
    11 public:
    12     bool operator() (const ELE &a, const ELE &b) const {
    13         if (a.board < b.board) return true;
    14         else if (a.board == b.board) {
    15             if (a.hand < b.hand) return true;
    16             else if (a.hand == b.hand) {
    17                 if (a.step < b.step) return true;
    18             }
    19         }
    20 
    21         return false;
    22     }
    23 };
    24 
    25 
    26 set<struct ELE, CCC> record;
    27 
    28 
    29 string RemoveBall( string insBoard, int& idx)
    30 {
    31     int count = 0; string ret;
    32     int l = idx; int r = idx;
    33     while (l >= 0 && insBoard[l] == insBoard[idx]) l--;
    34     while (r < insBoard.size()  && insBoard[r] == insBoard[idx]) r++;
    35 
    36     if (r - l >= 4) {
    37         ret = insBoard.substr(0, l+1) + insBoard.substr(r);
    38         idx = l;
    39     }
    40     else {
    41         ret = insBoard;
    42     }
    43 
    44     return ret;
    45 }
    46 
    47 
    48 int findMinStep(string board, string hand) {
    49 
    50     queue<struct ELE> q;
    51 
    52     q.push({ board,hand,0 });
    53     record.insert({ board,hand,0 });
    54 
    55     while (q.size()) {
    56         struct ELE a = q.front(); q.pop();
    57         int step = a.step;
    58 
    59         if (a.board.empty()) return a.step;
    60         else if (a.hand.empty()) continue;
    61 
    62         for (int i = 0; i < a.hand.size(); i++) {
    63             string curh = a.hand.substr(0, i) + a.hand.substr(i + 1);
    64             char insertChar = a.hand[i];
    65 
    66             for (int j = 0; j < a.board.size(); j++) {
    67                 string insBoard = a.board.substr(0, j) + insertChar + a.board.substr(j);
    68 
    69                 int t = j; string out = insBoard; string input ;
    70                 while (out.size() != input.size()) {
    71                     input = out;
    72                     out = "";
    73                     out = RemoveBall(input, t);
    74                 }
    75                 insBoard = out;
    76 
    77                 if (record.count({ insBoard ,curh }) == 0) {
    78                     record.insert({ insBoard, curh }); 
    79                     q.push({ insBoard, curh,a.step+1 });
    80                 }
    81             }
    82         }
    83     }
    84 
    85 
    86 
    87     return -1;
    88 }
    89 
    90 };
    91 
    92 作者:itdef
    93 链接:https://www.acwing.com/solution/content/18684/
    94 来源:AcWing
    95 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    1

    同上 BFS

     1 class Solution {
     2 public:
     3 
     4 
     5     map<pair<string,string>,int> record;
     6 
     7 
     8 string RemoveBall( string insBoard, int& idx)
     9 {
    10     int count = 0; string ret;
    11     int l = idx; int r = idx;
    12     while (l >= 0 && insBoard[l] == insBoard[idx]) l--;
    13     while (r < insBoard.size()  && insBoard[r] == insBoard[idx]) r++;
    14 
    15     if (r - l >= 4) {
    16         ret = insBoard.substr(0, l+1) + insBoard.substr(r);
    17         idx = l;
    18     }
    19     else {
    20         ret = insBoard;
    21     }
    22 
    23 
    24     return ret;
    25 }
    26 
    27 
    28 int findMinStep(string board, string hand) {
    29     queue<pair<string, string>> q;
    30     q.push({ board,hand });
    31     record[{ board, hand }] = 0;
    32 
    33     while (q.size()) {
    34         pair<string,string> curr = q.front();
    35         int step = record[curr];
    36         q.pop();
    37 
    38         if (curr.first.empty()) return  record[curr];
    39         else if (curr.second.empty()) continue;
    40 
    41         for (int i = 0; i < curr.second.size(); i++) {
    42             string currHand = curr.second.substr(0, i) + curr.second.substr(i + 1);
    43             char insertChar = curr.second[i];
    44 
    45             for (int j = 0; j < curr.first.size(); j++) {
    46                 string insBoard = curr.first.substr(0,j)+ insertChar+ curr.first.substr(j);
    47 
    48 
    49                 int t = j; string out = insBoard; string input ;
    50                 while (out.size() != input.size()) {
    51                     input = out;
    52                     out = "";
    53                     out = RemoveBall(input, t);
    54                 }
    55                 insBoard = out;
    56 
    57 
    58                 if (record.count({ insBoard ,currHand }) == 0) {
    59                     record[{ insBoard, currHand }] = step + 1;
    60                     q.push({ insBoard, currHand });
    61                 }
    62             }
    63         }
    64     }
    65 
    66     return -1;
    67 }
    68 
    69 };
    70 
    71 作者:itdef
    72 链接:https://www.acwing.com/solution/content/18684/
    73 来源:AcWing
    74 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    2
    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    玩不转云计算的架构
    从《从架构的角度看,如何写好代码?》中来看如何编写单元测试代码
    换种形式工作
    程序员下一门要学的编程语言Swift
    从钉钉微应用定制化导航栏看如何实现Hybrid App开发框架
    纯灌水Linus主义
    kFreeBSD有活过来的迹象?UbuntuBSD
    架构的重要性
    MacOS下如何进行Git的冲突(Conflict)处理
    [转]以Facebook为案例剖析科技公司应有的工具文化
  • 原文地址:https://www.cnblogs.com/itdef/p/13523084.html
Copyright © 2011-2022 走看看