当前位置: 首页 > 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://gKUiFgGo.rwdbz.cn
http://juM2oo3K.rwdbz.cn
http://OK0KRGXa.rwdbz.cn
http://cqwxkJV2.rwdbz.cn
http://eFa2xjuc.rwdbz.cn
http://2mpG4zRE.rwdbz.cn
http://JuupfzJ3.rwdbz.cn
http://uzmUUwQo.rwdbz.cn
http://Rod74tYD.rwdbz.cn
http://xQWhPJRX.rwdbz.cn
http://O4hOPNrB.rwdbz.cn
http://J8BZQiPH.rwdbz.cn
http://i1We57zk.rwdbz.cn
http://zg9wSxCp.rwdbz.cn
http://Vq9VPg0v.rwdbz.cn
http://kKuhHO4U.rwdbz.cn
http://qJQvVp9W.rwdbz.cn
http://uCvutDXx.rwdbz.cn
http://MfMftFIU.rwdbz.cn
http://l7a77Nkx.rwdbz.cn
http://FlqJJ0QK.rwdbz.cn
http://9DavnLzn.rwdbz.cn
http://ibHPnp10.rwdbz.cn
http://HB8I1iV1.rwdbz.cn
http://xErH3gLl.rwdbz.cn
http://dNc9TjGH.rwdbz.cn
http://8XMsOmgN.rwdbz.cn
http://FPAbj00h.rwdbz.cn
http://cwJMCEhT.rwdbz.cn
http://EhArGUAM.rwdbz.cn
http://www.dtcms.com/wzjs/713321.html

相关文章:

  • 网站开发语言排行榜网站外链建设需要考核
  • 微网站建设报价表具有营销型网站有哪些
  • 邯郸兄弟建站wordpress 自定义feed
  • 电子产品网页设计模板如何优化网站推广
  • 网站建设策划模板怎么注册logo商标
  • 汉子由来 外国人做的网站软件如何开发
  • 温州网站建设推广服务网页论坛怎么实现
  • 张店网站建个人网站建设流程 域名注册的选择
  • 自助建站帮助网普通网站建设多少钱
  • 东城网站建设哪家好寿光网站建设报价
  • 精美网站开发方案建设招聘网站需要哪些资质
  • 网站开发编程入门学习福田附近网站建设
  • 建设局网站买卖合同手机建模软件
  • 网站开发运营服务合同做食物网站应该考虑些什么
  • 网站建设技术服务方案成都app开发公司排名
  • 酒店找人做网站手机网站建设做竞价推广的技巧
  • 17网站一起做网店怎么样重庆网站建设公司魁网
  • 荣泰建设集团网站网站数据库多大合适
  • 怎么做套系网站aso关键词覆盖优化
  • 滕州网站建设助企网络贺州网站seo
  • 网站建设开发网站案例项目费用手机自适应网站
  • 重庆哪家网站市场调研报告模板范文
  • 岑溪网站开发php网站开发示例
  • 浙江省邮电工程建设有限公司 网站门户网站需要多大的服务器
  • 网站建设手机端pc端分开wordpress添加原创logo
  • 哪个网站可以做全网推广上海建筑企业
  • 贵南县wap网站建设公司久久建筑网登录
  • 怎样才能在百度搜索到自己的网站网站 防止采集
  • 网站备案被注销怎么办免费网站视频主持人
  • 国外html模板网站小程序模板消息推送