floodfill算法题目

2024-03-12 11:04
文章标签 算法 题目 floodfill

本文主要是介绍floodfill算法题目,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

大家好,我是jiantaoyab,在下面的题目中慢慢体会floodFill算法,虽然是新的算法,但是用的思想和前面的文章几乎一样,代码格式也几乎一样,但不要去背代码

图像渲染

https://leetcode.cn/problems/flood-fill/

解析

image-20240311085056178

代码

可以看到代码这部分,是不是和前面的文章的挺像的

class Solution {int m, n;int pre_color;int dx[4] = {0,0,1,-1};int dy[4] = {1,-1,0,0};
public:void dfs(vector<vector<int>>& image, int sr, int sc, int color){image[sr][sc] = color;for(int d = 0; d < 4; d++){int x = sr + dx[d], y = sc + dy[d];if((x >= 0 && x < m) && (y >= 0 && y < n) &&  image[x][y] == pre_color){image[x][y] = color;dfs(image, x, y, color);}}}vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {m = image.size(), n = image[0].size();pre_color = image[sr][sc];if(image[sr][sc] == color) return image;dfs(image, sr, sc, color);return image; }
};

岛屿数量

https://leetcode.cn/problems/number-of-islands/

解析

image-20240311101256605

代码

class Solution {int m, n;vector<vector<bool>> check;int dx[4]= {0, 0, 1, -1};int dy[4]= {1, -1, 0, 0};
public:void dfs(vector<vector<char>>& grid, int i, int j){check[i][j] = true; //从i,j位置来的for(int d = 0; d < 4; d++){int x = i + dx[d], y = j + dy[d];if((x >= 0 && x < m) && (y >= 0 && y < n)  && grid[x][y] == '1' && !check[x][y]){dfs(grid, x, y);}}}int numIslands(vector<vector<char>>& grid) {int m = grid.size(), n = grid[0].size();check = vector<vector<bool>> (m ,vector<bool>(n));int ret = 0;//把整个grid遍历一次for(int i = 0; i < m; i++){for(int j = 0; j < n; j++){//如果是一个岛屿而且是没有出现过的if(grid[i][j] == '1' && !check[i][j]){ret++;dfs(grid, i, j);}}}return ret;}
};

岛屿的最大面积

https://leetcode.cn/problems/ZL6zAn/

解析

大家看这个图就知道题目求的是什么了,比起上一题,多个统计数

image-20240311093330311

代码

class Solution {    int m, n;bool check[51][51];int dx[4] = {1,-1,0,0};int dy[4] = {0, 0,1,-1};int count;
public:void dfs(vector<vector<int>>& grid, int i, int j){count++;check[i][j] = true;for(int d = 0; d < 4; d++){int x = i + dx[d], y = j + dy[d];if(x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1  && !check[x][y]){dfs(grid, x, y);}}}int maxAreaOfIsland(vector<vector<int>>& grid) {m = grid.size(), n = grid[0].size();int ret = 0;for(int i = 0; i < m; i++){for(int j = 0; j < n; j++){if(!check[i][j] && grid[i][j] == 1){count = 0;dfs(grid, i, j);ret = max(ret, count);}}}return ret;}
};

被围绕的区域

https://leetcode.cn/problems/surrounded-regions/

解析

image-20240311105332642

代码

class Solution {int m, n;int dx[4] = {0,0,1,-1};int dy[4] = {1,-1,0,0};
public:void dfs(vector<vector<char>>& board, int i, int j){board[i][j] = 'a';for(int d = 0; d < 4; d++){int x = i + dx[d], y = j + dy[d];if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O'){dfs(board, x, y);}}}void solve(vector<vector<char>>& board) {m = board.size(), n = board[0].size();//把左右2列边界处理了for(int i = 0; i < m; i++){if(board[i][0] == 'O') dfs(board, i, 0);if(board[i][n-1] == 'O') dfs(board, i, n-1);}//把上下2行边界处理了for(int j = 0; j < n; j++){if(board[0][j] == 'O') dfs(board, 0, j);if(board[m-1][j] == 'O') dfs(board, m-1, j);}//还原 + 修改for(int i = 0; i < m; i++){for(int j = 0; j < n; j++){if(board[i][j] == 'a') board[i][j] = 'O';else if(board[i][j] == 'O') board[i][j] = 'X';}}}
};

太平洋大西洋水流问题

https://leetcode.cn/problems/pacific-atlantic-water-flow/

解析

image-20240311172851034

代码

class Solution {int m, n;int dx[4] = {1, -1, 0, 0};int dy[4] = {0,  0, 1, -1};
public:void dfs(vector<vector<int>>& heights, int i, int j, vector<vector<bool>>&check){check[i][j] = true;for(int d = 0; d < 4; d++){int x = i + dx[d], y = j + dy[d];if(x >= 0 && x < m && y >= 0 && y < n &&  !check[x][y] &&heights[x][y] >= heights[i][j] ){dfs(heights, x, y, check);}}}vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {m = heights.size(), n = heights[0].size();vector<vector<bool>> pac(m, vector<bool>(n));vector<vector<bool>> atl(m, vector<bool>(n));//处理pacfor(int j = 0; j < n; j++) dfs(heights, 0, j, pac);for(int i = 0; i < m; i++) dfs(heights, i, 0, pac);//处理altfor(int j = 0; j < n; j++) dfs(heights, m - 1, j, atl);for(int i = 0; i < m; i++) dfs(heights, i, n - 1, atl);vector<vector<int>> ret;for(int i = 0; i < m; i++){for(int j = 0; j < n; j++){if(pac[i][j] && atl[i][j])ret.push_back({i, j});}}return ret;}
};

扫雷游戏

https://leetcode.cn/problems/minesweeper/

解析

image-20240311180338853

代码

class Solution {int dx[8] = {0, 0, -1, 1, 1, 1, -1 ,-1};int dy[8] = {1, -1, 0, 0, 1, -1, 1 ,-1};int m, n;
public:void dfs(vector<vector<char>>& board, int i, int j){int count = 0; //地雷个数//统计地雷个数for(int d = 0; d < 8; d++){int x = i + dx[d], y = j + dy[d];if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'M'){count++;}}//周围有地雷if(count){board[i][j] = count + '0';return ;}//周围没地雷展开else{board[i][j] = 'B';for(int d = 0; d < 8; d++){int x = i + dx[d], y = j + dy[d];if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'E'){dfs(board, x, y);}}}}vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {m = board.size(), n = board[0].size();int x = click[0], y = click[1];//开局中地雷if(board[x][y] == 'M'){board[x][y] = 'X';return board;}dfs(board, x, y);return board;}
};

这篇关于floodfill算法题目的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/801061

相关文章

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1