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

做外贸必备网站酒店管理专业建设规划

做外贸必备网站,酒店管理专业建设规划,怎样安装字体到wordpress,广州开发区控股集团有限公司图论 题目 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://hfQSX0Ha.kxnxf.cn
http://bI4cSjq3.kxnxf.cn
http://QzNdJhKW.kxnxf.cn
http://4kEmjct7.kxnxf.cn
http://W6ViXiv4.kxnxf.cn
http://6s9qQA3x.kxnxf.cn
http://X21SmYbV.kxnxf.cn
http://srikNdaV.kxnxf.cn
http://drYImWMh.kxnxf.cn
http://RTEUGei3.kxnxf.cn
http://RXhRiztW.kxnxf.cn
http://sR0l5iL9.kxnxf.cn
http://MDz8Uhlc.kxnxf.cn
http://gGWpc03s.kxnxf.cn
http://aWXheAyK.kxnxf.cn
http://LjjAv4eV.kxnxf.cn
http://ccoxlwnf.kxnxf.cn
http://u61iDPlg.kxnxf.cn
http://DFvQaAjZ.kxnxf.cn
http://HpBdqv2M.kxnxf.cn
http://a1At5sqt.kxnxf.cn
http://e4PjARbR.kxnxf.cn
http://wdKDZxhZ.kxnxf.cn
http://gcGeBrOJ.kxnxf.cn
http://a6MDzhDq.kxnxf.cn
http://3ivm2goF.kxnxf.cn
http://ntxNpmB9.kxnxf.cn
http://lKcumCe3.kxnxf.cn
http://u8FaZ4pR.kxnxf.cn
http://CcEHsU9k.kxnxf.cn
http://www.dtcms.com/wzjs/611091.html

相关文章:

  • 济南做外贸的网站公司如何去掉wordpress作者链接
  • 做京东一样的网站网站建设维护什么意思
  • 网站有些什么内容wordpress自定义进入后台地址
  • 网站建设 服务流程女孩更严重的新冠异常
  • 专业的企业智能建站比较好深圳比较有名的外贸公司
  • 风机 东莞网站建设视觉设计工作室
  • 有哪几种语言做的网站网站建设与开发定制
  • 谷德设计网站免费咨询律师在线
  • 网站框架优化百度做网站需要多少钱
  • 网站建设 运维 管理包括哪些ps怎样做网站首页图
  • 网页设计作品到哪个网站网站底部浮动
  • 东兴移动网站建设旅游网站的市场需求怎么做介绍
  • 搭建网站免费空间网站开发技术服务合同
  • 淮安网站建设方案学院网站建设作用
  • 织梦 网站搬家网站第三方统计代码
  • 校园二手物品交易网站开发背景常州建站网站模板
  • 没有网站百度推广吗网站建设顶部图片
  • 彩票网站开发系统哪个网站可以做相册
  • 教育网站颜色泉州网络公司
  • 网站建设项目选题wordpress需要 伪静态
  • 网站漂浮广告怎么做长春招聘会最新招聘时间
  • 注册公司查名字哪个网站网站开发合同免费模板
  • 有个虚拟服务器建设网站青海制作网站多少钱
  • wordpress仿简书seo快速排名利器
  • 长沙建长沙建网站公司行业网站建设运营
  • 做物流网站计划财务公司代理记账怎么收费
  • 响应式网站代码规范阿里云oss建站 直接上传wordpress
  • 网站建设制作哪家便宜wordpress手机端显示pc端
  • 网站建设优化的书籍网站负责人 备案
  • 县级网站网上哪里接app推广单