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 }
  • 相关阅读:
    记录一次.Net框架Bug发现和提交过程:.Net Framework和.Net Core均受影响
    浅谈 Angular 项目实战
    Angular CLI 升级 6.0 之后遇到的问题
    构建具有用户身份认证的 Ionic 应用
    关于 Angular 跨域请求携带 Cookie 的问题
    使用 ng build 构建后资源地址引用错误的问题
    React 系列教程 1:实现 Animate.css 官网效果
    如何在已有的 Web 应用中使用 ReactJS
    关于浏览器后退操作与页面缓存问题
    三阶魔方公式速记
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4344198.html
Copyright © 2011-2022 走看看