Global warming
You have a picture of a sea area with NxN pixels, "." for ocean, "#" for land, as shown below:
...
.##...
.##...
...##.
...####.
...###.
...
An island is made of land connected in all four directions.For example, there are two islands in the picture above.
Because global warming is causing sea level rise, scientists predict that a pixel of the edge of an island will be submerged in the coming decades.Specifically, if a land pixel is adjacent to the ocean (there are oceans in the four adjacent pixels above and below), it will be submerged.
For example, the waters in the above image will look like this in the future:
...
...
...
...
...#...
...
...
Please calculate how many islands in the photo will be completely submerged, according to scientists'predictions.
[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.
Photos ensure that the pixels in row 1, column 1, row N, and column N are oceans.
[Output Format]
An integer represents the answer.
[Input Sample]
7
...
.##...
.##...
...##.
...####.
...###.
...
[Output Sample]
1
Resource conventions:
Peak memory consumption (including virtual machines) < 256M
CPU consumption < 1000ms
The original topic uses a string to represent the map, where I'm switching the sea and land to 0 and 1. This transformation is very similar. First, change the whole map to 0, change the position of # to 1, and use the breadth-first algorithm to traverse the whole map.
Here my program results are an analytical version.
#include<bits/stdc++.h> using namespace std; int m,n,counta=0,countb=0; int qwe[500][500]; int qwr[500][500]; int visited[500][500]; struct queue{ int x[500]; int y[500]; int head,tail; }qqq; int enqueue(int o,int p){ if((qqq.tail+1)%500==qqq.head){ return 0; } if(o<0||o>=m||p<0||p>=n){ return 0; } if(visited[o][p]!=0||qwe[o][p]==0){ return 0; } qqq.x[qqq.tail]=o; qqq.y[qqq.tail]=p; qqq.tail++; return 1; } int enqueue1(int o,int p){ if((qqq.tail+1)%500==qqq.head){ return 0; } if(o<0||o>=m||p<0||p>=n){ return 0; } if(visited[o][p]!=0||qwr[o][p]==0){ return 0; } qqq.x[qqq.tail]=o; qqq.y[qqq.tail]=p; qqq.tail++; return 1; } int dequeue(){ if(qqq.head==qqq.tail){ return 0; } qqq.head++; return 1; } int check(int o,int p){ if(o<0||o>=m||p<0||p>=n){ return 2; } if(qwe[o][p]==1){ return 1; } return 0; } bool isempty(){ if(qqq.head==qqq.tail){ return true; } return false; } int main(){ qqq.head=0; qqq.tail=0; int f,g; int i,j; cin>>m; cin>>n; for(i=0;i<m;i++){ for(j=0;j<n;j++){ cin>>qwe[i][j]; visited[i][j]=0; } } for(i=0;i<500;i++){ for(j=0;j<500;j++){ if(qwe[i][j]==1&&visited[i][j]==0){ enqueue(i,j); while (!isempty()){ f=qqq.x[qqq.head]; g=qqq.y[qqq.head]; visited[f][g]=counta+1; dequeue(); enqueue(f-1,g); enqueue(f+1,g); enqueue(f,g+1); enqueue(f,g-1); } counta++; } } } cout<<"--------------------"<<endl; for(i=0;i<m;i++){ for(j=0;j<n;j++){ cout<<visited[i][j]<<" "; } cout<<endl; } cout<<"tail"<<qqq.tail<<" head"<<qqq.head<<endl; cout<<"count"<<counta<<endl; cout<<"--------------------"<<endl; for(i=0;i<m;i++){ for(j=0;j<n;j++){ if(check(i,j)==1){ if(check(i-1,j)*check(i+1,j)*check(i,j-1)*check(i,j+1)!=0){ qwr[i][j]=1; } }else{ qwr[i][j]=0; } cout<<qwr[i][j]<<" "; visited[i][j]=0; } cout<<endl; } for(i=0;i<500;i++){ for(j=0;j<500;j++){ if(qwr[i][j]==1&&visited[i][j]==0){ enqueue(i,j); while (!isempty()){ f=qqq.x[qqq.head]; g=qqq.y[qqq.head]; visited[f][g]=countb+1; dequeue(); enqueue1(f-1,g); enqueue1(f+1,g); enqueue1(f,g+1); enqueue1(f,g-1); } countb++; } } } cout<<"--------------------"<<endl; for(i=0;i<m;i++){ for(j=0;j<n;j++){ cout<<visited[i][j]<<" "; } cout<<endl; } cout<<"tail"<<qqq.tail<<" head"<<qqq.head<<endl; cout<<"count"<<countb<<endl; }