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

代码随想录算法训练营第五十天|99.岛屿数量、100.岛屿的最大面积

题目链接:99.岛屿数量

解题思路:深度优先搜索

具体思路:

首先定义上下左右四个方向的数组 dir,用于 dfs 时遍历相邻位置,读取矩阵的行数 n 和列数 m,构建二维数组 grid 并输入每个位置的数值,初始化与矩阵大小一致的访问标记数组 visited 初始全为 false 和岛屿计数 ans 初始为 0,双层循环遍历网格的每个位置,若该位置未被访问且为陆地,则判定为新岛屿,ans 加 1 并标记该位置为已访问,调用 dfs 函数遍历该岛屿的所有相邻陆地,dfs 函数接收 grid 数组、访问数组和当前位置,遍历四个方向计算下一个位置,先判断是否越界,越界则跳过,若下一个位置未被访问且为陆地,标记为已访问并递归调用 dfs,直至该岛屿的所有陆地都被标记为已访问,最终输出 ans 即为网格中的岛屿总数。

具体代码:

#include<iostream> #include<vector> using namespace std; int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; void dfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) { for (int i = 0; i < 4; i++) { int nextx = x + dir[i][0]; int nexty = y + dir[i][1]; if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { visited[nextx][nexty] = true; dfs(grid, visited, nextx, nexty); } } } int main () { int n; int m; cin >> n >> m; vector<vector<int>> grid(n, vector<int>(m, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> grid[i][j]; } } int ans = 0; vector<vector<bool>> visited(n, vector<bool>(m, false)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (!visited[i][j] && grid[i][j] == 1) { visited[i][j] = true; ans++; dfs(grid, visited, i, j); } } } cout << ans << endl; }

解题思路:广度优先搜索

具体思路:

首先定义上下左右四个方向的数组 dir,用于 bfs 时遍历当前位置的相邻位置,读取矩阵的行数 n 和列数 m,构建二维数组 grid 并输入每个位置的数值,初始化与矩阵大小一致的访问标记数组 visited 初始全为 false 和岛屿计数 ans 初始为 0,双层循环遍历网格的每个位置,若该位置未被访问且为陆地,则判定为新岛屿,ans 加 1 并标记该位置为已访问,调用 bfs 函数遍历该岛屿的所有相邻陆地,bfs 函数接收 grid 数组、访问数组和初始陆地位置 (x, y),先初始化队列并将 (x, y) 入队,然后循环取出队首节点 (curx, cury),遍历四个方向计算相邻位置,先判断相邻位置是否越界,越界则跳过,若相邻位置未被访问且为陆地,则标记该位置为已访问并将其入队,直至队列为空,即该岛屿的所有陆地均被标记,最终输出 ans 即为网格中的岛屿总数。

具体代码:

#include<iostream> #include<vector> #include<queue> using namespace std; int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; void bfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) { queue<pair<int, int>> queue; queue.push({x, y}); while(!queue.empty()) { pair<int, int> cur = queue.front(); queue.pop(); int curx = cur.first; int cury = cur.second; for (int i = 0; i < 4; i++) { int nextx = curx + dir[i][0]; int nexty = cury + dir[i][1]; if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { queue.push({nextx, nexty}); visited[nextx][nexty] = true; } } } } int main () { int n; int m; cin >> n >> m; vector<vector<int>> grid(n, vector<int>(m, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> grid[i][j]; } } int ans = 0; vector<vector<bool>> visited(n, vector<bool>(m, false)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (!visited[i][j] && grid[i][j] == 1) { visited[i][j] = true; ans++; bfs(grid, visited, i, j); } } } cout << ans << endl; }

题目链接:100.岛屿的最大面积

解题思路:深度优先搜索

具体思路:

首先定义上下左右四个方向的数组 dir,用于 dfs 遍历当前位置的相邻陆地,声明全局变量 area 记录当前岛屿的面积,读取矩阵的行数 n 和列数 m,构建二维数组 grid 并输入每个位置的数值,初始化与矩阵大小一致的访问标记数组 visited 初始全为 false 和全局最大面积 ans 初始为 0,通过双层循环遍历网格的每个位置,若该位置未被访问且为陆地,则将当前岛屿面积 area 初始化为 1计入当前这块陆地,标记该位置为已访问,调用 dfs 函数遍历该岛屿的所有相邻陆地,dfs 函数接收 grid 数组、访问数组和当前位置,遍历四个方向计算相邻位置,先判断是否越界,越界则跳过,若相邻位置未被访问且为陆地,标记该位置为已访问、将 area 加 1 并递归调用 dfs,每完成一个岛屿的 dfs 遍历后,用当前岛屿的面积 area 更新全局最大面积 ans ,取 ans 和 area 的最大值,最终输出 ans 即为网格中最大的岛屿面积。

具体代码:

#include<iostream> #include<vector> using namespace std; int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; int area; void dfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) { for (int i = 0; i < 4; i++) { int nextx = x + dir[i][0]; int nexty = y + dir[i][1]; if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { visited[nextx][nexty] =true; area++; dfs(grid, visited, nextx, nexty); } } } int main() { int n; int m; cin >> n >> m; vector<vector<int>> grid(n, vector<int>(m, 0)); for (int i = 0 ; i < n; i++) { for (int j = 0; j < m; j++) { cin >> grid[i][j]; } } int ans = 0; vector<vector<bool>> visited(n, vector<bool>(m, false)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (!visited[i][j] && grid[i][j] == 1) { area = 1; visited[i][j] = true; dfs(grid, visited, i, j); ans = max(area, ans); } } } cout << ans << endl; }

解题思路:广度优先搜索

具体思路:

首先定义上下左右四个方向的数组 dir,用于 bfs 遍历当前位置的相邻陆地,声明全局变量 area 记录当前岛屿的面积,读取矩阵的行数 n 和列数 m,构建二维数组 grid 并输入每个位置的数值,初始化与矩阵大小一致的访问标记数组 visited 初始全为 false 和全局最大面积 ans 初始为 0,通过双层循环遍历网格的每个位置,若该位置未被访问且为陆地,则将当前岛屿面积 area 初始化为 1,计入当前这块陆地,标记该位置为已访问,调用 bfs 函数遍历该岛屿的所有相邻陆地,bfs 函数接收 grid 数组、访问数组和初始陆地位置 (x, y),先初始化队列并将 (x, y) 入队,然后循环取出队首节点 (curx, cury),遍历四个方向计算相邻位置,先判断相邻位置是否越界,越界则跳过,若相邻位置未被访问且为陆地,标记该位置为已访问、将 area 加 1 并将该位置入队,直至队列为空,即该岛屿的所有陆地均被遍历,每完成一个岛屿的 bfs 遍历后,用当前岛屿的面积 area 更新全局最大面积 ans,取 ans 和 area 的最大值,最终输出 ans 即为网格中最大的岛屿面积。

具体代码:

#include<iostream> #include<vector> #include<queue> using namespace std; int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; int area; void bfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) { queue<pair<int, int>> queue; queue.push({x, y}); while(!queue.empty()) { pair<int, int> cur = queue.front(); queue.pop(); int curx = cur.first; int cury = cur.second; for (int i = 0; i < 4; i++) { int nextx = curx + dir[i][0]; int nexty = cury + dir[i][1]; if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { visited[nextx][nexty] = true; area++; queue.push({nextx, nexty}); } } } } int main() { int n; int m; cin >> n >> m; vector<vector<int>> grid(n, vector<int>(m, 0)); for (int i = 0 ; i < n; i++) { for (int j = 0; j < m; j++) { cin >> grid[i][j]; } } int ans = 0; vector<vector<bool>> visited(n, vector<bool>(m, false)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (!visited[i][j] && grid[i][j] == 1) { area = 1; visited[i][j] = true; bfs(grid, visited, i, j); ans = max(area, ans); } } } cout << ans << endl; }
http://www.jsqmd.com/news/461746/

相关文章:

  • 回忆录优质品牌推荐:祖辈回忆录老照片修复/老华侨落叶归根回忆录与口述历史/老干部回忆录代笔与排版/重症家属生命回忆录抢救拍摄/选择指南 - 优质品牌商家
  • 【OpenClaw】史上最猛更新!AI记忆可自由插拔,开发者等了半年
  • Spring 的循环依赖
  • 探秘书匠策AI:解锁课程论文写作的“智慧钥匙”
  • 安装 OpenClaw
  • PbootCMS错误提示:执行SQL发生错误!错误:no such column: def1
  • 访问修饰符的基础面试题
  • 一款用在导弹上的自粘胶带:TJD-103(J)
  • canal和ES同步失败维护步骤
  • 基于Simulink的轮胎动力学模型(魔术公式)探索
  • # GEO霸屏,电商企业找上海企服猫就对啦!家人们,做电商的都知道,在这个竞争激烈的市场里,想要脱颖而出,那可太难了。今天咱就聊聊电商企业的引流利器——GEO霸屏,还会给大家分享一些超实用的实操建议
  • 2026年靠谱的大连考公品牌推荐:大连考公集训营/大连考公国考面试专项班行业内知名推荐公司 - 行业平台推荐
  • 散热器产业的下一步:从“金属件”到“系统级热管理模块”的结构升级
  • 一类并查集维护的区间染色问题
  • 替代WSTCC1130T双节锂电池充电IC集成均衡充功能
  • Win11操作系统激活
  • PPT Timer:一个置顶于PPT全屏放映之上的LCD倒计时器
  • AI赛博飞升,我们离“仙界”还远么
  • OpenClaw 完整安装指南
  • windows通过wsl的方式安装ubuntu系统(含离线方式)
  • (windows)本地安装openclaw,完成配置并接入本地大模型(ollama)全流程指南
  • 浏览器自动将http访问链接自动转化为https链接,解决办法
  • c++ static关键字的详细用法和作用
  • Spring的IOC详解
  • 2026年苏州青少年篮球培训怎么选?这几家TOP机构值得关注!
  • Claude Code 隐藏功能大全:90%的人不知道这些
  • 150 万人被偷家之后,我翻了翻自己的 API Key 管理,冷汗直流
  • 帮朋友拆了一个机械臂问题,从误解到最优解
  • FFMPEG网络推流
  • 技术落地解析:深圳市兴通物联俄罗斯诚信标签条码比对系统,提升对俄出口合规效率