• 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 }
  • 相关阅读:
    Oracle中常见的33个等待事件小结
    DATAGUARD中手工处理日志v$archive_GAP的方法
    ORACLE 如何定位消耗资源的SQL
    ORACLE 全局索引和本地索引
    Oracle中获取执行计划的几种方法分析
    BUFFER CACHE之主要的等待事件
    查看tablespace实际使用量和剩余空间
    Linux环境配置文件的理解
    Shell 传递参数
    Linux开局配置注意事项
  • 原文地址:https://www.cnblogs.com/shixinzei/p/10564981.html
Copyright © 2020-2023  润新知