• Oil Deposits


                                                         Oil Deposits

                             Time Limit: 1000ms                                                                 Memory Limit: 32768KB
                                                           64-bit integer IO format:      Java class name:

    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


    最基本的dfs题。
     1 include<cstring>
     2 #include<cstdio>
     3 using namespace std;
     4 const int MAX=105;
     5 char a[MAX][MAX];
     6 int n,m,idx[MAX][MAX];
     7 void dfs(int x,int y,int s)
     8 {
     9     if(x<0||x>=n||y<0||y>=m) return;
    10     if(idx[x][y]>0||a[x][y]!='@') return;
    11     idx[x][y]=s;
    12     for(int dx=-1;dx<=1;dx++)
    13         for(int dy=-1;dy<=1;dy++)
    14             if(dx!=0||dy!=0)
    15                 dfs(x+dx,y+dy,s);
    16 }
    17 int main()
    18 {
    19     while(scanf("%d%d",&n,&m)&&n&&m)
    20     {
    21         for(int i=0;i<n;i++)
    22             scanf("%s",a[i]);
    23         memset(idx,0,sizeof(idx));
    24         int k=0;
    25         for(int i=0;i<n;i++)
    26             for(int j=0;j<m;j++)
    27                 if(idx[i][j]==0&&a[i][j]=='@')
    28                     dfs(i,j,++k);
    29         printf("%d
    ",k);
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    Windows下Goland的Terminal设置为Git Bash
    BoltDB简单使用教程
    Base64编码转换原理
    [区块链|非对称加密] 对数字证书(CA认证)原理的回顾
    [数据库锁机制] 深入理解乐观锁、悲观锁以及CAS乐观锁的实现机制原理分析
    升级mojave后的小问题解决
    ubuntu安装ssh服务记录
    dubbo+maven多模块项目单元测试
    sass与less
    (转)初识 Lucene
  • 原文地址:https://www.cnblogs.com/q-c-y/p/5392748.html
Copyright © 2020-2023  润新知