中礦新生賽 H 璐神看島嶼【BFS/DFS求聯通塊/連通塊地區在邊界則此連通塊無效】

來源:互聯網
上載者:User

標籤:push   for   names   注意   技術分享   ==   orm   64bit   連通塊   

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld題目描述璐神現在有張n*m大小的地圖,地圖上標明了陸地(用"#"表示)和海洋(用"."表示),現在璐神要計算這張地圖上島嶼的數量。已知島嶼是由陸地的連通塊組成,即一塊陸地的上、下、左、右,左上,右上,左下,右下有其他陸地,則構成連通塊,以此類推。此外,島嶼的詳細定義如下:1、島嶼的周圍必須全是海洋。2、如果連通塊有任意地區在地圖邊界,則該連通塊不是島嶼。輸入描述:
第1行輸入兩個整數n,m,代表地圖的長和寬。
第2-n+1行,每行輸入m個字元,字元為"#"表示陸地,為"."表示海洋。
資料保證:0<n,m≤200
輸出描述:
輸出一行整數,代表島嶼的數量。
樣本1輸入
3 3....#....
輸出
1
說明
只有中間的1塊陸地是島嶼,所以島嶼數=1
樣本2輸入
3 3#...#....
輸出
0
說明
中間的連通塊有地區在邊界,所以不是島嶼,島嶼數=0。

【分析】:bfs 求解連通塊,注意的是在求解過程中,如果出現連通塊地區在邊界,則記錄此連通塊無效,但仍需將 bfs 操作進行完。
【代碼】:
#include <bits/stdc++.h>using namespace std;const int maxn = 250;char mp[250][250];int vis[maxn][maxn]={0};int dir[][2] ={{1,0},{0,1},{-1,0},{0,-1},{-1,-1},{-1,1},{1,-1},{1,1}};int n,m,flag=0;void dfs(int x,int y){    vis[x][y]=1;     if(x==0||y==0||x==n-1||y==m-1)           flag=1;    for(int i=0;i<8;i++)    {        int tx=x+dir[i][0];        int ty=y+dir[i][1];        if(mp[tx][ty]==‘#‘&&tx>=0&&ty>=0)        {            mp[tx][ty]=‘.‘;            dfs(tx,ty);        }    }}int main(){    int sum=0;    cin>>n>>m;    for(int i=0;i<n;i++)        cin>>mp[i];    for(int i=0;i<n;i++)    {        for(int j=0;j<m;j++)        {            if(mp[i][j]==‘#‘)            {                flag=0;                dfs(i,j);                if(flag==0)                    sum++;            }        }    }    cout<<sum<<endl;    return 0;}
DFS

 

#include<bits/stdc++.h>using namespace std; const int maxn=210; char mp[maxn][maxn];int vis[maxn][maxn];const int dir[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}}; int n,m,ans=0;void bfs(int x,int y){    if(mp[x][y]==‘.‘){        vis[x][y]=2;        return;    }    if(x==0||y==0||x==n-1||y==m-1){        vis[x][y]=2;      //  return;    }     queue<pair<int,int> >q;    q.push(make_pair(x,y));    while(!q.empty()){        int nowx=q.front().first;        int nowy=q.front().second;        if(nowx==0||nowy==0||nowx==n-1||nowy==m-1){            vis[x][y]=2;            //return;        }        q.pop();        for(int i=0;i<8;++i){            int x1=nowx+dir[i][0];            int y1=nowy+dir[i][1];            if(vis[x1][y1]==0&&mp[x1][y1]==‘#‘){                vis[x1][y1]=1;                q.push(make_pair(x1,y1));            }        }    }} int main(){    while(cin>>n>>m){        memset(mp,0,sizeof(mp));        memset(vis,false,sizeof(vis));        ans=0;        for(int i=0;i<n;++i)            cin>>mp[i];        for(int i=0;i<n;++i){            for(int j=0;j<m;++j){                if(!vis[i][j]){                    vis[i][j]=1;                    bfs(i,j);                    if(vis[i][j]==1){                         ans++;                    }                }            }        }        cout<<ans<<endl;    }     return 0;}
BFS

 

中礦新生賽 H 璐神看島嶼【BFS/DFS求聯通塊/連通塊地區在邊界則此連通塊無效】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.