Global Warming of the 9th Blue Bridge Cup



Title: Global Warming


You have a NxN pixel photo of a certain sea area, "." means ocean and "#" means 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 is an ocean 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】

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


[Example of output]
1  






Resource convention:
Peak memory consumption (including virtual machine) <256M
CPU consumption <1000ms




Please output strictly as required, and do not superfluous to print redundant content like: "Please enter...".


Note:
the main function needs to return 0;
only use the ANSI C/ANSI C++ standard;
. Calls do not depend on special functions of the compilation environment or operating system.
All dependent functions must be explicitly #include <XXX> in the source file
. Common header files cannot be omitted through project settings.


When submitting your program, take care to select the desired language type and compiler type.



The idea is to search deeply and mark and count the original number of islands and after being submerged


#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
using namespace std;
bool flag;//Mark whether the island is sinking
char a[1010][1010];//Map save
int cnt=0;//Count the number of surrounding #
int n;//
int d[4][2]= {1,0,-1,0,0,1,0,-1};//Array of enumeration directions
int ans=0;//Number of islands after being submerged
int res_ans=0;//Number of submerged islands
void dfs(int x,int y)//backtrack
{
	if(a[x][y]!='#')//If this point is marked as the content of the island that has been explored or the ocean, then return
		return ;
	if(x>=n||x<0||y>=n||y<0)//If it is out of bounds, it will also return
		return ;
	if(flag==false)//If it is not yet determined that it will not be submerged, that is, it is currently submerged
	{cnt=0;//Statistics of the current point coordinates x, y of the island are the four points of the upper, lower, left and right are all '#', if this is the case, then we can be sure that the island will not be submerged , let's call such a point an unsubmersible point, then as long as an island has such a point, it will not be submerged
		for(int i=0; i<4; i++)//enumerate four directions
		{
			int tx=x+d[i][0];
			int ty = y + d [i] [1];
			if(tx<n&&tx>=0&&ty<n&&ty>=0&&a[tx][ty]!='.')//Assuming that the points around the enumeration will not be out of bounds and are not oceans, then the number of cnt +1
			{
				cnt++;
			}
		}
		if(cnt==4)//If the surrounding points are like this, then this point is an unsubmersible point
		{
			ans++;//The last point + will not be submerged!
			flag=true;//The flag of this island has been determined, the current island cannot be submerged, so when you go back to other points on the island, you don't need to check it again, because an island has such a non-submersible point is enough
		}
	}
	a[x][y]='*';//The content of the island that has been explored is marked as '*',
	for(int i=0; i<4; i++)
	{
		int tx=x+d[i][0];
		int ty = y + d [i] [1];
			dfs(tx,ty);//For all points of the island to backtrack in four directions
	}
}
intmain()
{
	cin>>n;
	for(int i=0; i<n; i++)
	{
		scanf("%s",a[i]);
	}//Input data
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{// Traverse all the coordinates of the map
			if(a[i][j]=='#')//If the same island is found, it will be marked as '*', and will not enter the backtracking, only '#' means it is not searched corner of the island
			{
				res_ans++;//Then add 1 to the number of islands
				flag=false;//Assume it will be submerged
				dfs(i,j);//Backtrack to mark the coordinates of all points on this island, and if it will not be completely submerged, then mark
			}
		}
	}
	cout<<res_ans-ans<<endl;//Output the answer
}

Guess you like

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