zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    delphi JPG图片 旋转 切边 缩放
    CXGrid TcxButtonEdit 信息获取 TcxButtonEditProperties取得TcxGridDBTableView
    firemonkey EDit 改变颜色
    firemonkey ListView DynamicAppearance
    delphi GDI 图片压缩代码 据说是位图缩放保持原图视觉效果最好的算法
    cxGrid_Q31584 cxgrid 拖放移动记录
    delphi XE Datasnap SERVER 在windows 7 下为服务添加描述信息
    delphi 数组复制利用CopyMemory 最为完美
    delphi强制WebBrowser控件使用指定版本显示网页
    iOS从零开始学习直播之音频2.后台播放和在线播放
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3663013.html
Copyright © 2011-2022 走看看