zoukankan      html  css  js  c++  java
  • G

    G - Oil Deposits

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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

     

    //普通的dfs,把四周都遍历就行,计算有几块

     

     1 #include <iostream>
     2 #include <string.h>
     3 using namespace std;
     4 
     5 char map[105][105];
     6 int way[105][105];
     7 int all;
     8 int m,n;
     9 
    10 void read_map()
    11 {
    12     memset(way,0,sizeof(way));
    13     for (int i=1;i<=m;i++)
    14     {
    15         for (int j=1;j<=n;j++)
    16         {
    17             cin>>map[i][j];
    18         }
    19     }
    20 
    21 }
    22 
    23 void dfs(int x,int y)
    24 {
    25     way[x][y]=1;
    26     if (x+1<=m&&map[x+1][y]=='@'&&way[x+1][y]==0) dfs(x+1,y);
    27     if (x+1<=m&&y-1>=1&&map[x+1][y-1]=='@'&&way[x+1][y-1]==0) dfs(x+1,y-1);
    28     if (y-1>=1&&map[x][y-1]=='@'&&way[x][y-1]==0) dfs(x,y-1);
    29     if (y-1>=1&&x-1>=1&&map[x-1][y-1]=='@'&&way[x-1][y-1]==0) dfs(x-1,y-1);
    30     if (x-1>=1&&map[x-1][y]=='@'&&way[x-1][y]==0) dfs(x-1,y);
    31     if (x-1>=1&&y+1<=n&&map[x-1][y+1]=='@'&&way[x-1][y+1]==0) dfs(x-1,y+1);
    32     if (y+1<=n&&map[x][y+1]=='@'&&way[x][y+1]==0) dfs(x,y+1);
    33     if (y+1<=n&&x+1<=m&&map[x+1][y+1]=='@'&&way[x+1][y+1]==0) dfs(x+1,y+1);
    34 }
    35 
    36 
    37 int main()
    38 {
    39     
    40     while (cin>>m>>n)
    41     {
    42         if (m==0&&n==0) break;
    43 
    44         all=0;
    45         read_map();
    46         for (int i=1;i<=m;i++)
    47         {
    48             for (int j=1;j<=n;j++)
    49             {
    50                 if (map[i][j]=='@'&&way[i][j]==0)
    51                 {
    52                     dfs(i,j);
    53                     all++;
    54                 }
    55             }
    56         }
    57         cout<<all<<endl;
    58     }
    59 
    60     return 0;
    61 }
    View Code

     

  • 相关阅读:
    《CLR.via.C#第三版》第二部分第6,7章节读书笔记(三)
    《CLR.via.C#第三版》第二部分第4,5章节读书笔记(二)
    《CLR.via.C#第三版》第一部分读书笔记(一)
    https ssl
    bower 代理
    哦?
    Gulp 相关
    Edge浏览器的几个创意应用
    软件中英对照
    Windows 10 Mobile 演示:插入耳机自动执行 APP
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/5676724.html
Copyright © 2011-2022 走看看