当前位置: 首页 > news >正文

[力扣题解] 695. 岛屿的最大面积

题目:695. 岛屿的最大面积

思路

代码

深度优先搜索

// 深度搜索
class Solution {
private:int area_max = 0;int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};void dfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y, int& area){int m = grid.size(), n = grid[0].size();int i, j;int next_x, next_y;for(i = 0; i < 4; i++){next_x = x + dir[i][0];next_y = y + dir[i][1];if(next_x < 0 || next_x >= m || next_y < 0 || next_y >= n){continue;} if(!visited[next_x][next_y] && grid[next_x][next_y] == 1){visited[next_x][next_y] = true;area++;dfs(grid, visited, next_x, next_y, area);}}}public:int maxAreaOfIsland(vector<vector<int>>& grid) {int m = grid.size(), n = grid[0].size();int i, j;int area = 0, area_max = 0;vector<vector<bool>> visited(m, vector<bool>(n, 0));for(i = 0; i < m; i++){for(j = 0; j < n; j++){if(!visited[i][j] && grid[i][j] == 1){area = 1;visited[i][j] = true;dfs(grid, visited, i, j, area);if(area_max < area){area_max = area;}}}}return area_max;}
};
http://www.lryc.cn/news/353981.html

相关文章:

  • AI模型发展路径探析:开源与闭源,何者更胜一筹?
  • concurrency 并行编程
  • JavaScript如何让一个按钮的点击事件在完成之前禁用
  • 透视App投放效果,Xinstall助力精准分析,让每一分投入都物超所值!
  • 【Linux杂货铺】进程通信
  • 常用API(正则表达式、爬取、捕获分组和非捕获分组 )
  • JVM学习-Class文件结构②
  • 数据库连接项目
  • MySQL--InnoDB体系结构
  • ffplay 使用文档介绍
  • 四种网络IO模型
  • Mixed-precision计算原理(FP32+FP16)
  • Go 控制协程(goroutine)的并发数量
  • web安全渗透测试十大常规项(一):web渗透测试之CSRF跨站请求伪造
  • YOLOv10尝鲜测试五分钟极简配置
  • 社交媒体数据恢复:聊天宝
  • 备战秋招—模拟版图面试题来了
  • CAN总线简介
  • 【HSQL001】HiveSQL内置函数手册总结(更新中)
  • Rust面试宝典第14题:旋转数组
  • 解决SpringBoot中插入汉字变成?(一秒解决)
  • 5.26牛客循环结构
  • AIGC 004-T2I-adapter另外一种支持多条件组合控制的文生图方案!
  • 详解 Cookies 和 WebStorage
  • BeanFactory、FactroyBean、ApplicationContext
  • 【计算机网络】HTTPS 协议原理
  • springboot + Vue前后端项目(第十二记)
  • linux 常用命令:find grep ps netstat sudo df du rm
  • SQLiteOpenHelper数据库帮助器
  • 2024年5月26日 (周日) 叶子游戏新闻