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
  • 相关阅读:
    汇编语言从入门到精通-指令汇总
    汇编语言从入门到精通-5微机CPU的指令系统1
    两台W7系统的电脑,A电脑可以ping通B电脑,B电脑ping不通A电脑。
    linux与python3安装redis
    python3报:ImportError: No module named 'MySQLdb'
    Django安装数据库MySQLdb
    win7系统中开启wifi热点
    以太坊
    route命令
    Android——UI(1) (activity window decor)
  • 原文地址:https://www.cnblogs.com/shuzy/p/3231222.html
Copyright © 2011-2022 走看看