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

网上做网站兼职陕西网络营销优化公司

网上做网站兼职,陕西网络营销优化公司,文档管理软件,电子政务网站代码图论 题目 99. 岛屿数量 使用 DFS 实现方法 判断岛屿方法 1. 遍历图,若遍历到了陆地 grid[i][j] 1 并且陆地没有被访问,在这个陆地的基础上进行 DFS 方法,或者是 BFS 方法 2. 对陆地进行 DFS 的时候时刻注意以访问的元素添加访问标记 //…

图论

题目

99. 岛屿数量
使用 DFS 实现方法
判断岛屿方法
1. 遍历图,若遍历到了陆地 grid[i][j] = 1 并且陆地没有被访问,在这个陆地的基础上进行 DFS 方法,或者是 BFS 方法
2. 对陆地进行 DFS 的时候时刻注意以访问的元素添加访问标记
在这里插入图片描述

// DFS方法1
#include <iostream>
#include <vector>int dir[4][2] = {0,1,1,0,-1,0,0,-1};
void dfs(std::vector<std::vector<int>>& grid, std::vector<std::vector<bool>>& vis, int x, int y) {// 从四个方向进行搜索for (int i = 0; i < 4; ++i) {int nexti = x + dir[i][0];int nextj = y + dir[i][1];// 边界情况判定if (nexti < 0 || nexti >= grid.size()|| nextj < 0 || nextj >= grid[0].size()) continue;// 访问情况判定if (!vis[nexti][nextj] && grid[nexti][nextj] == 1) {// 时刻注意访问标记vis[nexti][nextj] = true;dfs(grid, vis, nexti, nextj);}}
}int main() {// 输入处理int n, m, res = 0;std::cin >> n >> m;std::vector<std::vector<int>> grid(n, std::vector<int>(m, 0));std::vector<std::vector<bool>> vis(n, std::vector<bool>(m, false));for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {std::cin >> grid[i][j];}}// 遍历网格进行DFS操作for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {// 访问到新陆地对该陆地进行遍历if (grid[i][j] == 1 && !vis[i][j]) {res++; // 记录新大陆vis[i][j] = true;dfs(grid, vis, i, j);}}}// 输出处理std::cout << res << std::endl;
}// dfs 三部曲写法
#include <iostream>
#include <vector>
using namespace std;int dir[4][2] = {0,-1,-1,0,1,0,0,1}; // 逆时针顺序
// 第一步参数与返回值
void dfs(vector<vector<int>>& grid, vector<vector<bool>>& vis, int x, int y) {// 第二部 终止条件if (vis[x][y] || grid[x][y] == 0) return;// 第三部 单层递归逻辑vis[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, vis, nextX, nextY);}
}int main() {int n, m, res = 0;cin >> n >> m;vector<vector<int>> grid(n, vector<int>(m, 0));vector<vector<bool>> vis(n, vector<bool>(m, false));for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {cin >> grid[i][j];}}for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if (!vis[i][j] && grid[i][j] == 1) {res++;dfs(grid, vis, i, j);}}}cout << res << endl;
}

使用广度优先搜索方法
主函数部分不变,调用变成广度优先搜索
广度优先搜索保证入队列的时候就对数据做标记为访问
如果在取出队列时候标记会导致大量重复元素入队!

#include <iostream>
#include <vector>
#include <queue>
using namespace std;int dir[4][2] = {0,-1,-1,0,1,0,0,1};
void bfs(vector<vector<int>>& grid, vector<vector<bool>>& vis, int x, int y) {// 创建队列存储下标queue<pair<int, int>> que;// 保证入队的时候就标记访问防止重复访问que.push(make_pair(x, y));while (!que.empty()) {pair<int, int> cur = que.front();que.pop();for (int i = 0; i < 4; ++i) {int nextX = cur.first + dir[i][0];int nextY = cur.second + dir[i][1];if (nextX < 0 || nextX >= grid.size() || nextY < 0 || nextY >= grid[0].size()) continue;if (!vis[nextX][nextY] && grid[nextX][nextY] == 1) {que.push({nextX, nextY});vis[nextX][nextY] = true; // 入队即标记防止重复}}}
}int main() {int n, m, res = 0;cin >> n >> m;vector<vector<int>> grid(n, vector<int>(m, 0));vector<vector<bool>> vis(n, vector<bool>(m, false));for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {cin >> grid[i][j];}}for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if (!vis[i][j] && grid[i][j] == 1) {res++;bfs(grid, vis, i, j);}}}cout << res << endl;
}

100. 岛屿的最大面积
岛屿最大面积,给出广度优先搜索,深度优先搜索(A 和 B)
深度优先 B广度优先在函数外初始化记录,深度优先 A 在函数内初始化记录

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;int tmp = 0;
int dir[4][2] = {0,-1,-1,0,1,0,0,1};int bfs(vector<vector<int>>& grid, vector<vector<bool>>& vis, int x, int y) {tmp = 1;queue<pair<int, int>> que;que.push(make_pair(x, y));vis[x][y] = true;while (!que.empty()) {pair<int, int> cur = que.front();que.pop();for (int i = 0; i < 4; ++i) {int nextX = cur.first + dir[i][0];int nextY = cur.second + dir[i][1];if (nextX < 0 || nextX >= grid.size() || nextY < 0 || nextY >= grid[0].size()) continue;if (!vis[nextX][nextY] && grid[nextX][nextY] == 1) {tmp++;que.push({nextX, nextY});vis[nextX][nextY] = true;}}}return tmp;
}void dfs(vector<vector<int>>& grid, vector<vector<bool>>& vis, int x, int y) {// 1 finif (vis[x][y] || grid[x][y] == 0) return;vis[x][y] = true;tmp++;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, vis, nextX, nextY);}
}void dfs2(vector<vector<int>>& grid, vector<vector<bool>>& vis, int x, int y) {// 2for (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 (!vis[nextX][nextY] && grid[nextX][nextY] == 1) {tmp++;vis[nextX][nextY] = true;dfs2(grid, vis, nextX, nextY);}}
}int main(){int n, m, res = 0, maxV = 0;cin >> n >> m;vector<vector<int>> grid(n, vector<int>(m, 0));vector<vector<bool>> vis(n, vector<bool>(m, false));for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {cin >> grid[i][j];}}for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if (!vis[i][j] && grid[i][j] == 1) {res++;// 深度优先 1// tmp = 0; // 因为dfs内首先计算tmp// dfs(grid, vis, i, j);// maxV = max(maxV, tmp);// 深度优先 2tmp = 1; // dfs需要初始vis[i][j] = true;dfs2(grid, vis, i, j);maxV = max(maxV, tmp);// 广度优先// maxV = max(maxV, bfs(grid, vis, i, j));}}}// cout << "res" << res << endl;cout << maxV << endl;return 0;
}
http://www.dtcms.com/wzjs/280380.html

相关文章:

  • 网站开发网页超链接路径泉州百度广告
  • 哪个网站做系统好杭州网站建设公司
  • 淘宝客网站开发教程免费域名注册二级域名
  • 政府网站建设 政府采购十大接单推广app平台
  • 源码网棋牌海城seo网站排名优化推广
  • 网页设计师培训费用上海seo公司
  • 动态网站建设简介北京百度seo服务
  • 企商网站建设友情手机站
  • 个人网站备案所需材料百度游戏排行榜
  • 揭阳网站制作费用互联网营销师是干什么
  • 济南网站制作定制公司模板建站优点
  • 直播网站开发报价周口网站seo
  • 简答网站内容建设的时候内链重要性百度收录网址
  • 网站升级建设费用吗手机如何制作自己的网站
  • 如何管理手机网站首页保定网站制作
  • 网站建设管理人员工作职责怎么进行网站推广
  • 小程序二级分销系统seo网站系统
  • 学做婴儿衣服网站好链接交易网
  • 做校园二手交易网站的目的搜索引擎优化seo是什么
  • 抄袭网站案例关键词的分类和优化
  • 山东省建设人才网站百色seo外包
  • 电子印章在线制作网站网站优化公司开始上班了
  • 郑州做网站制作的公司有别人的交易链接怎么交易
  • 湘潭做网站价格优选磐石网络网络营销swot分析
  • 用php做网站的方法凡科建站代理
  • 昌平网站建设浩森宇特今日国内新闻10则
  • 网站建设费用选择网络专业成都门户网站建设
  • 工作室做网站流程市场推广是做什么的
  • 网站建设客服流程seo sem优化
  • 安装wordpress出现http500优化大师的作用