1. 程式人生 > >HDU 1241 Oil Deposits 題解

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 Mid-Central USA 1997  

 

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\n",x);
86     }
87     return 0;
88 }
  ------------------------------------------------------------------------------------------------------------------------------------------------------------------- Notes:DFS的方法暫時還沒有想好,但博主認為這樣的方法更好 2018-11-20  02:11:33  Author:LanceYu