Global warming-BFS+ simulation

My level is limited and I am only for learning. If I find any mistakes, I would like to thank you for pointing out.

Topic link: Portal (click me)

You have a picture of a certain sea area with N×N pixels, "." means ocean and "#" means land, as shown below:

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

Among them, a piece of land connected in four directions of "up, down, left, and right" forms an island. For example, there are 2 islands in the picture above.
As global warming has caused the sea to rise, scientists predict that in the next few decades, a pixel area on the edge of the island will be submerged by sea water.
Specifically, if a land pixel is adjacent to the ocean (there is an ocean among the four adjacent pixels up, down, left, and right), it will be submerged.
For example, the sea area in the above picture will become as follows in the future:

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

Please calculate: According to the prediction of scientists, how many islands in the photo will be completely submerged.

Ideas

1. Wide search (or deep search) how many islands are there, and label these islands
2. Simulate seawater inundation
3. Count

AC code

#include<bits/stdc++.h>
using namespace std;
#define MAXN 1200

int n,flag=1,ans;
char mp[MAXN][MAXN];
int vis[MAXN][MAXN],cnt[MAXN*MAXN];
int xx[4]={
    
    1,-1,0,0},yy[4]={
    
    0,0,-1,1};

typedef struct {
    
    
    int x,y;
}island;

void bfs(island node)//board first search
{
    
    
    queue<island>q;
    q.push(node);vis[node.x][node.y]=flag;//signed the island of order
    while(q.size()!=0){
    
    
          island t=q.front();q.pop();
          for(int i=0;i<4;i++){
    
    
             island z;
             z.x = t.x+xx[i],z.y=t.y+yy[i];
             if(z.x<0||z.x>=n||z.y<0||z.y>=n||vis[z.x][z.y]!=0||mp[z.x][z.y]=='.')continue;
             vis[z.x][z.y]=flag;
             q.push(z);
          }
    }
    flag++;
}

int main()
{
    
    
    cin>>n;
    for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>mp[i][j];
    island node;
    for(int i=0;i<n;i++){
    
    
        for(int j=0;j<n;j++){
    
    
            if(mp[i][j]=='#'&&vis[i][j]==0){
    
    node.x=i,node.y=j;bfs(node);}
        }
    }
    for(int i=0;i<n;i++){
    
      //the island was submerged in the sea
        for(int j=0;j<n;j++){
    
    
            if(mp[i][j]=='.'){
    
    
                for(int k=0;k<4;k++){
    
    
                    int tx,ty;
                    tx=i+xx[k];ty=j+yy[k];
                    if(tx<0||tx>=n||ty<0||ty>=n||mp[tx][ty]=='.')continue;
                    mp[tx][ty]='s';
                }
            }
        }
     }
    //the remain island
    for(int i=0;i<n;i++)for(int j=0;j<n;j++)if(mp[i][j]=='#')cnt[vis[i][j]]++;
    for(int i=0;i<MAXN*MAXN;i++)if(cnt[i])ans++;
    return cout<<flag-ans-1<<endl,0;
}

After writing the code, I looked at the code and found that the first two steps can be combined into one step. While BFS, the flood count can be simulated to optimize the time.

Guess you like

Origin blog.csdn.net/weixin_43615816/article/details/114776950