global warming (bfs dfs)

You have a NxN pixel photo of a sea area, "." for ocean and "#" for land, like this:

.......
.##....
.##....
....##.
..####.
...###.
.......

Among them, a piece of land connected in the four directions of "up, down, left and right" forms an island. For example, there are 2 islands in the picture above.
Due to rising sea levels caused by global warming, scientists predict that a pixel of the edge of the island will be submerged by sea water in the next few decades. Specifically, if a land pixel is adjacent to an ocean (there are oceans in four adjacent pixels up, down, left, and right), it will be submerged.

For example, the sea area in the picture above will look like this in the future:

.......
.......
.......
.......
....#..
.......
.......

Please count: According to scientists' predictions, how many islands in the photo will be completely submerged.

【Input format】
The first line contains an integer N. (1 <= N <= 1000)
The following N rows and N columns represent a sea area photo.

The photo guarantees that the pixels in row 1, column 1, row N, and column N are all oceans.

[Output format]
An integer represents the answer.
【Input sample】

7 
.......
.##....
.##....
....##.
..####.
...###.
.......  

【Example of output】

1

Problem solving idea 1:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define N 1000
char mapp[N][N];
int n,vis[N][N],dir[4][2]={
   
   {-1,0},{0,-1},{0,1},{1,0}};
struct point{
	int x,y;
	point(int a,int b){//构造方法 
		x = a;
		y = b;
	}
};
int bfs(int x,int y){
	queue<point> q;
	q.push(point(x,y));
	vis[x][y] = 1; 
	int left = 0;
	while(!q.empty()){
		point p = q.front();
		q.pop();
		int cnt = 0;
		for(int i = 0;i < 4;i++){
			int dx = p.x + dir[i][0];
			int dy = p.y + dir[i][1];
			if( mapp[dx][dy] == '#' && dx >= 0 && dx < n && dy >= 0 && dy < n){	
				cnt++;	
				if(!vis[dx][dy]){
					vis[dx][dy] = 1;
					q.push(point(dx,dy));	
				}
			} 
		}
		if(cnt == 4){
			left++;	
		}
	}
	return left;
}
int main(){
	int i,j,ans = 0;
	cin >> n;
	for(i = 0;i < n;i++){
		for(j = 0;j < n;j++)
			cin >> mapp[i][j];
	}
	memset(vis,0,sizeof(vis));
	for(i = 0;i < n;i++){
		for(j = 0;j < n;j++){
			if(mapp[i][j] == '#' && !vis[i][j]){
				if(!bfs(i,j)){ 
					ans++;
				} 
			}
		}
	}
	cout << ans << endl;
	return 0;
}

How does dfs
determine how many islands there are? If you want to use dfs to judge, do dfs multiple times.
This idea is correct. In fact, we use two for loops to search for the first unvisited #, and just do dfs. How to record each island? Then determine whether the island will be completely submerged. But in fact, in this process, we can judge in the process of finding the island to determine whether there is a point# in the island, and it is surrounded by#

#include<iostream>
#include<cstdio>
using namespace std;
int mp[110][110];
int ans[11000];
bool vis[110][110];
void dfs(int x,int y,int k){
    if(mp[x][y] ==  '.')
        return;
    if(vis[x][y])
        return;
    vis[x][y] = 1;
    if(mp[x - 1][y] == '#'&& mp[x + 1][y] == '#'&&
    	 mp[x][y - 1] == '#' && mp[x][y + 1] == '#'){
    	ans[k]++;
    }    
    dfs(x + 1,y,k);
    dfs(x - 1,y,k);
    dfs(x,y - 1,k);
    dfs(x,y + 1,k);
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i = 1;i <= n;i++){
        getchar();
        for(int j = 1;j <= n;j++){
        	scanf("%c",&mp[i][j]);
        }  
    }
    int len = 0;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= n;j++){
            if(mp[i][j] == '#' && !vis[i][j]){
                dfs(i,j,len);
                len++;
            }
        }
    }
    int sum = 0;
    for(int i = 0;i < len;i++){
        if(ans[i] == 0){
            sum++;        	
        }
    }
    printf("%d\n",sum);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325679692&siteId=291194637