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!!
  • 相关阅读:
    linux字符设备文件的打开操作
    Linux用ps命令查找进程PID再用kill命令终止进程的方法
    Linux内核锁与中断处理
    写给大数据开发初学者的话
    zabbix监控系统客户端安装
    详解zabbix安装部署(Server端篇)
    Keepalived+Nginx架构整理版
    Nginx + Tomcat 动静分离实现负载均衡
    五个常用的Linux监控脚本代码
    16个Linux服务器监控命令
  • 原文地址:https://www.cnblogs.com/pingge/p/3185617.html
Copyright © 2011-2022 走看看