zoukankan      html  css  js  c++  java
  • hdu 1241:Oil Deposits(DFS)

    Oil Deposits

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 4   Accepted Submission(s) : 3

    Font: Times New Roman | Verdana | Georgia

    Font Size:  

    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

    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

    Mid-Central USA 1997

     
      这是道经典dfs搜索题,难度属于入门级,没有太难的地方。
      下面是代码:
     1 #include <iostream>
     2 using namespace std;
     3 char a[101][101];
     4 int dx[8] = {0,1,1,1,0,-1,-1,-1};
     5 int dy[8] = {1,1,0,-1,-1,-1,0,1};
     6 int m,n;
     7 void dfs(int x,int y)    //将(x,y)坐标有联系的所有坐标都标记为 * 
     8 {
     9     a[x][y]='*';
    10     for(int i=0;i<8;i++){
    11         int nx = x+dx[i];
    12         int ny = y+dy[i];
    13         if( nx<1 || ny<1 || nx>m || ny>n)    //如果越界 
    14             continue;
    15         if(a[nx][ny]!='@')    //如果这样走的下一步不是 @ 
    16             continue;
    17         dfs( nx , ny );
    18     }
    19 }
    20 int main()
    21 {
    22     while(cin>>m>>n,m!=0){
    23         int _count=0;
    24         for(int i=1;i<=m;i++)
    25             for(int j=1;j<=n;j++)
    26                 cin>>a[i][j];
    27         for(int i=1;i<=m;i++)
    28             for(int j=1;j<=n;j++){
    29                 if(a[i][j]=='@'){
    30                     _count++;
    31                     dfs(i,j);
    32                 }
    33             }
    34         cout<<_count<<endl;
    35     }
    36     return 0;
    37 }

      

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    Debian vim没有颜色的解决办法
    第四次作业
    第三次作业
    第二次作业
    Mad Libs游戏 & 华氏温度与摄氏温度转换
    有趣故事
    Mybatis的使用及增删改查
    jdbc数据连接池
    详解MySQL数据类型
    SQL语法大全
  • 原文地址:https://www.cnblogs.com/yym2013/p/3519282.html
Copyright © 2011-2022 走看看