zoukankan      html  css  js  c++  java
  • hdu 1241(DFS/BFS)

    Oil Deposits

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 38801    Accepted Submission(s): 22518


    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
     
    Recommend
    Eddy
     
    Statistic | Submit | Discuss | Note

    思路:'@'代表有石油,然后只要上下相邻或者左右相邻,或者对角相邻都算是一块石油,问有几块石油储备。

    就是用dfs或者bfs就行了,得用栈或者队列来模拟。我写的是DFS版

    不知道为什么在用scanf读入字符数组时,题目的第四组测试数据总是读入回车。。。getchar也不管用。

    换cin好了。。。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <stack>
     5 #include <queue>
     6 #include <conio.h>
     7 
     8 using namespace std;
     9 
    10 typedef struct info{
    11     int i,j;
    12 }location;
    13 
    14 int m,n;
    15 int countt;
    16 char t;
    17 char oilmap[105][105];
    18 stack<location> s;
    19 
    20 int calx[8]={-1,-1,-1,0,0,1,1,1};
    21 int caly[8]={-1,0,1,-1,1,-1,0,1};
    22 
    23 void exploit(int i,int j){
    24     location temp={i,j};
    25     location now,next;
    26     s.push(temp);
    27     while(!s.empty()){
    28         now=s.top();
    29         s.pop();
    30         oilmap[now.i][now.j]='*';
    31         for(int k=0;k<8;k++){
    32             next.i=now.i+calx[k];
    33             next.j=now.j+caly[k];
    34             if(next.i>=0 && next.i<m && next.j>=0 && next.j<n){
    35                 if(oilmap[next.i][next.j]=='@'){
    36                     s.push(next);
    37                 }
    38             }
    39         }
    40     }
    41 
    42 }
    43 
    44 int main()
    45 {
    46     while(~scanf("%d %d",&m,&n)){
    47         getchar();
    48         if(m==0) {break;}
    49         for(int i=0;i<m;i++){
    50             for(int j=0;j<n;j++){
    51                 cin>>oilmap[i][j];
    52             }
    53         }
    54         countt=0;
    55         for(int i=0;i<m;i++){
    56             for(int j=0;j<n;j++){
    57                 if(oilmap[i][j]=='@'){
    58                     exploit(i,j);
    59                     countt++;
    60                 }
    61             }
    62         }
    63         printf("%d
    ",countt);
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    DevC++实现调试功能
    DevC++实现调试功能
    《算法竞赛入门经典》计算组合数问题
    C#根据控件名获取控件对象
    C#根据控件名获取控件对象
    C# 使用反射获取私有属性的方法
    C# 使用反射获取私有属性的方法
    JSON转Object的方式
    JSON转Object的方式
    Hadoop: Setting up a Single Node Cluster.
  • 原文地址:https://www.cnblogs.com/TWS-YIFEI/p/8805435.html
Copyright © 2011-2022 走看看