zoukankan      html  css  js  c++  java
  • HDU 1241 Oil Deposits 题解

    Oil Deposits

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


    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
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------
    本题应为DFS的题目
    但由于博主学DFS学的比较菜
     
    所以博主没有采用DFS
    而是采用了多次BFS的方式
    逐行逐列搜索map
    一旦找到一个@就向八个方向BFS一次
    每次BFS的时候把@变为*
    这样搜完整片map
    就结束
    下面上我全是注释
    谁都看的懂的代码
     
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------
     1 //Author:LanceYu
     2 #include<iostream>
     3 #include<string>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<fstream>
     7 #include<iosfwd>
     8 #include<sstream>
     9 #include<fstream>
    10 #include<cwchar>
    11 #include<iomanip>
    12 #include<ostream>
    13 #include<vector>
    14 #include<cstdlib>
    15 #include<queue>
    16 #include<set>
    17 #include<ctime>
    18 #include<algorithm>
    19 #include<complex>
    20 #include<cmath>
    21 #include<valarray>
    22 #include<bitset>
    23 #include<iterator>
    24 #define ll long long
    25 using namespace std;
    26 const double clf=1e-8;
    27 //const double e=2.718281828;
    28 const double PI=3.141592653589793;
    29 const int MMAX=2147483647;
    30 //priority_queue<int>p;
    31 //priority_queue<int,vector<int>,greater<int> >pq;
    32 int dir[8][2]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,1},{1,-1}};//此处应为八个方向 
    33 int n,m;
    34 char a[101][101];
    35 struct node
    36 {
    37     int x,y;
    38 };
    39 void bfs(int x,int y)
    40 {    
    41     int i;
    42     queue<node> q;
    43     node g;
    44     g.x=x;g.y=y;
    45     q.push(g);
    46     a[x][y]='*';
    47     while(!q.empty())
    48     {
    49         node t=q.front();
    50         q.pop();
    51         for(i=0;i<8;i++)//八个方向寻找是否成片 
    52         {
    53             int dx=t.x+dir[i][0];
    54             int dy=t.y+dir[i][1];
    55             if(dx>=0&&dy>=0&&dx<m&&dy<n&&a[dx][dy]=='@')//如果出现@,变为*(防止下面重复运算) 
    56             {
    57                 a[dx][dy]='*';
    58                 g.x=dx;g.y=dy;
    59                 q.push(g);
    60             }
    61         }
    62     }
    63 }
    64 int main()
    65 {
    66     int x;
    67     while(scanf("%d%d",&m,&n)!=EOF)
    68     {
    69         if(m==0&&n==0) 
    70             return 0;
    71         x=0;//初始化 
    72         for(int i=0;i<m;i++)
    73             scanf("%s",a[i]);
    74         for(int i=0;i<m;i++)
    75         {
    76             for(int j=0;j<n;j++)
    77             {
    78                 if(a[i][j]=='@')//找到一个@就bfs一下是否成一片,并且消除它们 
    79                 {
    80                     bfs(i,j);
    81                     x++;
    82                 }
    83             }
    84         }
    85         printf("%d
    ",x);
    86     }
    87     return 0;
    88 }
     
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Notes:DFS的方法暂时还没有想好,但博主认为这样的方法更好
    2018-11-20  02:11:33  Author:LanceYu
  • 相关阅读:
    Python学习札记(十五) 高级特性1 切片
    LeetCode Longest Substring Without Repeating Characters
    Python学习札记(十四) Function4 递归函数 & Hanoi Tower
    single number和变体
    tusen 刷题
    实验室网站
    leetcode 76. Minimum Window Substring
    leetcode 4. Median of Two Sorted Arrays
    leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions 、434. Number of Islands II(lintcode) 并查集 、178. Graph Valid Tree(lintcode)
    刷题注意事项
  • 原文地址:https://www.cnblogs.com/lanceyu/p/9986846.html
Copyright © 2011-2022 走看看