Oil Deposits

来源:互联网 发布:如何查看淘宝降权 编辑:程序博客网 时间:2024/06/16 00:26
Problem 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
题意:'@'代表油袋,'*'代表没有油;油袋相连(水平,垂直,斜对角线相连)的表示一个油藏。问给你一个m*n大小的矩阵有多少个油藏?
此题可以用基本的DFS,也可用BFS,输入的时候要按行输入,一行的输入数据后可能有空格!!!
1.DFS
time:15ms code len:810B
    
#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const int N=105;int n,m;char a[N][N];void dfs(int x,int y){    a[x][y]='*';    for(int i=-1; i<=1; i++)        for(int j=-1; j<=1; j++)        {            int nx=x+i,ny=y+j;            if(a[nx][ny]=='@'&&nx>=0&&nx<m&&ny>=0&&ny<n)                dfs(nx,ny);        }    return;}int main(){    while(~scanf("%d%d",&m,&n)&&(m||n))    {        int i,j,ans=0;        for(i=0; i<m; i++)            scanf("%s",a[i]);        for(i=0; i<m; i++)            for(j=0; j<n; j++)            {                if(a[i][j]=='@')                {                    dfs(i,j);                    ans++;                }            }        printf("%d\n",ans);    }    return 0;}
2.BFS
time:0ms code len:1152B
#include<cstdio>#include<queue>#include<algorithm>using namespace std;int n,m;char a[105][105];int dx[]={1,1,1,0,0,-1,-1,-1},dy[]={0,1,-1,-1,1,-1,1,0};struct node{    int xx;    int yy;};void bfs(int x,int y){    node now,next;    queue<node>q;    now.xx=x;    now.yy=y;    q.push(now);    while(!q.empty())    {        now=q.front();        q.pop();        for(int i=0; i<8; i++)        {            int nx=now.xx+dx[i],ny=now.yy+dy[i];            if(nx>=0&&nx<m&&ny>=0&&ny<n&&a[nx][ny]=='@')            {                a[nx][ny]='*';                next.xx=nx;                next.yy=ny;                q.push(next);            }        }    }    return;}int main(){    while(scanf("%d%d",&m,&n)&&(m||n))    {        int i,j,ans=0;        for(i=0; i<m; i++)            scanf("%s",a[i]);        for(i=0; i<m; i++)            for(j=0; j<n; j++)            {                if(a[i][j]=='@')                {                    bfs(i,j);                    ans++;                }            }        printf("%d\n",ans);    }    return 0;}
原创粉丝点击