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

算法 day 51

今日ps:在力扣上其实也都有原题,可以搜到。

岛屿计数(dfs)

个人觉得,版本二的代码会更好理解一些。在处理边界的时候会更清晰。

#include <iostream>
#include <vector>
using namespace std;
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
void dfs(const vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {if (visited[x][y] || grid[x][y] == 0) return; // 终止条件:访问过的节点 或者 遇到海水visited[x][y] = true; // 标记访问过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;  // 越界了,直接跳过dfs(grid, visited, nextx, nexty);}
}int main() {int n, 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];}}vector<vector<bool>> visited(n, vector<bool>(m, false));int result = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (!visited[i][j] && grid[i][j] == 1) {result++; // 遇到没访问过的陆地,+1dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true}}}cout << result << endl;
}

岛屿计数(bfs)

此题需要注意到超时的情况,也就是加入队列的时机。在图论中,一访问到,就要立刻入队。

#include <iostream>
#include <vector>
#include <queue>
using namespace std;int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
void bfs(const vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {queue<pair<int, int>> que;que.push({x, y});visited[x][y] = true; // 只要加入队列,立刻标记while(!que.empty()) {pair<int ,int> cur = que.front(); que.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) {que.push({nextx, nexty});visited[nextx][nexty] = true; // 只要加入队列立刻标记}}}
}int main() {int n, 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];}}vector<vector<bool>> visited(n, vector<bool>(m, false));int result = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (!visited[i][j] && grid[i][j] == 1) {result++; // 遇到没访问过的陆地,+1bfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true}}}cout << result << endl;
}

最大岛屿的面积

这个题dfs更好些,给到核心代码模式吧,在力扣上通过的。

class Solution {
public:int count;int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
void dfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {if (visited[x][y] || grid[x][y] == 0) return; // 终止条件:访问过的节点 或者 遇到海水visited[x][y] = true; // 标记访问过count++;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;  // 越界了,直接跳过dfs(grid, visited, nextx, nexty);}
}int maxAreaOfIsland(vector<vector<int>>& grid) {vector<vector<bool>> visited = vector<vector<bool>>(grid.size(), vector<bool>(grid[0].size(), false));int result = 0;for (int i = 0; i < grid.size(); i++) {for (int j = 0; j < grid[0].size(); j++) {if (!visited[i][j] && grid[i][j] == 1) {count = 0; // 因为dfs处理当前节点,所以遇到陆地计数为0,进dfs之后在开始从1计数dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 trueresult = max(result, count);}}}return result;}
};

http://www.dtcms.com/a/597850.html

相关文章:

  • BI二维数据可视化大屏升级三维可视化大屏:前端开发者下一个内卷赛道
  • 插补算法(逐点比较法)+PWM配置操作
  • 唐山网站制作app新郑市网站建设
  • 买完阿里云域名如何做网站网站商业授权
  • QEMU 使用 Open vSwitch网桥连接虚拟机网络
  • 充气泵方案:充气泵与汽车的关系
  • 北京P2P公司网站建设网站建设合同 模板 下载
  • 贴片机编程:提高生产效率与精度的关键技术 | 贴片机编程技巧与注意事项详解
  • 深度学习_三层神经网络传播案例(L0->L1->L2)
  • 营销类网站建设需要注意的问题国家信用信息公示系统官网山东
  • 第四章:C# 面向对象编程详解:从类与对象到完整项目实践
  • DDoS防护:为企业业务保驾护航的高可用盾牌
  • 企业产品做哪个网站推广好建筑培训课程有哪些
  • 模版 c++
  • LLaMA Factory微调大模型
  • UaGateway构建高可用OPC UA架构:实现冗余通信与数据聚合
  • Linux之vmlinux文件段布局和arm64 的链接脚本vmlinux.lds.S分析
  • C#6、三种主要的错误类型是什么
  • 使用Selenium进行网页自动化
  • 论坛网站建设推广优化wordpress主题下载资源
  • 网站推广文章范例在哪里查企业信息最准确
  • MCP驱动的Rgentic RRG(向量数据库+网络搜索)
  • 网站漏洞怎么修复上海劳务市场招聘信息查询
  • 异常的分类
  • 做时时网站要多少钱用虚拟机做网站服务器
  • 任性动图教程2 -如何让诗词生成动图
  • 零基础学JAVA--Day29( StringBuffer+ StringBuilder)
  • 做源码演示的网站果洛wap网站建设多少钱
  • Zabbix7设置告警邮件提醒
  • 【九、Linux访问网络附加存储】