The number of closed statistics islands (DFS): Leetcode 1254

The number of closed islands of statistics


Problem Description:
There is a two-dimensional matrix grid, each location either land (symbol 0) is either water (1 mark).

We proceed from one land mass, each can up under the direction of the adjacent area of ​​about four to go, and went to all land areas, which we will call an "island."

If an island completely surrounded by water, that is, all the adjacent land areas around the edge of the waters are up and down, then we will call it "closed the island."

Please return the number of closed islands.

Example 1:

Input: grid = [[1,1,1,1,1,1,1,0], [1,0,0,0,0,1,1,0], [1,0,1,0, 1,1,1,0], [1,0,0,0,0,1,0,1], [1,1,1,1,1,1,1,0]]
output: 2
explanation:
island island gray area is closed, because the island is completely surrounded by water (i.e., an area enclosed).

Example 2:

Input: Grid = [[1,1,1,1,1,1,1],
[1,0,0,0,0,0,1],
[1,0,1,1,1,0, 1],
[1,0,1,0,1,0,1],
[1,0,1,1,1,0,1],
[1,0,0,0,0,0,1] ,
[1,1,1,1,1,1,1]]
output: 2

Problem-solving ideas:

  First, we need to traverse the entire island, if the current position is 0 (land), we need to determine whether the location of the island closed the island. dfs four directions up and down, if all four directions are land Returns true, the total number of +1. (Note: dfs when the need to hit land marked as waters, so that next time to avoid double counting when traversing a)
  of this title does not need to go back, because it is seeking the same region, does not need to calculate the path.

Graphic:

From the top left corner for the first time entered the dfs

(1) becomes 1, and the four directions dfs

(2) The first returns 0, 0 and the land turned into waters 1.

(3) when entering waters surrounded by land, returns 1

Here Insert Picture Description

code show as below:

#include<iostream>
#include<vector>
using namespace std;
	int dfs(int i,int j,vector<vector<int> >& grid){
		if(i<0||j<0||i>=grid.size()||j>=grid[i].size()){//不满足条件 
			return 0;
		}
		if(grid[i][j]==1){//如果碰到水域返回1 
			return 1;
		}
		grid[i][j]=1;//如果碰到陆地则标记为水域 
		int x1= dfs(i+1,j,grid);
		int x2= dfs(i-1,j,grid);
		int y1= dfs(i,j+1,grid);
		int y2= dfs(i,j-1,grid);
		//四个方向全为水域返回1 
		if(x1&&x2&&y1&&y2){
			return 1;
		}
		return 0;
	}
	int closedIsland(vector<vector<int> >& grid) {
		int sum=0;
		for(int i=1;i<grid.size()-1;i++){//边缘部分肯定不是岛屿,所以从1开始 
			for(int j=1;j<grid[i].size()-1;j++){
				if(grid[i][j]==0){ //如果是陆地 
					if(dfs(i,j,grid)){
						sum++;
						}
					}
				}
		} 
			return sum;
    }
    
int main(){
	vector<vector<int> > grid={{0,0,1,1,0,1,0,0,1,0},{1,1,0,1,1,0,1,1,1,0},{1,0,1,1,1,0,0,1,1,0},{0,1,1,0,0,0,0,1,0,1},{0,0,0,0,0,0,1,1,1,0},{0,1,0,1,0,1,0,1,1,1},{1,0,1,0,1,1,0,0,0,1},{1,1,1,1,1,1,0,0,0,0},{1,1,1,0,0,1,0,1,0,1},{1,1,1,0,1,1,0,1,1,0}};
	cout<<closedIsland(grid);
	return 0;
}
	
Released six original articles · won praise 4 · Views 7783

Guess you like

Origin blog.csdn.net/Milan_1in/article/details/104983603