zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Win10远程桌面 出现 身份验证错误,要求的函数不受支持,这可能是由于CredSSP加密Oracle修正 解决方法
    通过WifI开发调试Android设备
    js 什么是深拷贝问题?
    JavaScript 如何从引用类型(Array 、 Object)创建一个新的对象
    css ::selection 的妙用
    nodejs request gb2312乱码的问题
    echarts geo地图坐标转换为页面Offset坐标
    关于 Chrome Console 查看DOM详情细节的奇思淫巧
    ie11 下 input 默认有 X 关闭按钮的问题
    办公技巧:局域网内设置固定ip
  • 原文地址:https://www.cnblogs.com/q-c-y/p/5392748.html
Copyright © 2011-2022 走看看