# Group B of 2018 blue bridge provincial competition - global warming

There is such a question when I recently brush the Blue Bridge Cup:

## The main idea of the topic

```You have a picture of NxN pixels in a certain sea area, where "... Represents the ocean and" ා "represents the land, as shown below:
...
.##...
.##...
...##.
...####.
...###.
...

Among them, an island is composed of a piece of land connected in four directions of "up, down, left and right". For example, there are two islands in the picture above.
As global warming causes sea levels to rise, scientists predict that in the next few decades, a pixel of the edge of the island will be submerged by the sea. Specifically, if a land pixel is adjacent to the ocean (there are oceans in the four adjacent pixels), it will be submerged.
For example, the sea area in the figure above will become the following in the future:
...
...
...
...
...#...
...
...
Please calculate: according to scientists' prediction, how many islands in the picture 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 picture of the sea area.
The photo guarantees that the pixels in the first row, the first column, the nth row and the nth column are all oceans.
```

## Output format

```An integer represents the answer.
```

```7
...
.##...
.##...
...##.
...####.
...###.
...
```

## sample output

```1
```

This problem is a simple DFS type problem, which is not worth mentioning for algorithm God. The simplest idea is:

```	1. Count the number of islands first

2. Simulate the process of inundation

3. Count the number of islands again
```

The following code is the solution when N=5, and I didn't encapsulate the function. The code is quite jumbled, mainly looking at the idea:

```#include<bits/stdc++.h>
using namespace std;

int a={0};//Initialize ocean and land
int vis={0};//Tag array, has the record been searched
int coun=0;//How many islands are there at the beginning
int coun1=0;//Count how many islands there are after the inundation
void bfs(int m,int n)
{
if(vis[m][n]==0)
{
vis[m][n]=1;
if(m-1>=0&&a[m-1][n]==1&&vis[m-1][n]==0)
{
bfs(m-1,n);
}
if(m+1<=4&&a[m+1][n]==1&&vis[m+1][n]==0)
{
bfs(m+1,n);
}
if(n-1>=0&&a[m][n-1]==1&&vis[m][n-1]==0)
{
bfs(m,n-1);
}
if(n+1<=4&&a[m][n+1]==1&&vis[m][n+1]==0)
{
bfs(m,n+1);
}
}

}
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
scanf("%d",&a[i][j]);
}
}
//Count the number of islands at the beginning
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i][j]==1&&vis[i][j]==0)
{
bfs(i,j);
coun++;
}
}
}

//Here is the initialization of vis [] [] array. Note: This is a two-dimensional array, which cannot be initialized with memset
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
vis[i][j]=0;
}
}
//Simulate the process of being submerged
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i][j]==0&&vis[i][j]==0)
{
if(i-1>=0&&a[i-1][j]==1)
{
a[i-1][j]=0;
vis[i-1][j]=1;
}
if(i+1>=0&&a[i+1][j]==1)
{
a[i+1][j]=0;
vis[i+1][j]=1;
}
if(j-1>=0&&a[i][j-1]==1)
{
a[i][j-1]=0;
vis[i][j-1]=1;
}
if(j+1>=0&&a[i][j+1]==1)
{
a[i][j+1]=0;
vis[i][j+1]=1;
}
}
}
}
//Initialize the vis [] [] array again
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
vis[i][j]=0;
}
}
//Count the number of islands after inundation
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i][j]==1&&vis[i][j]==0)
{
bfs(i,j);
coun1++;
}
}
}

printf("%d\n",coun-coun1);
return 0;
}

```

Posted on Tue, 03 Dec 2019 00:16:40 -0500 by unlishema.wolf