Algorithm exercise 38 --- Blue Bridge Cup 2018 provincial competition "global warming"

Posted by dickey on Sat, 05 Mar 2022 02:54:19 +0100

preface

Blue Bridge Cup 2018 provincial competition, programming problem (C + +)

This topic examines the application of DFS algorithm

1, Title Description

You have a picture of NxN pixels of a certain sea area, "." represents the ocean, "#" represents the land, as shown below:

.......

.##....

.##....

....##.

..####.

...###.

.......

Among them, a piece of land connected in the four directions of "up, down, left and right" forms an island. For example, there are two islands in the above picture.

As the sea level rises due to global warming, scientists predict that a pixel of the edge of the island will be submerged by the sea in the coming decades. Specifically, if a land pixel is adjacent to the ocean (there is an ocean in the four adjacent pixels above, below, left and right), it will be submerged.

For example, the sea area in the above figure will look like the following in the future:

.......

.......

.......

.......

....#..

.......

.......

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

Enter description

The first line contains an integer N (1 ≤ N ≤ 1000).

The following n rows and N columns represent a sea area photo.

The picture ensures that the pixels in row 1, column 1, row N and column n are oceans

Output an integer to represent the answer.

Sample input and output

Examples

input

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

output

1

Operational limits

  • Maximum running time: 1s
  • Maximum operating memory: 256M

2, Train of thought

This question is divided into two parts:

The first part is to let all the oceans submerge the land that can be submerged (special mark of submerged land)

The second part is to call dfs to traverse the submerged land or non submerged land on the basis of traversal. If no non submerged land is found after dfs traversal, the island is considered to be completely submerged, ans+1

Therefore, I have written two DFS functions. One is used to realize the process of seawater rise in the first part, and the other function DFS2 () is used to realize the operation in the second part. In the second DFS, flag is used as a mark. If you encounter land that is not submerged, mark it to show that the island is not completely submerged and is not included in the answer.

Construction of DFS function if you are interested, you can see my previous blog: (2 messages) algorithm exercise 7 - BFS and DFS exercise_ Yang Da Xiong's code World blog - CSDN blog , introduces the most basic DFS function construction.

3, Specific code

#include<bits/stdc++.h>
using namespace std;
char juzhen[1005][1005];
int ans=0;
int n;
int dir[4][2]={{0,-1},{1,0},{0,1},{-1,0}};  //For left, down, right, up
int flag=0;
struct point
{
	int x;
	int y;	
};
bool check(int x,int y)
{
	if(x>=1&&x<=n&&y>=1&&y<=n)
	{
		return true;
	}
	return false;
}
void dfs(point a)
{
	juzhen[a.x][a.y]='*';  //Prove to be traversed
	point temp=a;
	for(int i=0;i<4;i++)  //They are left, down, right and up
	{
		temp.x=a.x+dir[i][0];
		temp.y=a.y+dir[i][1];
		if(check(temp.x,temp.y)&&juzhen[temp.x][temp.y]=='.')   //It's the ocean
		{
			dfs(temp);
		}
		else if(check(temp.x,temp.y)&&juzhen[temp.x][temp.y]=='#')
		{
			juzhen[temp.x][temp.y]='@';  //Mark after inundation is set to@
		}
	}
	return ;
}

void dfs2(point a)
{
	juzhen[a.x][a.y]='$';  //Explored
	point temp=a;
	for(int i=0;i<4;i++)  //They are left, down, right and up
	{
		temp.x=a.x+dir[i][0];
		temp.y=a.y+dir[i][1];
		if(check(temp.x,temp.y)&&juzhen[temp.x][temp.y]=='@')   //Submerged land
		{
			dfs2(temp);
		}
		else if(check(temp.x,temp.y)&&juzhen[temp.x][temp.y]=='#'/ / land that has not been completely submerged should be marked here
		{
			flag=1;
			dfs2(temp);
		}
	}
	return ;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cin>>juzhen[i][j];
		}
	}
	point start;
	start.x=1;
	start.y=1;
	dfs(start);  //Rise first, and then count the number of submerged islands
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			flag=0;
			if(juzhen[i][j]=='#')
			{
				flag=1;
				start.x=i;
				start.y=j;
				dfs2(start);
			}
			else if(juzhen[i][j]=='@')
			{
				start.x=i;
				start.y=j;
				dfs2(start);
				if(flag==0)  //Completely submerged
				{
					ans++;
				}
			}
		}
	}
	cout<<ans<<endl;
	return 0;	
}

Topics: Algorithm