200. Number of Islands(重要)

本文介绍了一个算法问题:在一个由'1'(陆地)和'0'(水域)组成的二维网格中,通过深度优先搜索(DFS)来计算岛屿的数量。岛屿被定义为水平或垂直相邻的陆地块集合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

11110
11010
11000
00000

Answer: 1

Example 2:

11000
11000
00100
00011

Answer: 3

class Solution {
public:
	int numIslands(vector<vector<char>>& grid) {
		int row=grid.size();
		if(row==0) return 0;
		int col=grid[0].size();
		if(col==0) return 0;
		int res=0;
		for(int i=0;i<row;i++){
			for(int j=0;j<col;j++){
				if(grid[i][j]=='1'){
					dfs(grid,i,j);
					res++;
				}
			}
		}
		return res;
	}
private:
	void dfs(vector<vector<char>>& grid,int x,int y){
		int row=grid.size(),col=grid[0].size();
		grid[x][y]='0';
		if(x>0&&grid[x-1][y]=='1'){
			dfs(grid,x-1,y);
		}
		if(x+1<row&&grid[x+1][y]=='1'){
			dfs(grid,x+1,y);
		}
		if(y>0&&grid[x][y-1]=='1'){
			dfs(grid,x,y-1);
		}
		if(y+1<col&&grid[x][y+1]=='1'){
			dfs(grid,x,y+1);
		}
	}
};



学习下列洪水算法(注意FLA指的是洪水算法而非蛙跳算法),并将其改进,用于解决IEEE33节点的孤岛划分问题:function [Destination_fitness,Destination_position,Convergence_curve]=FLA(nPop,MaxIt,lb,ub,nVar,fobj) CostFunction = @(x) fobj(x); tic time1=0; VarSize = [1 nVar]; VarMin = lb; % Lower Bound of Variables VarMax = ub ; % Upper Bound of Variables NFEs=0; % Number of fitness evaluations Ne=5; % Number of weak population that will be replaced it=0; % Iteration counter %% Initialization POP.Position=[]; % Initialize Population Array POP.Cost=[]; Val=0*unifrnd(VarMin,VarMax,VarSize); BestSol.Cost=inf; % Initialize Best Solution Ever Found pop=repmat(POP,nPop,1); % Initialize Population Array %% Create Initial Population for i=1:nPop pop(i).Position=unifrnd(VarMin,VarMax,VarSize); %%Eq.(4) pop(i).Cost=CostFunction(pop(i).Position); Val(i,:)=0*unifrnd(VarMin,VarMax,VarSize); if pop(i).Cost<=BestSol.Cost BestSol=pop(i); end end while NFEs<nPopMaxIt it=it+1; PK=(((((MaxIt(it^2))+1)^0.5)+((1/((MaxIt/4)it))(log((((MaxIt*(it^2))+1)^0.5)+((MaxIt/4)it)))))^((-2/3)))(1.2/it); %%Eq.(5) for i=1:nPop %% Implementing phase I % Sorting the population [sortpop, ~]=sort([pop.Cost]); Pe_i=((pop(i).Cost-sortpop(1))/(sortpop(end)-sortpop(1)))^(2); %%Eq.(7) A=randperm(nPop); A(A==i)=[]; a=A(1); if rand>(rand+Pe_i) Val(i,:)=((PK^randn)/it)*unifrnd(VarMin,VarMax,VarSize); newsol.Position =pop(i).Position+Val(i,:); %%Eq.(6) newsol.Position=max(newsol.Position,VarMin); newsol.Position=min(newsol.Position,VarMax); else newsol.Position=BestSol.Position+rand(VarSize).*(pop(a).Position-pop(i).Position); newsol.Position=max(newsol.Position,VarMin); newsol.Position=min(newsol.Position,VarMax); end newsol.Cost=CostFunction(newsol.Position); NFEs=NFEs+1; if newsol.Cost<=pop(i).Cost pop(i)=newsol; end if newsol.Cost<=BestSol.Cost BestSol=newsol; end end %% Implementing phase II Pt=abs(sin(rand/(it))); %%Eq.(8) if rand<Pt [~, Sortorder]=sort([pop.Cost]); pop=pop(Sortorder); pop=pop(1:nPop-Ne); newpop=repmat(POP,Ne,1); for ie=1:Ne newpop(ie).Position=BestSol.Position+(rand)*(unifrnd(VarMin,VarMax,VarSize)); %%Eq.(9) newpop(ie).Cost=CostFunction(newpop(ie).Position); NFEs=NFEs+1; Val(nPop+ie,:)=0*unifrnd(VarMin,VarMax,VarSize); if newpop(ie).Cost<=BestSol.Cost BestSol=newpop(ie); end end pop=[pop newpop]; end % Store Record for Current Iteration Convergence_curve(it) =BestSol.Cost; end %% Results Destination_fitness=pop(1).Cost; Destination_position=pop(1).Position; %% Plotting Convergence_curve % % % semilogy(Convergence_curve, ‘LineWidth’, 1.5); % % % % % % % % % % % Set ylabel % % % ylabel(‘Log(Objective Function)); % % % % % % % % % % % Set xlabel % % % xlabel(‘Iteration’); % % % % % % % % % % % Set the y-axis to semilog scale % % % set(gca, ‘YScale’, ‘log’); % % % set(gca, ‘FontName’, ‘Times New Roman’); % % % % % % % % % % % Display the box around the axes % % % box on; end
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值