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 }
  • 相关阅读:
    Nutch的配置(使用MySQL作为数据存储)
    MySQL简单实现多字段模糊查询
    nutch的一些基础整理
    Java分布式爬虫Nutch教程——导入Nutch工程,执行完整爬取
    Nutch2 WebPage写入数据库的过程分析
    Nutch2 WebPage 字段解释
    nutch如何修改regex-urlfilter.txt爬取符合条件的链接
    Run Nutch In Eclipse on Linux and Windows nutch version 0.9
    Linux Mint 17.1 安装全配置
    Ubuntu(Linux Mint):sudo apt-get upgrade升级失败
  • 原文地址:https://www.cnblogs.com/shixinzei/p/10564981.html
Copyright © 2011-2022 走看看