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 }
  • 相关阅读:
    计算字符串相似度算法——Levenshtein
    信息检索参考网站
    文献检索
    【BZOJ】1684: [Usaco2005 Oct]Close Encounter(暴力+c++)
    【BZOJ】1664: [Usaco2006 Open]County Fair Events 参加节日庆祝(线段树+dp)
    【BZOJ】1644: [Usaco2007 Oct]Obstacle Course 障碍训练课(bfs)
    【BZOJ】1652: [Usaco2006 Feb]Treats for the Cows(dp)
    【BZOJ】1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚(dp/线段树)
    Codeforces Round #265 (Div. 2)
    中秋节模拟赛之冷月葬花魂(被虐瞎)
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/8906038.html
Copyright © 2011-2022 走看看