2018 Lanqiao Cup Question I --- Global Warming

global warming

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

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

Among them, a piece of land connected in four directions "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.
[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 picture 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.
【Input sample】

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

[Sample output]

1  

Resource agreement:
Peak memory consumption (including virtual machines) <256M
CPU consumption <1000ms

Idea: Deeply search the islands and then process the submerged area to make a final judgment. It is worth noting that there may be more islands after the seawater has eroded. It is like this example is prone to errors.

I am recording which points the original island is composed of, and see if these points are still alive after erosion.

Code:

#include<stdio.h>
#include<string.h>
#define  SIZE  1001

int dir[4][2]={
    
    //记录上下左右走的数组 
	{
    
    0,1},
	{
    
    0,-1},
	{
    
    1,0},
	{
    
    -1,0}
};

char M[SIZE][SIZE];//存储图的二维矩阵 
int M_mark[SIZE][SIZE];//是否已经被dfs访问过 
int M_del[SIZE][SIZE];//是否该被被侵蚀函数 

void del_mark(int i,int j,int n){
    
    //单次删除标记 
	if(M[i][j]=='#'){
    
    
		if(i==1&&j==1){
    
    //左上角 
			if(M[i+1][j]=='.'||M[i][j+1]=='.')M_del[i][j]=1;
			else M_del[i][j]=0;
		}else if(i==1&&j==n){
    
    //右上角 
			if(M[i][j-1]=='.'||M[i+1][j]=='.')M_del[i][j]=1;
			else M_del[i][j]=0;
		}else if(i==n&&j==1){
    
    //左下角 
			if(M[i-1][j]=='.'||M[i][j+1]=='.')M_del[i][j]=1;
			else M_del[i][j]=0;
		}else if(i==n&&j==n){
    
    //右下角 
			if(M[i-1][j]=='.'||M[i][j-1]=='.')M_del[i][j]=1;
			else M_del[i][j]=0;
		}else if(i==1){
    
    //第一行 
			if(M[i][j-1]=='.'||M[i][j+1]=='.'||M[i+1][j]=='.')M_del[i][j]=1;
			else M_del[i][j]=0;
		}else if(i==n){
    
    //第n行 
			if(M[i][j-1]=='.'||M[i][j+1]=='.'||M[i-1][j]=='.')M_del[i][j]=1;
			else M_del[i][j]=0;
		}else if(j==1){
    
    //第一列 
			if(M[i+1][j]=='.'||M[i-1][j]=='.'||M[i][j+1]=='.')M_del[i][j]=1;
			else M_del[i][j]=0;
		}else if(j==n){
    
    //第n列 
			if(M[i+1][j]=='.'||M[i-1][j]=='.'||M[i][j-1]=='.')M_del[i][j]=1;
			else M_del[i][j]=0;
		}else{
    
    //最一般的情况 
			if(M[i+1][j]=='.'||M[i-1][j]=='.'||M[i][j-1]=='.'||M[i][j+1]=='.')M_del[i][j]=1;
			else M_del[i][j]=0;
		}
	}else{
    
    
		M_del[i][j]=0;
	}
}

void del(int n){
    
    //侵蚀函数 
	memset(M_del,0,sizeof M_del);
	for(int i=1;i<=n;i++){
    
    
		for(int j=1;j<=n;j++){
    
    
			del_mark(i,j,n);
		}
	}
	
	for(int i=1;i<=n;i++){
    
    
		for(int j=1;j<=n;j++){
    
    
			if(M_del[i][j]==1){
    
    
				M[i][j]='.';//岛屿变为海域 
			}
		}
	}
	return ;
}

void dfs(int i,int j,int n){
    
    //dfs函数 
	if(i<1||j<1||i>n||j>n)return ;
	if(M_mark[i][j]==1)return ;
	if(M[i][j]=='.')return ;
	
	M_mark[i][j]=1;
	
	for(int i=0;i<4;i++){
    
    
		int ii=i+dir[i][0];
		int jj=j+dir[i][1];
		dfs(ii,jj,n);
	}
	return ;
}

int sum(int n){
    
    //求和函数 
	int cnt=0;
	for(int i=1;i<=n;i++){
    
    
		for(int j=1;j<=n;j++){
    
    
			if(M[i][j]=='#'&&M_mark[i][j]==0){
    
    
				dfs(i,j,n);
				cnt++;
			}
		}
	}
	return cnt;
}

int main(){
    
    
	int n;
	scanf("%d",&n);
	getchar();
	for(int i=1;i<=n;i++){
    
    
		for(int j=1;j<=n;j++){
    
    
			scanf("%c",&M[i][j]);
		}
		getchar();
	}	
	del(n);
	int ans=sum(n);
	printf("%d",ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/115238297