油田统计

来源:互联网 发布:jdk 7u79 windows x64 编辑:程序博客网 时间:2024/04/29 01:52

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
/**这个题是我以前做过的原题,但是留了一个疑问,就是最后一组测试数据结过有问题,当时感觉思路没问题,就没有多想,结果这次坑了自己,所以这次做依旧用了很多的时间,虽然过了,但为了透彻的理解,我又做了一遍,发现,如果以注释的方法输入确实存在一些问题,但可以AC,问学长,学长也说不太清楚,让我尽量以%s的方式输入,*/#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;char opt[150][150];int direct[8][2]= {{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};int n,m;void DFS(int x, int y){    opt[x][y] = '*';///本来还打算标记一下,直接覆盖更爽了一点    for(int i = 0; i < 8; i++)    {        int new_x = x + direct[i][0];        int new_y = y + direct[i][1];                if(opt[new_x][new_y] == '@')///为了麻烦的剪枝,进行深搜的条件定的苛刻一点,            DFS(new_x,new_y);           ///但是一定记得初始化,不然会越界,问题大大的有    }}int main(){    while(~scanf("%d%d",&n, &m)&&(n+m))    {        getchar();        memset(opt,'\0',sizeof(opt));        for(int i =0; i < n; i++)        {            /*for(int j = 0; j < m; j++)                scanf("%c",&opt[i][j]);            getchar();*/            scanf("%s",opt[i]);        }        int ans = 0;        for(int i =0; i < n; i++)            for(int j = 0; j < m; j++)            {                if(opt[i][j] == '@')                {                    DFS(i,j);                    ans++;                }            }        printf("%d\n",ans);    }    return 0;}

0 0
原创粉丝点击