• HDUOJ---1241Oil Deposits(dfs)


    Oil Deposits

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 10644    Accepted Submission(s): 6176


    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
     
    Source
     
     
     
    代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 char map[101][101];
     5 int dir[8][2]={
     6     {1,0},
     7     {-1,0},
     8     {0,1},
     9     {0,-1},
    10     {1,1},
    11     {-1,-1},
    12     {1,-1},
    13     {-1,1}
    14 };
    15  int n,m;
    16 void dfs(int st,int en)
    17 {
    18      for(int i=0;i<8;i++)
    19      {
    20          if(st+dir[i][0]>=n&&st+dir[i][0]<0) continue;
    21          if(en+dir[i][0]>=m&&en+dir[i][0]<0) continue;
    22          if(map[st+dir[i][0]][en+dir[i][1]]=='@')
    23          {
    24             map[st+dir[i][0]][en+dir[i][1]]='*';
    25             dfs(st+dir[i][0],en+dir[i][1]);
    26          }
    27      }
    28 }
    29 int main()
    30 {
    31   int i,j,cnt;
    32   while(scanf("%d%d",&n,&m),n+m)
    33   {
    34       cnt=0;
    35       for(i=0;i<n;i++)
    36           scanf("%s",map[i]);
    37       for(i=0;i<n;i++)
    38       {
    39           for(j=0;j<m;j++)
    40           {
    41               if(map[i][j]=='@')
    42               {
    43                 dfs(i,j);
    44                 cnt++;
    45               }
    46           }
    47       }
    48       printf("%d
    ",cnt);
    49   }
    50  return 0;
    51 }
    View Code
  • 相关阅读:
    原生JS写轮播图(转自https://www.cnblogs.com/LIUYANZUO/p/5679753.html)
    用JS写成简易计算器(转自https://mp.weixin.qq.com/s/Jxe3V7D0PFLvIFNZPlSyNg)
    Js获取当前日期时间的方法
    三元运算符的应用(转自百度百科)
    <pre> 标签的简要作用(以前未接触过的)
    用JavaScript实现两种功能:1、切换全选/全不选文字;2、根据选中个数更新全选框状态;(实例代码来自view-source:http://www.fgm.cc/learn/lesson2/12.html)
    Windows 网络通讯开发
    函数指针的使用
    cin的使用问题
    markdown简介
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3663013.html
Copyright © 2020-2023  润新知