连通分支

来源:互联网 发布:王夫之的知行 编辑:程序博客网 时间:2024/06/05 06:06

Description

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid. 
 

Input

The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
 

Output

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
 

Sample Input

1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0
 

Sample Output

0122
代码:
dfs:
#include<iostream>#include<cstring># define Maxn 110using namespace std;char mp[Maxn][Maxn];int vis[Maxn][Maxn];int cou,m,n;int dx[]={0,0,1,-1,-1,-1,1,1};int dy[]={-1,1,0,0,-1,1,-1,1};int judge(int x,int y){    return x>=0&&y>=0&&x<m&&y<n;}void dfs(int x,int y){    if(!vis[x][y]) vis[x][y]=1;    int flag=0;    for(int i=0;i<8;i++){        int tx=x+dx[i],ty=y+dy[i];        if(judge(tx,ty)&&!vis[tx][ty])            if(mp[tx][ty]=='*') vis[tx][ty]=1;//dfs停止,标注该点已搜索            else {flag--;dfs(tx,ty);}        flag++;    }    if(flag==8&&mp[x][y]=='@') cou++;}int main(){    while(cin>>m>>n,m){        for(int i=0;i<m;i++)            for(int j=0;j<n;j++)                cin>>mp[i][j];        cou=0;        memset(vis,0,sizeof vis);        for(int i=0;i<m;i++)            for(int j=0;j<n;j++)                if(!vis[i][j]) dfs(i,j);     cout<<cou<<endl;    }    return 0;}
bfs:
#include<iostream>#include<cstring>#include <algorithm>#include<queue>#include<vector># define Maxn 110#define MP(a, b) make_pair(a, b)using namespace std;typedef pair<int, int> pii;int m,n,cou;char mp[Maxn][Maxn];int vis[Maxn][Maxn];int dx[]={0,0,1,-1,-1,-1,1,1};int dy[]={-1,1,0,0,-1,1,-1,1};int judge(int x,int y){    return x>=0&&y>=0&&x<m&&y<n;}void bfs(int x,int y){    vis[x][y]=1;    queue<pii> p;    p.push(MP(x,y));    while(!p.empty()){        pii nv=p.front();p.pop();        for(int i=0;i<8;i++){            int tx=nv.first+dx[i],ty=nv.second+dy[i];            if(judge(tx,ty)&&mp[tx][ty]=='@'&&vis[tx][ty]==0)            {vis[tx][ty]=1;p.push(MP(tx,ty));}        }    }}int main(){    char to;    while(cin>>m>>n,m){        for(int i=0;i<m;i++)            for(int j=0;j<n;j++)                cin>>mp[i][j];        cou=0;        memset(vis,0,sizeof vis);        for(int i=0;i<m;i++)            for(int j=0;j<n;j++)            if(mp[i][j]=='@'&&vis[i][j]==0) {cou++;bfs(i,j);}        cout<<cou<<endl;    }    return 0;}
注:非常简单的搜索题。
0 0
原创粉丝点击