hdu-1241 Oil Deposits

 

Oil Deposits

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


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
 

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1241

 

 

题意:

     给你一块地,找石油田。条件:油田必须是一块一块的,也就是由小田连成大田。中间不可有东西隔开,如果油田中间有东西隔开则为两块油田。‘*’为障碍,‘@’为石油。

方法:

     用广搜或者深搜都可以,遍历方向有八个:上下左右,还有斜角线。利用行列矩阵存储网格。一边遍历即可以判断是否存在油田,是则返回true,在主函数判断返回值,根据其真假累计sum的值。最后得到的sum值就是油田数。

此题我用的是BFS广搜。

 

 

View Code
 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4 
 5 const int Max = 150;
 6 char used[Max][Max];
 7 int to[8][2]={-1,0,1,0,0,-1,0,1,-1,-1,-1,1,1,-1,1,1};
 8 int h, w; //行、列
 9 
10 typedef struct 
11 {
12     int x, y;
13 }Node;
14 
15 int BFS(int x, int y)
16 {
17     int flag = 0;
18     queue<Node>Q;
19     Node go;
20     go.x = x;
21     go.y = y;
22     if(used[x][y] != '@')
23         return 0;    
24     used[x][y] = '*';
25     flag = 1;
26     Q.push(go);
27     while(!Q.empty())
28     {
29         go=Q.front();
30         Q.pop();
31         x = go.x;
32         y = go.y;
33         for(int i=0; i<8; i++)
34         {
35             go.x = x+to[i][0];
36             go.y = y+to[i][1];
37             if(used[go.x][go.y]=='@' && go.x>=0 && go.x<h && go.y>=0 && go.y<w )
38             {
39                 used[go.x][go.y] = '*';
40                 Q.push(go);
41             }
42         }
43     }
44     return flag;
45 }
46 
47 int main()
48 {
49     int x, y;
50     while(cin>>h>>w && (w+h)>0)
51     {
52         int i, j;
53         for(i=0; i<h; i++)
54         {
55             scanf("%s", &used[i]);
56         }
57         int sum = 0;
58         for(i=0; i<h; i++)
59             for(j=0; j<w; j++)
60             {
61                 if(BFS(i, j) == 1)
62                     sum++;
63             }
64         cout<<sum<<endl;
65     }
66     return 0; 
67 }

 

View Code
 1 /*
 2    HDUACM  diy  zhbit_BFS & DFS
 3    1002 Oil Deposits 
 4    2012.7.04  
 5    12:52
 6 */
 7 
 8 #include<iostream>
 9 #include<queue>
10 using namespace std;
11 
12 int h,w;
13 int to[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}};
14 char point[100][100];
15 
16 struct pp
17 {
18     int x;
19     int y;
20 };
21 
22 int BFS(int x,int y)
23 {
24     int flag = 0;
25 
26     if(point[x][y] == '@')
27     {
28             flag = 1;
29             point[x][y] = '*';
30     }
31     else
32         return 0;
33 
34     pp front,a,go;
35     queue<pp>q;
36     a.x = x;
37     a.y = y;
38     q.push(a);
39     
40     while(!q.empty())
41     {
42         front = q.front();
43         q.pop();
44         for(int i=0; i<8; i++)
45         {
46             go.x = front.x + to[i][0];
47             go.y = front.y + to[i][1];
48             if(go.x>=0 && go.x<h && go.y>=0 && go.y<w && point[go.x][go.y] == '@')
49             {
50                 q.push(go);
51                 point[go.x][go.y] = '*';
52             }
53         }
54     }
55     return flag;
56 }
57 
58 int main()
59 {
60     int i,j;
61     int sum;
62     while(cin>>h>>w && h+w)
63     {
64         sum = 0;
65         for(i=0; i<h; i++)
66         {
67             cin>>point[i];
68         }
69         for(i=0; i<h; i++)
70         {
71             for(j=0; j<w; j++)
72             {
73                 if(BFS(i,j) == 1)
74                     sum++;
75             }
76         }
77         cout<<sum<<endl;
78     }
79     return 0;
80 }

 

 DFS

View Code
 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 const int Max = 105;
 5 char map[Max][Max];
 6 bool used[Max][Max];
 7 int n,  m;
 8 int flag;
 9 int to[8][2] = {0,-1, 0,1, 1,0, -1,0, 1,1, -1,-1, 1,-1, -1,1};
10 
11 int DFS(int x, int y)
12 {
13     int a, b;
14 /*    if(map[x][y] != '@')
15     {
16         used[x][y] = true;
17         //return flag;
18     }*/
19 //    flag = 1;
20     for(int i=0; i<8; i++)
21     {
22         a = x+to[i][0];
23         b = y+to[i][1];
24         if(a>=0 && a<n && b>=0 && b<m && !used[a][b] && map[a][b]=='@')
25         {    
26             used[a][b] = true;
27             DFS(a, b);
28             //used[a][b] = false;
29         }
30     }
31     return flag;
32 }
33 
34 
35 int main()
36 {
37     while(cin>>n>>m && n+m)
38     {
39         memset(used, false, sizeof(used));
40         memset(map, 0, sizeof(map));
41         int i, j;
42         int sum = 0;
43         for(i=0; i<n; i++)
44         {
45             scanf("%s", map[i]);
46         }
47         flag = 0;
48         for(i=0; i<n; i++)
49         {
50             for(j=0; j<m; j++)
51             {
52                 if(map[i][j] == '*')
53                 {
54                     used[i][j] = true;
55                     //return flag;
56                 }
57                 else if(!used[i][j])
58                 {    
59                     DFS(i, j);
60                     //if(flag)
61                         sum++;
62                 }
63             }
64         }
65         cout<<sum<<endl;
66     }
67     return 0;
68 }

 

posted @ 2012-07-04 13:40  另Ⅰ中Feel▂  阅读(157)  评论(0编辑  收藏  举报