• HDU_1241——石油探索DFS


    Problem 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
     1 #include <stdio.h>
     2 char map[100][100];
     3 int num,m,n;
     4 void Dfs(int i,int j)
     5 {
     6    if(i>=0&&i<=m&&j>=0&&j<=n&&map[i][j]=='@')
     7       {
     8          map[i][j]='#';//记录走过之后不再复原路劲,表示这条路已经走过了 
     9          Dfs(i+1,j);
    10          Dfs(i-1,j);
    11          Dfs(i,j+1);
    12          Dfs(i,j-1);
    13          Dfs(i+1,j+1);
    14          Dfs(i+1,j-1);
    15          Dfs(i-1,j-1);
    16          Dfs(i-1,j+1);
    17       }
    18 }
    19 int main()
    20 {
    21    while(scanf("%d%d",&m,&n)!=EOF && m!=0)
    22       {
    23          num=0;
    24          for(int i=0;i<m;i++)
    25             scanf("%s",map[i]);
    26          for(int i=0;i<m;i++)
    27             for(int j=0;j<n;j++)
    28                if(map[i][j]=='@')
    29                   {
    30                      Dfs(i,j);
    31                      num++;   
    32                   }
    33          printf("%d
    ",num);
    34       }  
    35 }
    ——现在的努力是为了小时候吹过的牛B!!
  • 相关阅读:
    java设计模式之工厂方法模式
    java设计模式之抽象工厂模式
    HADOOP集群搭建
    错误代码:0x80070032 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
    Data Flow ->> Character Map
    Data Flow ->> Import Column & Export Column
    Data Flow ->> Raw File Source & Raw File Destination
    Data Flow ->> OLE DB Destination ->> Fast Load
    Connection Manager ->> Multiple Flat File Connection & Multiple File Connection
    Data Flow ->> Multiple Excel Sheet Loaded Into One Table
  • 原文地址:https://www.cnblogs.com/pingge/p/3185617.html
Copyright © 2020-2023  润新知