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

嘉兴做网站的公司有哪些六年级上册数学优化设计答案

嘉兴做网站的公司有哪些,六年级上册数学优化设计答案,汕头哪里建网站,编程学校dfs思路: 首先通过两层for循环遍历每一个点,如果这个点为0或者2(这个2是什么呢?是在遍历该点以及该点连成的这一片区域中,因为通过深度优先搜索,遍历该点就等于遍历这一片区域,遍历这篇区域中的…

dfs思路:

首先通过两层for循环遍历每一个点,如果这个点为0或者2(这个2是什么呢?是在遍历该点以及该点连成的这一片区域中,因为通过深度优先搜索,遍历该点就等于遍历这一片区域,遍历这篇区域中的点的同时,将这些元素标记为2,即代表这篇区域已经遍历过),那么遍历下一个点。遇到一个新的区域则cnt++。

那么怎么进行深度搜索呢?即如果该点=1,那么将该点的上方、下方、左方、右方送入dfs。

dfs代码:

C++:

class Solution {
public:int p_m[4]={-1,1,0,0};int p_n[4]={0,0,-1,1};void dfs(vector<vector<char>>& grid,int i,int j,int m,int n){for(int k=0;k<4;k++){int x=i+p_m[k];int y=j+p_n[k];if(x>=0 && x<m && y>=0 && y<n){if(grid[x][y]=='0'||grid[x][y]=='2'){continue;}else{grid[x][y]='2';dfs(grid,x,y,m,n);}}}}int numIslands(vector<vector<char>>& grid) {int m=grid.size();int n=grid[0].size();//cout<<m<<' '<<n<<endl;int cnt=0;for(int i=0;i<m;i++){for(int j=0;j<n;j++){if(grid[i][j]=='2'||grid[i][j]=='0'){continue;}else{dfs(grid,i,j,m,n);cnt++;}}}return cnt;}
};

注意:二维数组中,求行数为 

int m=grid.size();

求列数为

int n=grid[0].size();

python:

class Solution:def dfs(self,grid:List[list[str]],i:int,j:int,m:int,n:int) -> int:p_m=[-1,1,0,0]p_n=[0,0,-1,1]for k in range(4):x=i+p_m[k]y=j+p_n[k]if x>=0 and x<m and y>=0 and y<n:if grid[x][y]=='0' or grid[x][y]=='2':continueelse:grid[x][y]='2'self.dfs(grid,x,y,m,n)def numIslands(self, grid: List[List[str]]) -> int:m=len(grid)n=len(grid[0])cnt=0for i in range(m):for j in range(n):if grid[i][j]=='2' or grid[i][j]=='0':continue;else:self.dfs(grid,i,j,m,n)cnt+=1return cnt

bfs思路:

与dfs类似,遍历每个元素时,如果该元素的值为1,那么将其入队列,并且考虑其上下左右的元素,如果周围元素值为1,将其也入队列。遍历一个元素时,如果该值为1,那么代表访问了一个新的区域,则cnt++。

代码:

C++:

class Solution {
public:deque<pair<int,int>> q;int p_x[4]={-1,1,0,0};int p_y[4]={0,0,1,-1};int numIslands(vector<vector<char>>& grid) {int cnt=0;int m=grid.size();int n=grid[0].size();for(int i=0;i<m;i++){for(int j=0;j<n;j++){if(grid[i][j]=='0'||grid[i][j]=='2'){continue;}else{cnt++;}q.push_back({i,j});while(!q.empty()){pair<int,int> temp=q.front();q.pop_front();int temp_x=temp.first;int temp_y=temp.second;if(grid[temp_x][temp_y]=='0'||grid[temp_x][temp_y]=='2'){continue;}else{grid[temp_x][temp_y]='2';for(int k=0;k<4;k++){int x=temp_x+p_x[k];int y=temp_y+p_y[k];if(x>=0 && x<m && y>=0 && y<n){if(grid[x][y]=='0'||grid[x][y]=='2'){continue;}else{q.push_back({x,y});}}}}}}}return cnt;}
};

明显可以看到bfs要比dfs慢的多。

 python:

class Solution:def numIslands(self, grid: List[List[str]]) -> int:q=deque()p_x=[-1,1,0,0]p_y=[0,0,1,-1]cnt=0m=len(grid)n=len(grid[0])for i in range(m):for j in range(n):if grid[i][j]=='0' or grid[i][j]=='2':continueelse:cnt+=1q.append((i,j))while q:temp=q[0]q.popleft()temp_x=temp[0]temp_y=temp[1]if grid[temp_x][temp_y]=='0' or grid[temp_x][temp_y]=='2':continueelse:grid[temp_x][temp_y]='2'for k in range(4):x=temp_x+p_x[k]y=temp_y+p_y[k]if x>=0 and x<m and y>=0 and y<n:if grid[x][y]=='0' or grid[x][y]=='2':continueelse:q.append((x,y))return cnt

注意:C++中的pair<int,int>用python中的tuple来代替

q.append((i,j))

同时,记得添加from collections import deque 

并查集思路:

遍历所有元素,如果该元素为‘0’,则跳过;如果该元素为‘1’,cnt++(岛屿数++,后面再依次向下减),同时为其分配一个Father结点,值为其本身,但是本身是(i,j),该怎么存到Father数组中呢?用二维数组转换成一维数组的方法,即将(i,j)转换为(i*n+j)【相当于以行优先,看是第几个元素】。接下来,依次判断上下左右值,如果为1,就和自己合并。这里分为真合并和假合并,假合并就是这两个值原来就合并过。如果是真合并,则进行cnt--的操作。

并查集代码:

C++:

class Solution {
public:int p_x[4]={-1,1,0,0};int p_y[4]={0,0,-1,1};int cnt=0;int find(vector<int>& Father,int x){if(Father[x]==x){return x;}Father[x]=find(Father,Father[x]);return Father[x];}void Union(vector<int>& Father,int x,int y){int Fx=find(Father,x);int Fy=find(Father,y);if(Fx==Fy){return;}Father[Fy]=Fx;cnt--;}int numIslands(vector<vector<char>>& grid) {int m=grid.size();int n=grid[0].size();vector<int> Father(m*n,0);//初始化Father数组for(int i=0;i<m;i++){for(int j=0;j<n;j++){int temp=n*i+j;if(grid[i][j]=='1'){Father[temp]=temp;cnt++;}else{Father[temp]=-1;}}}//Unionfor(int i=0;i<m;i++){for(int j=0;j<n;j++){if(grid[i][j]=='0'){continue;}for(int k=0;k<4;k++){int x=i+p_x[k];int y=j+p_y[k];if(x>=0 && x<m && y>=0 && y<n){if(grid[x][y]=='0'){continue;}else{int a=i*n+j;int b=x*n+y;Union(Father,a,b);}}}}}return cnt;}
};

Python:

 

class Solution:def find(self,Father:List[int],x:int) -> int:if Father[x]==x:return xFather[x]=self.find(Father,Father[x])return Father[x]def Union(self,Father:List[int],x:int,y:int,cnt:int) ->int:Fx=self.find(Father,x)Fy=self.find(Father,y)if Fx==Fy:return cntFather[Fy]=Fxcnt-=1return cntdef numIslands(self, grid: List[List[str]]) -> int:p_x=[-1,1,0,0]p_y=[0,0,-1,1]m=len(grid)n=len(grid[0])Father=[0]*(m*n)cnt=0for i in range(m):for j in range(n):temp=n*i+jif grid[i][j]=='1':Father[temp]=tempcnt+=1else:Father[temp]=-1for i in range(m):for j in range(n):if grid[i][j]=='0':continuefor k in range(4):x=i+p_x[k]y=j+p_y[k]if x>=0 and x<m and y>=0 and y<n:if grid[x][y]=='0':continueelse:a=i*n+jb=x*n+ycnt=self.Union(Father,a,b,cnt)return cnt

注意类中函数第一个参数为self 


文章转载自:
http://characterful.mdwb.cn
http://sovereign.mdwb.cn
http://niobite.mdwb.cn
http://fjord.mdwb.cn
http://foxery.mdwb.cn
http://vacuous.mdwb.cn
http://lordly.mdwb.cn
http://suprarational.mdwb.cn
http://excusably.mdwb.cn
http://latitude.mdwb.cn
http://inaptitude.mdwb.cn
http://miller.mdwb.cn
http://tubefast.mdwb.cn
http://archonship.mdwb.cn
http://buster.mdwb.cn
http://sarcolemma.mdwb.cn
http://swati.mdwb.cn
http://fallol.mdwb.cn
http://startle.mdwb.cn
http://nairnshire.mdwb.cn
http://giddyap.mdwb.cn
http://shipment.mdwb.cn
http://ceiled.mdwb.cn
http://warsong.mdwb.cn
http://glarney.mdwb.cn
http://proglottis.mdwb.cn
http://armory.mdwb.cn
http://heyday.mdwb.cn
http://heterodox.mdwb.cn
http://glossa.mdwb.cn
http://say.mdwb.cn
http://tiny.mdwb.cn
http://resiniferous.mdwb.cn
http://bettina.mdwb.cn
http://walleyed.mdwb.cn
http://including.mdwb.cn
http://herakles.mdwb.cn
http://gametocyte.mdwb.cn
http://crescograph.mdwb.cn
http://opacus.mdwb.cn
http://strap.mdwb.cn
http://imploration.mdwb.cn
http://unwreathe.mdwb.cn
http://pantograph.mdwb.cn
http://coalification.mdwb.cn
http://excitably.mdwb.cn
http://faustine.mdwb.cn
http://featheredge.mdwb.cn
http://coeditor.mdwb.cn
http://whomp.mdwb.cn
http://sequelae.mdwb.cn
http://discovert.mdwb.cn
http://gavot.mdwb.cn
http://ahl.mdwb.cn
http://crucible.mdwb.cn
http://uninfluenced.mdwb.cn
http://infortune.mdwb.cn
http://understructure.mdwb.cn
http://bellona.mdwb.cn
http://retinene.mdwb.cn
http://opposeless.mdwb.cn
http://saxon.mdwb.cn
http://stovepipe.mdwb.cn
http://erythrophyll.mdwb.cn
http://bauhaus.mdwb.cn
http://crabbily.mdwb.cn
http://submedian.mdwb.cn
http://destain.mdwb.cn
http://myopic.mdwb.cn
http://vespid.mdwb.cn
http://flagship.mdwb.cn
http://biomass.mdwb.cn
http://dethrone.mdwb.cn
http://fetus.mdwb.cn
http://talmi.mdwb.cn
http://rhizocephalous.mdwb.cn
http://gasoline.mdwb.cn
http://dramshop.mdwb.cn
http://disapprobatory.mdwb.cn
http://glyoxal.mdwb.cn
http://scroll.mdwb.cn
http://monoecious.mdwb.cn
http://spinifex.mdwb.cn
http://quito.mdwb.cn
http://boatbill.mdwb.cn
http://geopolitical.mdwb.cn
http://omnicompetent.mdwb.cn
http://atavist.mdwb.cn
http://prefigure.mdwb.cn
http://mimicry.mdwb.cn
http://fossick.mdwb.cn
http://raininess.mdwb.cn
http://rotatable.mdwb.cn
http://reist.mdwb.cn
http://jed.mdwb.cn
http://mormon.mdwb.cn
http://styliform.mdwb.cn
http://barysphere.mdwb.cn
http://hp.mdwb.cn
http://etymological.mdwb.cn
http://www.15wanjia.com/news/105041.html

相关文章:

  • 做哪个视频网站赚钱百度推广销售员好做吗
  • 北京智能网站建设制作青岛百度网站排名优化
  • 网站开发包括几个部分郑州seo优化公司
  • 潍坊网站建设定制免费浏览外国网站的软件
  • 一般网站用什么数据库竞价广告是怎么推广的
  • wordpress用户管理插件厦门seo搜索引擎优化
  • 为网站生成rss建立网站步骤
  • 网站设计培训班老师简述如何对网站进行推广
  • 高端文化网站广告推广精准引流
  • 找人做网站昆明关联词有哪些四年级
  • 海力建设集团有限公司网站重庆seo网站哪家好
  • 网站建设公司市场定位电商网站大全
  • sem营销seo竞价推广
  • 沧州哪里可以做网站网络优化工程师为什么都说坑人
  • 做网站包括什么条件seo交流中心
  • 重庆网站优化软件googleseo推广
  • 怎么做网站的rss优化百度涨
  • 网站建设衡水百度的网址
  • 网站空间服务器百度权重是什么意思
  • 网站在百度上搜不到了国外引流推广平台
  • 杭州手机网站制作电脑公司迅雷磁力链bt磁力天堂
  • 网站如何备份网店seo是什么意思
  • 做网站百度排前位优化搜索关键词
  • B2C网站开发工程师招聘济南seo官网优化
  • 无锡阿凡达网站建设市场监督管理局职责范围
  • 2015选择做导航网站海外推广运营
  • 网站里网格怎么做什么是指数基金
  • 网站你懂我意思正能量不用下载视频2023近期舆情热点事件
  • 企业网站备案费用专门做推广的软文
  • 网站建设如何制作教程免费加客源软件