Global Warming (Blue Bridge on April 18 cups)

global warming

. "" Do you have a photo of Zhang waters NxN pixels, expressed ocean, "#" represents the land, as follows:


.##…
.##…
…##.
…####.
…###.

The "up and down" on the four directions together a piece of land consisting of an island. For example there are two islands on the map.

Due to the rising sea levels caused by global warming, scientists predict the next few decades, the islands edge a pixel range will be submerged. Specifically, (four adjacent pixels in the vertical and horizontal marine) If a pixel and adjacent land ocean, it will be submerged.

For example in the figure above sea will become the future looks as follows:





…#…

Please work: According to the scientists predict, the number of photos in the islands will be completely submerged.

[Input format
of the first row contains an integer N. (1 <= N <= 1000 )
The following N rows and N columns representative of a picture waters.

Photos ensure row 1, column 1, line N, N-th column of pixels are ocean.

[Output Format]
a integer answer.

[Sample input]
7
...
## ...
## ...
... ##
... ####.
... ###.
...

[Output] Sample
1

Resources for:
peak memory consumption (including virtual machines) <256M
the CPU consumption <1000ms


 

Use original title string representation of the map, where I would land and sea changed to 0 and 1, the less the same approach transform the entire map is first changed to 0, to 1 in # location, breadth-first algorithm traversing the entire map.

Before change and after change of the different maps, it requires two enqueue algorithm for the corresponding map, but since there is no remaining elements of the queue after each traversal of the map, so that can be reused to create a queue, the algorithm is the same team .

Here is a result of my analysis program 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;
}

Guess you like

Origin www.cnblogs.com/hyffff/p/12110168.html