zoukankan      html  css  js  c++  java
  • uva 572

      Oil Deposits 

    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 le m le 100$ and $1 le n le 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
    
    分析:简单搜索,枚举每个未访问油田位置进行dfs,每次dfs搜索油田并标记就可以了,dfs次数就是油田的次数。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #define maxlen 1100
     5 using namespace std;
     6 int n,m;
     7 char maps[maxlen][maxlen];
     8 int xx[]={-1,-1,-1, 0, 0, 1, 1, 1};
     9 int yy[]={-1, 0, 1,-1, 1,-1, 0, 1};
    10 bool visited[maxlen][maxlen];
    11 bool judge(int x,int y)
    12 {
    13     return x>=0&&x<n&&y>=0&&y<m&&maps[x][y]=='@'&&!visited[x][y];
    14 }
    15 int dfs(int x,int y)
    16 {
    17     if(!judge(x,y))
    18         return 0;
    19     visited[x][y]=true;
    20     for(int i=0;i<8;++i)
    21     {
    22         int nx=x+xx[i];
    23         int ny=y+yy[i];
    24         if(judge(nx,ny))
    25             dfs(nx,ny);
    26     }
    27     return 1;
    28 }
    29 int main ()
    30 {
    31     while(scanf("%d%d",&n,&m)!=EOF)
    32     {
    33         if(n==0&&m==0)
    34             break;
    35         for(int i=0;i<n;++i)
    36             for(int j=0;j<m;++j)
    37                 cin>>maps[i][j];
    38         int cnt=0;
    39         memset(visited,0,sizeof(visited));
    40         for(int i=0;i<n;++i)
    41         {
    42             for(int j=0;j<m;++j)
    43             {
    44                 if(!visited[i][j])
    45                     cnt+=dfs(i,j);
    46             }
    47         }
    48         printf("%d
    ",cnt);
    49     }
    50 }
    View Code
  • 相关阅读:
    Oracle数据库安装
    [转]卡西欧手表调日期正确方法
    python密码处理(可用于生产模式)
    [转]python对json的相关操作
    [转]Python中的with…as…
    Python标准库--os模块
    我的github代码添加
    Python正则表达式+自创口诀
    自己总结python用xlrdxlwt读写excel
    CentOS安装+配置+远程
  • 原文地址:https://www.cnblogs.com/shuzy/p/3231222.html
Copyright © 2011-2022 走看看