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
复制代码

 

posted on   itdef  阅读(288)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示