zoukankan      html  css  js  c++  java
  • hdu1241 Oil Deposits

                                                     Oil Deposits

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


    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
     
    DFS类型题
     1 #include<iostream>
     2 #include<cstdio>
     3 #define N 110
     4 using namespace std;
     5 
     6 int m,n;
     7 
     8 int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
     9 
    10 char maps[N][N];
    11 
    12 int nx,ny;
    13 
    14 void dfs(int x,int y)
    15 {
    16 
    17     if(x<0||x>=m||y<0||y>=n)//判断当前所在位置是否合法;
    18         return ;
    19     if(maps[x][y]=='*')//递归结束条件;
    20         return ;
    21     else
    22     {
    23         maps[x][y]='*';
    24         for(int i=0;i<8;i++)//搜索相邻的8个方向;
    25         {
    26             nx=x+dir[i][0];
    27             ny=y+dir[i][1];
    28             dfs(nx,ny);//进行深搜;
    29         }
    30 
    31     }
    32 }
    33 
    34 int main()
    35 {
    36     int i,j,ans;
    37 
    38     while(cin>>m>>n,m+n)
    39     {
    40         ans=0;
    41 
    42         for(i=0; i<m; i++)
    43             scanf("%s",maps[i]);
    44         for(i=0; i<m; i++)
    45         {
    46             for(j=0; j<n; j++)
    47             {
    48                 if(maps[i][j]=='@')
    49                 {
    50                     dfs(i,j);
    51                     ans++;
    52                 }
    53             }
    54         }
    55         printf("%d
    ",ans);
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    php实现简单的流程管理
    【百度地图API】如何制作多途经点的线路导航——驾车篇
    利用MFC实现浏览器的定制与扩展(JavaScript与C++交互)
    c++与js脚本交互,C++调用JS函数/JS调用C++函数
    VC/MFC中通过CWebPage类调用javascript函数(给js函数传参,并取得返回值)
    Android中半透明Activity效果另法
    mac java环境
    在Mac osx使用ADT Bundle踩过的坑
    Android自动检测版本及自动升级
    C++编译遇到参数错误(cannot convert parameter * from 'const char [**]' to 'LPCWSTR')
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4344198.html
Copyright © 2011-2022 走看看