• Oil Deposits(DFS连通图)


    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

    0 1 2 2
     
     
    套模板,上代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 #include<iostream>
     5 using namespace std;
     6 char map[110][110];
     7 int  vis[110][110];///标记数组
     8 int dir[8][2]= {{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,-1},{1,-1},{-1,1}};///八面搜素
     9 int n,m;
    10 void DFS(int x,int y)
    11 {
    12     int a,b,i;
    13     vis[x][y]=1;
    14     for(i=0; i<8; i++)
    15     {
    16         a=x+dir[i][0];
    17         b=y+dir[i][1];
    18         if(a>=0&&a<n&&b>=0&&b<m&&vis[a][b]==0&&map[a][b]=='@')
    19         {
    20             DFS(a,b);
    21         }
    22     }
    23     return ;
    24 }
    25 int main()
    26 {
    27     int count,i,j;
    28     while(scanf("%d%d",&n,&m)!=EOF)
    29     {
    30         getchar();
    31         if(n==0&&n==0)
    32             break;
    33         memset(map,0,sizeof(map));
    34         memset(vis,0,sizeof(vis));
    35         count=0;
    36         for(i=0; i<n; i++)
    37         {
    38             scanf("%s",map[i]);
    39         }
    40         for(i=0; i<n; i++)
    41         {
    42             for(j=0; j<m; j++)
    43             {
    44                 if(vis[i][j]==0&&map[i][j]=='@')
    45                 {
    46                     count++;
    47                     DFS(i,j);
    48                 }
    49             }
    50         }
    51         printf("%d
    ",count);
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    改写历史,永久删除git库的物理文件
    双调排序
    GitHub从无到有
    Nginx的安装与基本应用
    Django从无到有的艰苦历程
    pycharm 相关设置问题
    ORM介绍
    Django中的过滤器
    FBV和CBV的差异
    django中models field详解
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/8906038.html
Copyright © 2020-2023  润新知