zoukankan      html  css  js  c++  java
  • Hdu1241 Oil Deposits (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
     
    Source
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 char a[105][105];
     4 int n,m,res;
     5 int dx[4]={1,-1,0,0};
     6 int dy[4]={0,0,1,-1};
     7 void dfs(int x,int y)
     8 {
     9     a[x][y]='*';
    10     for(int i=-1;i<=1;i++){
    11         for(int j=-1;j<=1;j++){
    12             int nx=x+i,ny=y+j;
    13             if(nx>=0&&nx<n&&ny>=0&&ny<m&&a[nx][ny]=='@'){
    14                 dfs(nx,ny);
    15             }
    16         }
    17     }
    18     return ;
    19 }
    20 void solve()
    21 {
    22     res=0;
    23     for(int i=0;i<n;i++){
    24         for(int j=0;j<m;j++){
    25             if(a[i][j]=='@'){
    26                 dfs(i,j);
    27                 res++;
    28             }
    29         }
    30     }
    31 }
    32 int main() {
    33     while(cin>>n>>m&&(n&&m)){
    34         for(int i=0;i<n;i++){
    35             for(int j=0;j<m;j++){
    36                 cin>>a[i][j];
    37             }
    38         }
    39         res=0;
    40         solve();
    41         cout<<res<<endl;    
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    【转】Java HotSpot(TM) 64-Bit Server VM warning: Insufficient space for shared memory file:
    kafka之partition分区及副本replica升级
    Rokid开发者社区skill之【历史上的今天】之简介+玩法+设计+实现+心得
    【转】批量删除redis中的key
    shell实现除法,保留小数点后N位
    [Django学习]中间件
    [Django学习]静态文件处理
    [Django学习]模板
    [Django学习]视图
    [Django学习]模型
  • 原文地址:https://www.cnblogs.com/shixinzei/p/10564981.html
Copyright © 2011-2022 走看看