zoukankan      html  css  js  c++  java
  • 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!!
  • 相关阅读:
    1.Python基础语法
    Python学习4:商城购物
    Python学习3:猜年龄游戏进阶版
    Python学习2:猜年龄游戏
    python学习1:判断学生成绩等级
    K8S集群平滑回退或升级
    Xtrabackup工作原理
    Android App 侧边栏菜单的简单实现
    NoActionBar主题下如何添加OptionsMenu
    TabLayout+ViewPager制作简单导航栏
  • 原文地址:https://www.cnblogs.com/pingge/p/3185617.html
Copyright © 2011-2022 走看看