油田!

 

题意:

   

     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;   

 








 

转载于:https://www.cnblogs.com/Lynn0814/p/4686975.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值