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

天津哪家公司做公司网站杭州seo网站建设

天津哪家公司做公司网站,杭州seo网站建设,计算机网站开发书籍,通过网站编辑发稿是怎么做的刷题记录 101. 孤岛的总面积DFSBFS 102. 沉没孤岛DFSBFS *103. 水流问题*104. 建造最大岛屿 101. 孤岛的总面积 题目地址 本题要求不与矩阵边缘相连的孤岛的总面积。先将与四个边缘相连的岛屿变为海洋,再统计剩余的孤岛的总面积。无需再标识访问过的结点&#xff…

刷题记录

  • 101. 孤岛的总面积
    • DFS
    • BFS
  • 102. 沉没孤岛
    • DFS
    • BFS
  • *103. 水流问题
  • *104. 建造最大岛屿

101. 孤岛的总面积

题目地址

本题要求不与矩阵边缘相连的孤岛的总面积。先将与四个边缘相连的岛屿变为海洋,再统计剩余的孤岛的总面积。无需再标识访问过的结点,因为访问过后都变为海洋了。

时间复杂度: O ( n 2 ) O(n^2) O(n2)
空间复杂度: O ( n 2 ) O(n^2) O(n2)

DFS

// c++
#include<bits/stdc++.h>
using namespace std;
int direction[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
int result = 0;void pre_dfs(vector<vector<int>> &matrix, int x, int  y){matrix[x][y] = 0;result++;for(int i=0; i<4; i++){int nextx = x + direction[i][0];int nexty = y + direction[i][1];if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;if(matrix[nextx][nexty]){matrix[nextx][nexty] = 0;pre_dfs(matrix, nextx, nexty);}}
}int main(){int n,m;cin>>n>>m;vector<vector<int>> matrix(n, vector<int>(m, 0));for(int i=0; i<n; i++){for(int j=0; j<m; j++){cin>>matrix[i][j];}}for(int i=0; i<n; i++) {if(matrix[i][0]){pre_dfs(matrix, i, 0);}if(matrix[i][m-1]){pre_dfs(matrix, i, m-1);}}for(int j=0; j<m; j++){if(matrix[0][j]){pre_dfs(matrix, 0, j);}if(matrix[n-1][j]){pre_dfs(matrix, n-1, j);   }}result = 0;for(int i=0; i<n; i++){for(int j=0; j<m; j++){if(matrix[i][j]){pre_dfs(matrix, i, j);   }}}cout<<result;return 0;
}

BFS

// c++
#include<bits/stdc++.h>
using namespace std;
int direction[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
int result = 0;void pre_dfs(vector<vector<int>> &matrix, int x, int  y){matrix[x][y] = 0;result++;for(int i=0; i<4; i++){int nextx = x + direction[i][0];int nexty = y + direction[i][1];if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;if(matrix[nextx][nexty]){matrix[nextx][nexty] = 0;pre_dfs(matrix, nextx, nexty);}}
}void pre_bfs(vector<vector<int>> &matrix, int x, int  y){queue<pair<int, int>> que;que.push({x, y});matrix[x][y] = 0;result++;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 + direction[i][0];int nexty = cury + direction[i][1];if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;if(matrix[nextx][nexty]){matrix[nextx][nexty] = 0;result++;que.push({nextx, nexty});}}}
}int main(){int n,m;cin>>n>>m;vector<vector<int>> matrix(n, vector<int>(m, 0));for(int i=0; i<n; i++){for(int j=0; j<m; j++){cin>>matrix[i][j];}}/*// dfsfor(int i=0; i<n; i++) {if(matrix[i][0]){pre_dfs(matrix, i, 0);}if(matrix[i][m-1]){pre_dfs(matrix, i, m-1);}}for(int j=0; j<m; j++){if(matrix[0][j]){pre_dfs(matrix, 0, j);}if(matrix[n-1][j]){pre_dfs(matrix, n-1, j);   }}*/// bfsfor(int i=0; i<n; i++) {if(matrix[i][0]){pre_bfs(matrix, i, 0);}if(matrix[i][m-1]){pre_bfs(matrix, i, m-1);}}for(int j=0; j<m; j++){if(matrix[0][j]){pre_bfs(matrix, 0, j);}if(matrix[n-1][j]){pre_bfs(matrix, n-1, j);   }}result = 0;for(int i=0; i<n; i++){for(int j=0; j<m; j++){if(matrix[i][j]){// pre_dfs(matrix, i, j);   pre_bfs(matrix, i, j);}}}cout<<result;return 0;
}

102. 沉没孤岛

题目地址

本题是上一题的反向操作

先把非孤岛做访问标记,再对剩余陆地进行操作。

时间复杂度: O ( n 2 ) O(n^2) O(n2)
空间复杂度: O ( n 2 ) O(n^2) O(n2)

DFS

// c++
#include<bits/stdc++.h>
using namespace std;
int direction[][2] = {0, 1, 0, -1, -1, 0, 1, 0};
void pre_dfs(const vector<vector<int>>& matrix,vector<vector<bool>>& visited,int x, int y){visited[x][y] = true;for(int i=0; i<4; i++){int nextx = x + direction[i][0];int nexty = y + direction[i][1];if(nextx>=matrix.size() || nexty>=matrix.size() ||nextx<0 || nexty<0) continue;if(matrix[nextx][nexty] && !visited[nextx][nexty]){// visited[nextx][nexty] = true;pre_dfs(matrix, visited, nextx, nexty);}}
}void dfs(vector<vector<int>>& matrix,vector<vector<bool>>& visited,int x, int y){matrix[x][y] = 0;for(int i=0; i<4; i++){int nextx = x + direction[i][0];int nexty = y + direction[i][1];if(nextx>=matrix.size() || nexty>=matrix.size() ||nextx<0 || nexty<0) continue;if(matrix[nextx][nexty] && !visited[nextx][nexty]){visited[nextx][nexty] = true;dfs(matrix, visited, nextx, nexty);}}
}
int main(){int n,m;cin>>n>>m;vector<vector<int>> matrix(n, vector<int>(m, 0));vector<vector<bool>> visited(n, vector<bool>(m, false));for(int i=0; i<n; i++){for(int j=0; j<m; j++){cin>>matrix[i][j];}}for(int i=0; i<n; i++){if(matrix[i][0] && !visited[i][0]) pre_dfs(matrix, visited, i, 0);if(matrix[i][m-1] && !visited[i][m-1]) pre_dfs(matrix, visited, i, m-1);}for(int j=0; j<m; j++){if(matrix[0][j] && !visited[0][j]) pre_dfs(matrix, visited, 0, j);if(matrix[n-1][j] && !visited[n-1][j]) pre_dfs(matrix, visited, n-1, j);}for(int i=0; i<n; i++){for(int j=0; j<m; j++){if(matrix[i][j] && !visited[i][j]){visited[i][j] = true;dfs(matrix,visited, i, j);}}for(int j=0; j<m; j++) cout<<matrix[i][j]<<" ";cout<<endl;}return 0;
}

BFS

//c++
#include<bits/stdc++.h>
using namespace std;
int direction[][2] = {0, 1, 0, -1, -1, 0, 1, 0};
void pre_dfs(const vector<vector<int>>& matrix,vector<vector<bool>>& visited,int x, int y){visited[x][y] = true;for(int i=0; i<4; i++){int nextx = x + direction[i][0];int nexty = y + direction[i][1];if(nextx>=matrix.size() || nexty>=matrix.size() ||nextx<0 || nexty<0) continue;if(matrix[nextx][nexty] && !visited[nextx][nexty]){// visited[nextx][nexty] = true;pre_dfs(matrix, visited, nextx, nexty);}}
}void dfs(vector<vector<int>>& matrix,vector<vector<bool>>& visited,int x, int y){matrix[x][y] = 0;for(int i=0; i<4; i++){int nextx = x + direction[i][0];int nexty = y + direction[i][1];if(nextx>=matrix.size() || nexty>=matrix.size() ||nextx<0 || nexty<0) continue;if(matrix[nextx][nexty] && !visited[nextx][nexty]){visited[nextx][nexty] = true;dfs(matrix, visited, nextx, nexty);}}
}void pre_bfs(const vector<vector<int>>& matrix,vector<vector<bool>>& visited,int x, int y){visited[x][y] = true;queue<pair<int, int>> que;que.push({x,y});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 + direction[i][0];int nexty = cury + direction[i][1];if(nextx>=matrix.size() || nexty>=matrix.size() ||nextx<0 || nexty<0) continue;if(matrix[nextx][nexty] && !visited[nextx][nexty]){visited[nextx][nexty] = true;que.push({nextx, nexty});}}}
}void bfs(vector<vector<int>>& matrix,vector<vector<bool>>& visited,int x, int y){visited[x][y] = true;matrix[x][y] = 0;queue<pair<int, int>> que;que.push({x,y});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 + direction[i][0];int nexty = cury + direction[i][1];if(nextx>=matrix.size() || nexty>=matrix.size() ||nextx<0 || nexty<0) continue;if(matrix[nextx][nexty] && !visited[nextx][nexty]){visited[nextx][nexty] = true;matrix[nextx][nexty] = 0;que.push({nextx, nexty});}}}
}
int main(){int n,m;cin>>n>>m;vector<vector<int>> matrix(n, vector<int>(m, 0));vector<vector<bool>> visited(n, vector<bool>(m, false));for(int i=0; i<n; i++){for(int j=0; j<m; j++){cin>>matrix[i][j];}}/*// dfsfor(int i=0; i<n; i++){if(matrix[i][0] && !visited[i][0]) pre_dfs(matrix, visited, i, 0);if(matrix[i][m-1] && !visited[i][m-1]) pre_dfs(matrix, visited, i, m-1);}for(int j=0; j<m; j++){if(matrix[0][j] && !visited[0][j]) pre_dfs(matrix, visited, 0, j);if(matrix[n-1][j] && !visited[n-1][j]) pre_dfs(matrix, visited, n-1, j);}*/// bfsfor(int i=0; i<n; i++){if(matrix[i][0] && !visited[i][0]) pre_bfs(matrix, visited, i, 0);if(matrix[i][m-1] && !visited[i][m-1]) pre_bfs(matrix, visited, i, m-1);}for(int j=0; j<m; j++){if(matrix[0][j] && !visited[0][j]) pre_bfs(matrix, visited, 0, j);if(matrix[n-1][j] && !visited[n-1][j]) pre_bfs(matrix, visited, n-1, j);}for(int i=0; i<n; i++){for(int j=0; j<m; j++){if(matrix[i][j] && !visited[i][j]){// visited[i][j] = true;// dfs(matrix,visited, i, j);bfs(matrix,visited, i, j);}}for(int j=0; j<m; j++) cout<<matrix[i][j]<<" ";cout<<endl;}return 0;
}

*103. 水流问题

题目地址

使用两个标识访问的数组分别从两组边界出发进行dfs遍历,使用从低向高流(反向流)来分别记录两组边界的结点。最后两组边界的交集就是本题答案。
思路

时间复杂度: O ( m ∗ n ) O(m*n) O(mn)
空间复杂度: O ( m ∗ n ) O(m*n) O(mn)

// c++
#include<bits/stdc++.h>
using namespace std;
int direction[][2] = {0, 1, 0, -1, -1, 0, 1, 0};
void dfs(const vector<vector<int>> &matrix, vector<vector<bool>> &visited,int x, int y){visited[x][y] = true;for(int i=0; i<4; i++){int nextx = x + direction[i][0];int nexty = y + direction[i][1];if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;if(matrix[x][y]>matrix[nextx][nexty]) continue;if(!visited[nextx][nexty]) dfs(matrix, visited, nextx, nexty);}
}int main(){int n,m;cin>>n>>m;vector<vector<int>> matrix(n, vector<int>(m, 0));vector<vector<bool>> first(n, vector<bool>(m, false));vector<vector<bool>> second(n, vector<bool>(m, false));for(int i=0; i<n; i++){for(int j=0; j<m; j++){cin>>matrix[i][j];}}for(int i=0; i<n; i++){dfs(matrix, first, i, 0);dfs(matrix, second, i, m-1);}for(int j=0; j<m; j++){dfs(matrix, first, 0, j);dfs(matrix, second, n-1, j);}for(int i=0; i<n; i++){for(int j=0; j<m; j++){if(first[i][j] && second[i][j]) cout<<i<<" "<<j<<endl;}}return 0;   
}

*104. 建造最大岛屿

题目地址

题解思路

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

// c++

文章转载自:
http://irrigator.c7491.cn
http://kenotron.c7491.cn
http://rive.c7491.cn
http://center.c7491.cn
http://donnish.c7491.cn
http://henwife.c7491.cn
http://pki.c7491.cn
http://dearness.c7491.cn
http://seroconvert.c7491.cn
http://litterbug.c7491.cn
http://recumbently.c7491.cn
http://symbolist.c7491.cn
http://saseno.c7491.cn
http://baffle.c7491.cn
http://biology.c7491.cn
http://collaborative.c7491.cn
http://gens.c7491.cn
http://occident.c7491.cn
http://instilment.c7491.cn
http://derequisition.c7491.cn
http://fanaticism.c7491.cn
http://syren.c7491.cn
http://irishwoman.c7491.cn
http://pki.c7491.cn
http://diffusedly.c7491.cn
http://psychoenergetic.c7491.cn
http://chicom.c7491.cn
http://fuci.c7491.cn
http://markman.c7491.cn
http://gingerbread.c7491.cn
http://pitman.c7491.cn
http://flesher.c7491.cn
http://unabroken.c7491.cn
http://inaugurate.c7491.cn
http://hemoprotein.c7491.cn
http://aidance.c7491.cn
http://silphid.c7491.cn
http://materially.c7491.cn
http://falsely.c7491.cn
http://denehole.c7491.cn
http://cladogram.c7491.cn
http://contradict.c7491.cn
http://mortmain.c7491.cn
http://unscramble.c7491.cn
http://strome.c7491.cn
http://ketosteroid.c7491.cn
http://polarisable.c7491.cn
http://asciferous.c7491.cn
http://sanguinopurulent.c7491.cn
http://nc.c7491.cn
http://aerometeorograph.c7491.cn
http://piligerous.c7491.cn
http://ravelin.c7491.cn
http://pennyroyal.c7491.cn
http://quilimane.c7491.cn
http://vinegarette.c7491.cn
http://microstomous.c7491.cn
http://rockstaff.c7491.cn
http://loxodrome.c7491.cn
http://heterozygote.c7491.cn
http://gemmiform.c7491.cn
http://outcaste.c7491.cn
http://qstol.c7491.cn
http://avirulent.c7491.cn
http://recense.c7491.cn
http://subgum.c7491.cn
http://rumbly.c7491.cn
http://evilness.c7491.cn
http://flowerer.c7491.cn
http://orangism.c7491.cn
http://astroarchaeology.c7491.cn
http://gurdwara.c7491.cn
http://glitter.c7491.cn
http://branchiate.c7491.cn
http://meteorologist.c7491.cn
http://batholithic.c7491.cn
http://kieselgur.c7491.cn
http://sext.c7491.cn
http://moonwards.c7491.cn
http://firestone.c7491.cn
http://thecate.c7491.cn
http://packsack.c7491.cn
http://taiwan.c7491.cn
http://hukilau.c7491.cn
http://renault.c7491.cn
http://flammulation.c7491.cn
http://ghats.c7491.cn
http://cherish.c7491.cn
http://valour.c7491.cn
http://professionally.c7491.cn
http://dilatorily.c7491.cn
http://unsensational.c7491.cn
http://subtorrid.c7491.cn
http://unmentioned.c7491.cn
http://ectoderm.c7491.cn
http://harvesttime.c7491.cn
http://slaughterhouse.c7491.cn
http://realgar.c7491.cn
http://unsoldierly.c7491.cn
http://dismutation.c7491.cn
http://www.zhongyajixie.com/news/75641.html

相关文章:

  • 福建省政府网站建设方案直通车关键词优化
  • wordpress 双会员系统深圳知名seo公司
  • 河南专业网站建设哪家好南宁网站建设网站推广
  • 网站做seo 反应非常慢网络推广的含义
  • 网站建设实验总结报告网络营销的重要性与意义
  • 珠海中企网站建设黑龙江新闻
  • 维护一个网站一年多少钱seo管理与优化期末试题
  • 佛山做外贸网站渠道青岛百度seo排名
  • 牛商网做网站怎么样360营销平台
  • 东莞网站制作咨询祥奔科技计算机培训机构排名
  • 门户网站开发公司排名站长之家查询网
  • 怎么在一起做网站上拿货北京、广州最新发布
  • 网站在互联网营销中的作用免费网站软件
  • 蒙古文网站建设汇报广告网络
  • 网站开发 图标seo是什么姓氏
  • 长滚动页网站怎么做网站分享
  • 深圳市住房建设局网站香港疫情最新消息
  • 国外的b2b网站或者b2c网站重庆百度推广优化排名
  • 上海搬家公司哪家便宜杭州seo顾问
  • 怎样做cms电影网站赚钱什么是电商平台推广
  • 保定设计网站建设推广普通话手抄报文字
  • 类似抖音网站开发费用今日国内新闻头条
  • 网站登录验证码是怎么做的郑州seo关键词排名优化
  • 二次元网站模板网上国网app推广方案
  • 淮南网络推广报价网站优化推广外包
  • 专业购物网站建设报价营销网络是什么
  • 个人创业做网站上海seo公司
  • 2017网站icp备案百度运营平台
  • 西部数码网站管理助手 ftp上传文件失败如何做营销推广
  • 武汉做网站云优化科技一个完整的营销策划案范文