zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    空间谱专题16:间距选取分析
    复数矩阵分解的拆解思路(矩阵求逆/特征值分解)
    〖Linux〗Linux高级编程
    〖Linux〗安装和使用virtualenv,方便多个Python版本中切换
    〖Linux〗bash和expect执行ssh命令行sshcmd.exp
    【Linux】解决Android Stadio报错:error in opening zip file
    〖Linux〗穿越城墙之后,直接连接国内网站的路由配置
    〖Linux〗使用纯命令行来操作VBOX(宿主机不需要X11 Server)
    〖Network〗一行命令创建 http-server
    〖Linux〗多个JDK版本之间快速切换
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/8906038.html
Copyright © 2011-2022 走看看