油田!

 

题意:

   

     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

思路分析:
 题目很长,但是从案例我们可以看到就是找连通块,所以我们用DFS深搜。
1、从第一行第一列A开始搜索,满足条件(是@而且没有搜索过这个地方)继续下一步
2、接着把找到的A设置为搜索过,再搜索它的周围(八个方向)
3、每找到一个A需要判断是否越界:不是@或者已经出界。



源代码:


 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 using namespace std;
 5 char chess[110][110];
 6 int vis[110][110];
 7 int a, b;
 8 void dfs(int x,int y)
 9 {
10     if (x < 0 || y < 0 || x >= a || y >= b)   return;               //越界条件不能丢
11     if (chess[x][y] == 0 || chess[x][y] == '*' || vis[x][y] == 1)    //越界
12         return;
13     vis[x][y] = 1;                //表示已经搜索过
14     dfs(x - 1, y - 1); dfs(x - 1, y );  dfs(x - 1, y +1);
15     dfs(x , y - 1);                     dfs(x , y+1 );           //遍历八个方向
16     dfs(x +1, y - 1);  dfs(x + 1, y );  dfs(x+ 1, y + 1);
17 }
18 
19 
20 int main()
21 {
22     int i,j;
23     while (cin>>a>>b)
24     {
25         memset(chess, 0, sizeof(chess));                  //每次开始要对上一次的数据清零
26          memset(vis, 0, sizeof(vis));
27         if (a == 0 || b == 0)
28             break;                                   //结束条件
29         int count = 0;                               //连通块数
30         for (i = 0; i < a; i++)
31             for (j = 0; j < b; j++)
32             {
33             cin >> chess[i][j];
34             }
35 
36         for (i = 0; i < a; i++)
37             for (j = 0; j < b; j++)
38             {
39             if (chess[i][j] == '@'&&vis[i][j]==0)           //目标锁定,且没有搜索过
40             {                                                       
41                 dfs(i, j);                               
42                 ++count;
43             }
44             }
45 
46         cout << count << endl;
47     }
48     
49         return 0;
50 }

          

 

     心得:

             题目跟之前做得种子填充题目一样,只是当时没有加上这句越界条件,但是那个题目没有这个条件通过了,这个题却不行,有待研究经过几个题目的联系,找连通块的问题没之前想象的那么复杂了,是一种进步,加油~

1 if (x < 0 || y < 0 || x >= a || y >= b)  
2        return;   

 








 

posted @ 2015-07-29 19:29  白一  阅读(188)  评论(0编辑  收藏  举报