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

找人做网站要拿到源代码吗中国最新消息今天

找人做网站要拿到源代码吗,中国最新消息今天,电子商务网站建设策划书范文,北京怎么做网站图论 题目 101. 孤岛的总面积 计算孤岛总面积&#xff0c;一个想法是将相邻的陆地的位置置为 0&#xff0c;最后计算孤岛面积中最小的一个 #include <iostream> #include <vector> #include <queue>using namespace std;int sum 0; int dir[4][2] {0,-1…

图论

题目

101. 孤岛的总面积
计算孤岛总面积,一个想法是将相邻的陆地的位置置为 0,最后计算孤岛面积中最小的一个

#include <iostream>
#include <vector>
#include <queue>using namespace std;int sum = 0;
int dir[4][2] = {0,-1,-1,0,0,1,1,0};// 这里面的vis没用到写出来就是联系模板代码用的
void dfs(vector<vector<int>>& grid, vector<vector<bool>>& vis, int x, int y) {if (vis[x][y] || grid[x][y] == 0) return;grid[x][y] = 0;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);}
}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));vis[x][y] = true;grid[x][y] = 0;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 (grid[nextX][nextY] == 1 && !vis[nextX][nextY]) {grid[nextX][nextY] = 0;vis[nextX][nextY] = true;que.push({nextX, nextY});}}}
}int main() {int n, m;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];}}// 将紧邻陆置为0for (int i = 0; i < n; ++i) {if (grid[i][0] == 1) dfs(grid, vis, i, 0);if (grid[i][m-1] == 1) dfs(grid, vis, i, m-1);}for (int j = 0; j < m; ++j) {if (grid[0][j] == 1) bfs(grid, vis, 0, j);if (grid[n-1][j] == 1) bfs(grid, vis, n-1, j);}// 遍历孤岛总面积for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if (grid[i][j] == 1) sum++;}}cout << sum << endl;
}

102. 沉没孤岛
沉默孤岛就可以使用 vis 数组了,遍历大陆框架
For (int i = 0; i < n; ++i) 这样从边框遍历,并记录遍历结果,最后 vis 就是我们访问过的非孤岛
返回 vis 就是将孤岛沉没后的结果

#include <iostream>
#include <vector>
#include <queue>using namespace std;int dir[4][2] = {0,-1,-1,0,0,1,1,0};
void dfs(vector<vector<int>>& grid, vector<vector<bool>>& vis, int x, int y) {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;if (!vis[nextX][nextY] && grid[nextX][nextY] == 1) {dfs(grid, vis, nextX, nextY);}}
}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));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) {vis[nextX][nextY] = true;que.push({nextX, nextY});}} }
}int main() {int n, m;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) {if (grid[i][0] == 1) dfs(grid, vis, i, 0);if (grid[i][m-1] == 1) dfs(grid, vis, i, m-1);}for (int j = 0; j < m; ++j) {if (grid[0][j] == 1) bfs(grid, vis, 0, j);if (grid[n-1][j] == 1) bfs(grid, vis, n-1, j);}for (int i = 0; i < n; ++i) {for (int j = 0; j < m-1; ++j) {cout << vis[i][j] << " ";}cout << vis[i][m-1] << endl;}
}

103. 水流问题
题目是问:有没有一个节点可以让水流流到第一边界与第二边界,如果有输出这个节点的坐标
暴力想法:遍历每一个节点查看是否可以从该节点遍历到第一边界与第二边界
这样可以实现,但是时间复杂度太高,因为要对每个节点遍历整张地图
逆向想法:从第一边界与第二边界边开始逆向遍历
这样就只用遍历四个边的内容而不用遍历到里面的内容,减少了计算量
而且实际代码中流水是一条路径,遍历当前边的其他节点遇见了之前的路径就可以跳过了
Dfs bfs 判断条件就是要大于等于当前值的才继续搜索,从而实现标记
最后通过比较两个 vis 数组都 true的节点输出坐标即可
最后本质上由于 vis 存在每个节点至多访问一次复杂度为 n * m

#include <iostream>
#include <vector>
#include <queue>using namespace std;int dir[4][2] = {0,-1,-1,0,0,1,1,0};void dfs(vector<vector<int>>& grid, vector<vector<bool>>& vis, int x, int y) {// 终止条件访问过了就退出if (vis[x][y]) 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;// 水流情况判断 逆向蔓延往高处流if (grid[x][y] > grid[nextX][nextY]) continue;dfs(grid, vis, nextX, nextY);}return;
}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));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 (grid[cur.first][cur.second] > grid[nextX][nextY]) continue;if (!vis[nextX][nextY]){vis[nextX][nextY] = true;que.push(make_pair(nextX, nextY));}}}
}int main() {int n, m;cin >> n >> m;vector<vector<int>> grid(n, vector<int>(m, 0));vector<vector<bool>> firstVis(n, vector<bool>(m, false));vector<vector<bool>> secondVis(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) {dfs(grid, firstVis, i, 0);dfs(grid, secondVis, i, m-1);}for (int j = 0; j < m; ++j) {bfs(grid, firstVis, 0, j);bfs(grid, secondVis, n-1, j);}// 输出结果 只有两个vis均为true的时候输出坐标for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if (firstVis[i][j] && secondVis[i][j]) {cout << i << " " << j << endl;}}}return 0;
}

104. 建造最大岛屿
建造岛屿问题,默认方法就是遍历
优化方法:由于给出的是岛屿地图因此可以对不同岛屿编号直接利用岛屿编号代替遍历
1. 对岛屿遍历记录岛屿的编号与面积的关系,使用 unordermap
2. 对地图中每个海洋 0 变成 1 后直接遍历周围遍历到岛屿的时候直接取编号相加即可
这里面要防止重复添加可以记录已经添加了的岛屿到集合中搜索到之后就不再添加了
在这里插入图片描述

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>using namespace std;int markIdx = 1, count = 0;
int dir[4][2] = {0,-1,-1,0,0,1,1,0};void dfs(vector<vector<int>>& grid, vector<vector<bool>>& vis, int x, int y, int mark) {if (vis[x][y] || grid[x][y] == 0) return;vis[x][y] = true;grid[x][y] = mark;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, vis, nextX, nextY, mark);}
}int main() {int n, m, res = 0;cin >> n >> m;unordered_map<int, int> map; // 记录岛屿编号与岛屿面积unordered_set<int> set; // 记录参与计算面积的岛屿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];}}bool allLand = true;// 步骤一 遍历岛屿记录岛屿面积与标号 mark从2开始 1表示填海造陆的岛屿for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if (grid[i][j] == 0) allLand = false;if (!vis[i][j] && grid[i][j] == 1) {count = 0;markIdx = markIdx+1;dfs(grid, vis, i, j, markIdx);map[markIdx] = count;}}}if(allLand) {cout << n * m << endl;return 0;}// 步骤二 遍历整个地图将海洋变成岛屿 然后遍历岛屿记录面积for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {count = 1; // 海洋变成了陆地用于记录陆地面积set.clear(); // 清空选择if (grid[i][j] == 0) {for (int k = 0; k < 4; ++k){int nextX = i + dir[k][0];int nextY = j + dir[k][1];if (nextX < 0 || nextX >= grid.size() || nextY < 0 || nextY >= grid[0].size()) continue;// 记录是否添加了这个岛屿if (set.count(grid[nextX][nextY])) continue;count += map[grid[nextX][nextY]];set.insert(grid[nextX][nextY]);}}res = max(res, count);}}cout << res << endl;
}// 模式2#include <iostream>
#include <vector>
#include <queue>
#include <unordered_set>
#include <unordered_map>using namespace std;int count = 0;
int dir[4][2] = {0,-1,-1,0,0,1,1,0};void bfs(vector<vector<int>>& grid, vector<vector<bool>>& vis, int x, int y, int mark) {queue<pair<int, int>> que;que.push({x, y});vis[x][y] = true;grid[x][y] = mark;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;grid[nextX][nextY] = mark;count++;}}}
}int main() {int n,m, markIdx = 2, sum = 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];}}bool allLand = true;unordered_map<int, int> map;for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if (grid[i][j] == 0) allLand = false;if (!vis[i][j] && grid[i][j] == 1) {count = 1;bfs(grid, vis, i, j, markIdx);map[markIdx] = count;markIdx++;}}}if (allLand) {cout << n * m << endl;return 0;}unordered_set<int> set;for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {count = 0;set.clear();if (grid[i][j] == 0) {count = 1;for (int k = 0; k < 4; ++k) {int nextX = i + dir[k][0];int nextY = j + dir[k][1];if (nextX < 0 || nextX >= grid.size() || nextY < 0 || nextY >= grid[0].size()) continue;if (set.count(grid[nextX][nextY])) continue;count += map[grid[nextX][nextY]];set.insert(grid[nextX][nextY]);}}sum = max(sum, count);}}cout << sum << endl;return 0;
}
http://www.dtcms.com/wzjs/339426.html

相关文章:

  • 哪个网站可以做全景图广州seo网络营销培训
  • 色情网站模板百度外链查询工具
  • 广州海珠网站开发定制企业推广宣传方案
  • 南宁网站建设超博网络前端开发培训机构推荐
  • 做网站推广有啥活动seo优化咨询
  • 聊城市城乡建设委员会网站seo查询工具
  • 网站建设网络推广微信网站百度浏览器网页版入口
  • 网站备案域名所有人seo优化搜索结果
  • 园林景观设计网站推荐百度竞价怎么排名第一
  • 公司核名在哪个网站长沙优化网站推广
  • 网站后台是什么网站注册域名
  • 做商城网站要哪些流程湖南seo优化哪家好
  • 开源程序做网站成都自然排名优化
  • 百度快照 直接进入网站如何做网站推广及优化
  • 海南城乡建设庁网站2021最火营销方案
  • 怎么用ngrok做网站陕西新站seo
  • 做logo的比赛网站上海关键词排名优化怎样
  • 网站建制作公司做神马seo快速排名软件
  • 网站开发实践研究报告长沙seo优化公司
  • 中山网站建设企业网络推广怎样做
  • 办公室设计装修咨询长春网站优化服务
  • 西部数码官方网站申请一个网站需要多少钱
  • 成都网站建设多少钱宝塔建站系统
  • 宿州科技网站建设seo优化软件免费
  • 西安网站建设xamokj学习软件
  • 企业网站平台如何做网络推广网络推广优化培训
  • 网站建设模板是什么网站制作网站推广
  • 网站开发和网站建设公司做个网站多少钱
  • 网站怎样自己做推广推广计划书范文
  • 网站跳转怎么办网页设计制作软件