zoukankan      html  css  js  c++  java
  • hdu 1241

    Oil Deposits
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 48366    Accepted Submission(s): 27816
    
    
    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
     
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <string>
     5 #include <string>
     6 #include <cstring>
     7 #include <map>
     8 #include <utility>
     9 using namespace std;
    10 const int  N = 1e2+20 ;
    11 int m,n;
    12 char s[N][N];
    13 int flag,ans;//flag定义为全局变量 
    14 int dir[8][2]={
    15 {0,1},{0,-1},{1,0},{-1,0},
    16 {-1,1},{1,1},{-1,-1},{1,-1}
    17 };
    18 bool check(int r,int l){
    19     if(r>=0&&r<m&&l>=0&&l<n){
    20         return true;
    21     }
    22     return false;
    23 }
    24 bool   dfs(int  r,int l){
    25     if(s[r][l]=='*') return flag;
    26     flag=1;
    27     s[r][l]='*';//避免重复 
    28     for(int i=0;i<8;i++){
    29             int ii=r+dir[i][0];
    30             int jj=l+dir[i][1];
    31             if(check(ii,jj)){
    32                 dfs(ii,jj);//把和这个石油是同一种的全变为* 
    33             }
    34     }
    35     return flag;//一定要return  
    36 }
    37 int  main()
    38 {
    39     while(~scanf("%d%d",&m,&n))
    40     {
    41         if(n==0&&m==0) break;
    42         for(int i=0;i<m;i++)
    43         scanf("%s",s[i]);
    44         ans=0;
    45         for(int i=0;i<m;i++){
    46             for(int j=0;j<n;j++){
    47                 flag=0;
    48                 if(dfs(i,j)){//找到一种石油 
    49                     ans++;
    50                 }
    51             }
    52         }
    53             printf("%d
    ",ans);
    54     }
    55   return  0;
    56 }
  • 相关阅读:
    SpannaleString总结
    【未完成】bug记录2013427>import工程时出现Build path contains duplicate entry:'src' for project 'XXX'
    【未完成】给eclipse项目改名
    android创建和删除桌面快捷方式
    bug记录2013426(2)>Select at least one project错误
    hosts配置
    获取当前应用的版本号及android系统版本号及手机型号
    转载:如何将offcie 2003文档(.doc、.xls、.ppt)转换成mht文档
    转载:.NET2.0 验证控件常用的正则表达式
    转载: RESTORE DATABASE命令还原SQLServer 2005 数据库
  • 原文地址:https://www.cnblogs.com/tingtin/p/10496095.html
Copyright © 2011-2022 走看看