[Leetcode] 723. Candy Crush 解题报告

基于知识图谱的农作物病虫害问答系统设计与实现 基于规则的问题分类是自问答系统诞生以来就一直存在的方式,主要代表就是众多的专家系统,构建专家系统采用的是规则分类器,优点是能够对特定领域、特征明显的问题进行高效精准问答,但是。,在问答系统中,问题以语音及文本字符等自然语言形式输入,利用问答系统的内部的逻辑处理模块,通过自然语言处理技术获得输入问句的详细语义信息,再经过知识检索、置信分析等步骤,自然语言处理中,用于文本分类任务的机器学习方式有两种,一种用传统的机器学习,如 NBC、SVM 等,另一种采用深度学习,如 CNN 和 RNN 等。 阅读详情

题目:

This question is about implementing a basic elimination algorithm for Candy Crush.

Given a 2D integer array board representing the grid of candy, different positive integers board[i][j] represent different types of candies. A value of board[i][j] = 0 represents that the cell at position (i, j) is empty. The given board represents the state of the game following the player's move. Now, you need to restore the board to a stable state by crushing candies according to the following rules:

  1. If three or more candies of the same type are adjacent vertically or horizontally, "crush" them all at the same time - these positions become empty.
  2. After crushing all candies simultaneously, if an empty space on the board has candies on top of itself, then these candies will drop until they hit a candy or bottom at the same time. (No new candies will drop outside the top boundary.)
  3. After the above steps, there may exist more candies that can be crushed. If so, you need to repeat the above steps.
  4. If there does not exist more candies that can be crushed (ie. the board is stable), then return the current board.

You need to perform the above rules until the board becomes stable, then return the current board.

Example 1:

Input:
board = 
[[110,5,112,113,114],[210,211,5,213,214],[310,311,3,313,314],[410,411,412,5,414],[5,1,512,3,3],[610,4,1,613,614],[710,1,2,713,714],[810,1,2,1,1],[1,1,2,2,2],[4,1,4,4,1014]]
Output:
[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[110,0,0,0,114],[210,0,0,0,214],[310,0,0,113,314],[410,0,0,213,414],[610,211,112,313,614],[710,311,412,613,714],[810,411,512,713,1014]]
Explanation: 

Note:

  1. The length of board will be in the range [3, 50].
  2. The length of board[i] will be in the range [3, 50].
  3. Each board[i][j] will initially start as an integer in the range [1, 2000].

思路:

这道题目本身并不难,算法框架分为两步:1)标记出所有需要被crash掉的元素;2)将这些元素置为0,并且crash。但是我感觉难的地方是如何确定哪些元素需要被crash。例如在题目中给出的例子中,5个“2”需要被crash,但是只有4个“1”需要被crash,而board[8][0]位置上的1不可以被crash(这也是我开始写的程序怎么都跑不对的原因)。为了解决这一问题,我的思路是:如果一个元素可以被crash,则我们首先将其置为负数。这样在判断某个元素是否可以被crash的时候,我们就判断它的纵向或者横向是否存在三个连续的位置,其值不为0,并且绝对值相同。如果可以被crash,则将其置为原数对应的负数。这样在后面crash的时候,我们只需要保留正数,而将所有的非正数都置为0。

代码:

class Solution {
public:
    vector<vector<int>> candyCrush(vector<vector<int>>& board) {
        int row_num = board.size(), col_num = board[0].size();
        while (true) {
            // step 1: mark the elements that will be crashed
            bool changed = false;
            for (int r = 0; r < row_num; ++r) {
                for (int c = 0; c < col_num; ++c) {
                    if (mark(board, r, c)) {
                        changed = true;
                    }
                }
            }
            // step 2: crash elements
            if (changed) {
                for (int c = 0; c < col_num; ++c) {
                    vector<int> positives;
                    for (int r = 0; r < row_num; ++r) {
                        if (board[r][c] > 0) {
                            positives.push_back(board[r][c]);
                        }
                    }
                    int size = positives.size();
                    for (int r = 0; r < row_num - size; ++r) {
                        board[r][c] = 0;
                    }
                    for (int r = row_num - size; r < row_num; ++r) {
                        board[r][c] = positives[r - row_num + size];
                    }
                }
            }
            else {
                break;
            }
        }
        return board;
    }
private:
    bool mark(vector<vector<int>> &board, int row, int col) {
        int row_num = board.size(), col_num = board[0].size();
        if (row < 0 || row >= row_num || col < 0 || col >= col_num || board[row][col] <= 0) {
            return false;
        }
        int num = board[row][col];
        int left = col, right = col, up = row, down = row;
        while (left - 1 >= 0 && abs(board[row][left - 1]) == num) {
            --left;
        }
        while (right + 1 < col_num && abs(board[row][right + 1]) == num) {
            ++right;
        }
        if (right - left + 1 >= 3) {
            board[row][col] = -board[row][col];
            return true;
        }
        while (up - 1 >= 0 && abs(board[up - 1][col]) == num) {
            --up;
        }
        while (down + 1 < row_num && abs(board[down + 1][col]) == num) {
            ++down;
        }
        if (down - up + 1 >= 3) {
            board[row][col] = -board[row][col];
            return true;
        }
        return false;
    }
};

SAP CBC 集成质量系统初始化接入实战指南 摘要 本文探讨了SAP S/4HANA Cloud Public Edition项目中质量系统的初始接入过程。质量系统并非开箱即用,而是需要经过SAP Central Business Configuration(CBC)的Scope and Organizational Structure阶段部署后才能具备完整功能。文章重点分析了几个关键环节:CBC里程碑确认的时机选择、Identity Authentication的初始管理员设置、Business Role Template的创建规范等。实施过程中需特 阅读详情

相关推荐

WangEditor 5.0版本dangerouslyInsertHtml方法的正确使用方式

WangEditor 5.0版本dangerouslyInsertHtml方法的正确使用方式

yang1067155909的专栏 4291

【LeetCode - 723】粉碎糖果

【中等】数组、双指针

学哥斌的博客 817

大规模SoC芯片Hierarchical Flow Calibre LVS教程

大规模SoC芯片Hierarchical Flow Calibre物理验证LVS实现教程

weixin_37584728的博客 1200

[LeetCode] 723. Candy Crush 糖果粉碎

This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D integer arrayboardrepresenting the grid of candy, different positive integersboard[i][j]represent dif...

weixin_30675967的博客 384

UFS 3.1协议实战解析:从MIPI M-PHY到SCSI命令的完整链路拆解

本文深入解析UFS 3.1存储协议,从MIPI M-PHY物理层到SCSI命令层,完整拆解数据链路。通过分析协议栈三层架构(UIC、UTP、UCS),结合Wireshark抓包实战,详解UPIU数据包传输与SCSI命令封装,并探讨GEAR模式对性能与功耗的影响,为嵌入式存储开发与调试提供实用指南。

weixin_29207533的博客 422

3D医学影像分割新突破:手把手教你用MedSAM2快速标注CT/MRI数据(附3D Slicer插件配置)

本文详细介绍了如何利用MedSAM2模型进行3D医学影像分割,包括从环境准备到3D Slicer插件配置的全流程。通过实战案例和技巧分享,帮助医疗从业者快速掌握CT/MRI数据的高效标注方法,提升医学影像分析的准确性和效率。

ujm5678901的博客 300

【Leetcode】723. Candy Crush

每一轮开始先求出所有要被消去的位置,存起来,然后将其消去即可。找位置的时候,只需要判断其上下是否都与其相等,或者其左右是否都与其相等。消去的时候,可以遍历每一列,然后进行整体的消去。的段就会同时都被消掉。每一轮找到所有要被消掉的数之后,将上面的数向下填充,上面空出的位置填。这是糖果消消乐的局面,每一轮要进行如下操作:如果某个位置的左右延伸长度。问经过若干轮直到无法再消去新的块的时候,矩阵。是消去的轮数,取决于具体数据),空间。的段都相等,或者其上下延伸长。的段都相等,那么组成长度为。

数学、算法爱好者的博客 587

粒子群算法实战:用Matlab解决旅行商问题(TSP)的完整代码解析

本文提供了使用粒子群算法解决旅行商问题的完整Matlab实战指南。通过将连续空间的粒子群算法进行离散化改造,采用基于交换序的策略来更新粒子位置与速度,并给出了从问题定义、数据结构设计、核心函数实现到主迭代循环的详细代码解析。文章还探讨了参数调优、性能优化及处理复杂TSP变体的进阶思路,为物流规划、路径优化等领域的工程应用提供了可直接复用的解决方案。

m2n3o4p5的博客 350

LWC 57:723. Candy Crush

LWC 57:723. Candy Crush传送门:723. Candy CrushProblem: This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D integer array board representing the grid of can

Demon-初来驾到 1002

Doris实战——结合Flink构建极速易用的实时数仓

Doris实战——结合Flink构建极速易用的实时数仓

爱吃辣条的博客 8233

但是我如果真的用差速轮去控制转向 为了尽量原地旋转 转向角度精确 我应该怎么去配置左右两个轮子的速度呢

目标实现方式原地转向左右轮速度相等、方向相反精确角度用 IMU 检测 yaw 实时对齐停止判断达到目标 yaw ± 容差时立即停转控制方式状态机(idle → turning → done)推荐容差2°~3° (可根据实际调整)

qqqweiweiqq的博客 313

【LeetCode】723. Candy Crush 解题报告 (C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法暴力日期 题目地址:https://leetcode-cn.com/problems/candy-crush/ 题目描述 This question is about implementing a basic elimination alg...

负雪明烛 1476

中文核心:swin transformer 农作物病虫害识别 完整代码数据

中文核心:swin transformer 农作物病虫害识别

940

LeetCode 723. Candy Crush

原题链接在这里:https://leetcode.com/problems/candy-crush/ 题目: This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D integer arrayboardrepresenting the grid o...

weixin_30650859的博客 229

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

723. Candy Crush

https://leetcode.com/problems/candy-crush/description/ 题目大意:就是消消乐,横竖数字相同且连起来大于3的消掉. 解题思路:暴力法,遍历每个位置,然后检查是否有横竖连续大于3的,有就将该位置记录在to_crash中.然后对 to_crash中的点进行处理,直到to_Crash为空.画图模拟比较好理解. 代码: class Solut...

caisense的专栏 599

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LeetCode 723. 粉碎糖果(模拟)

文章目录1. 题目2. 解题 1. 题目 这个问题是实现一个简单的消除算法。 给定一个二维整数数组 board 代表糖果所在的方格,不同的正整数 board[i][j] 代表不同种类的糖果,如果 board[i][j] = 0 代表 (i, j) 这个位置是空的。 给定的方格是玩家移动后的游戏状态,现在需要你根据以下规则粉碎糖果,使得整个方格处于稳定状态并最终输出。 如果有三个及以上水平或者垂直相连的同种糖果,同一时间将它们粉碎,即将这些位置变成空的。 在同时粉碎掉这些糖果之后,如果有一个空的位置上方还有糖

Michael是个半路程序员 2172

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

leetcode723 - Candy Crush - medium

This question is about implementing a basic elimination algorithm for Candy Crush.Given a 2D integer array board representing the grid of candy, different positive integers board[i][j] represent diffe...

weixin_30784141的博客 178

leetcode:Candy 【Java】

一、问题描述 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have

tterminator的专栏 678

[LeetCode] Candy Crush 糖果消消乐

  This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D integer array board representing the grid of candy, different positive integers board[i][j] represent ...

weixin_34034670的博客 396

【LeetCode】Candy 解题报告

There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least on

跳出温水的青蛙 6087

【LeetCode】877. Stone Game 解题报告(Python & C++)

【LeetCode】877. Stone Game 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/stone-game/descriptio...

负雪明烛 3125

LeetCode Candy

一直线上站了N个孩子,每个孩子都有一个属于自己的数字,现在按照如下规则给孩子分发糖果:每个孩子至少有一个糖果;相邻的孩子中数字比较大的那个拿的糖果也比较多。求最少要发掉多少个糖果。

DRFish 4454

LeetCode:135. Candy - Python

135. 分发糖果 问题描述: 老师想给孩子们分发糖果,有N个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到1个糖果。 相邻的孩子中,评分高的孩子必须获得更多的糖果。 那么这样下来,老师至少需要准备多少颗糖果呢? 示例 1: 输入: [1,0,2] 输出: 5 解释: 你可以分别给这三个孩子分发 2、1、2...

Growth Diary 984

【leetcode】Candy

Problem Code/* #include <stdio.h> #include <stdlib.h> #include <stdbool.h> * 1、找出“两边高,中间低”的那个点,设为1(可能有很多这样的点); * 2、满足“任意一个孩子都至少有一颗糖”,那么就是从1中这些点出发,分别向两边递增; * 3、每两个点之间以较长的序列为基准! */ void print(int n

猫和鱼爪的鸟窝 428
上一篇: [Leetcode] 722. Remove Comments 解题报告
下一篇: [Leetcode] 724. Find Pivot Index 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值