Oil Deposits

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 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

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


题意:
求油田的个数;
与八连块的题目极为相似。。。
这他妈不知到碰到过多少次
是个人都会做。。日狗去吧。。
 1 #include<iostream>
 2 using namespace std;
 3 int n,m;
 4 char grid[105][105];//存储网格;
 5 int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{0,-1},{1,1},{1,0},{1,-1}};
 6 void DFS(int x,int y)//从(x,y)位置进行DFS
 7 {
 8     int i,xx,yy;
 9     grid[x][y]='*';//进过后设置成*保证不会在经过了
10     for(i=0;i<8;i++)
11     {
12         xx=x+dir[i][0];
13         yy=y+dir[i][1];
14         if(xx>=n||yy>=m||xx<0||yy<0)//判断
15         continue;
16         if(grid[xx][yy]!='*')
17         DFS(xx,yy);
18     }
19 }
20 int main()
21 {
22     int i,j;
23     int count;//统计数目
24     while(cin>>n>>m,n)
25     {
26         cin.get();
27         for(i=0;i<n;i++)
28         {
29             for(j=0;j<m;j++)
30             cin>>grid[i][j];
31             cin.get();
32         }
33         count=0;
34         for(i=0;i<n;i++)
35         {
36             for(j=0;j<m;j++)
37             {
38                 if(grid[i][j]=='@')
39                 {
40                     DFS(i,j);//从(i,j)位置进行DFS;
41                     count++;
42                 }
43             }
44         }
45         cout<<count<<endl;
46     }
47     return 0;
48 } 
View Code
原文地址:https://www.cnblogs.com/demodemo/p/4690647.html