Problem K

Time Limit : 2000/1000ms (Java/Other)   Memory Limit :65536/32768K (Java/Other)

Total Submission(s) : 2   Accepted Submission(s) : 1

Problem Description

The GeoSurvComp geologicsurvey company is responsible for detecting underground oil deposits.GeoSurvComp works with one large rectangular region of land at a time, andcreates a grid that divides the land into numerous square plots. It thenanalyzes each plot separately, using sensing equipment to determine whether ornot the plot contains oil. A plot containing oil is called a pocket. If twopockets are adjacent, then they are part of the same oil deposit. Oil depositscan be quite large and may contain numerous pockets. Your job is to determinehow many different oil deposits are contained in a grid. <br>

Input

The input file contains one ormore grids. Each grid begins with a line containing m and n, the number of rowsand columns in the grid, separated by a single space. If m = 0 it signals theend 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-linecharacters). Each character corresponds to one plot, and is either `*',representing the absence of oil, or `@', representing an oil pocket.<br>

Output

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

Sample Input

1 1

*

3 5

*@*@*

**@**

*@*@*

1 8

@@****@*

5 5

****@

*@@*@

*@**@

@@@*@

@@**@

0 0

Sample Output

0

1

2

2

T算法分析：

`#include<bits/stdc++.h>using namespace std;int n,m,k;char mapp[105][105];int flag[105][105]={0},a[105]={0};int dx[8]={0,0,1,-1,1,1,-1,-1},    dy[8]={1,-1,0,0,1,-1,-1,1};int bfs(int x1,int y1){    k++;   //油田加1    int i,j,head=0,tail=1,x,y;    flag[x1][y1]=0;    int xx[10000],yy[10000];    memset(xx,0,sizeof(xx));    memset(yy,0,sizeof(yy));    xx[1]=x1;    yy[1]=y1;    do    {        head++;        for(i=0;i<8;i++)        {            x=xx[head]+dx[i];            y=yy[head]+dy[i];            if(x>=0&&x<n&&y>=0&&y<m&&flag[x][y])            {                tail++;                flag[x][y]=0;                xx[tail]=x;                yy[tail]=y;            }        }    }while(head<tail);}int main(){    int i,j;    while(scanf("%d%d",&n,&m)&&(n||m))    {        k=0;        memset(a,0,sizeof(a));        memset(flag,0,sizeof(flag));  //注意清0       for(i=0;i<n;i++)       {           scanf("%s",&mapp[i]);           for(j=0;j<m;j++)            if(mapp[i][j]=='@')               flag[i][j]=1;       }       for(i=0;i<n;i++)        for(j=0;j<m;j++)        if(flag[i][j]==1)       {           bfs(i,j);       }       cout<<k<<endl;    }    return 0;}`