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 }
  • 相关阅读:
    supervisorctl的安装使用
    react应用多入口配置
    百度编辑器
    formData文件上传
    thymeleaf的内联th:inline(在javascript访问model中的数据)
    浅谈Object.prototype.toString.call()方法
    JS中call()和apply()以及bind()的区别
    Json对象与Json字符串的转化
    全面解析JavaScript中“&&”和“||”操作符(总结篇)
    TortoiseSVN客户端重新设置用户名和密码
  • 原文地址:https://www.cnblogs.com/wydxry/p/10564981.html
Copyright © 2011-2022 走看看