题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241

Oil Deposits

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

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
题目大意:
这个题目就是要查找油田的个数,比如说第二组数据
3 5
*@*@*
**@**             注明:"@"这个与他的几个方向都是@都是可以相连的。所以称为一块油田。输出1。
*@*@*

 这种就很容易想到搜索。这里的一个技巧就是起点就从@开始,其次就是找到@就使其变成*;不过找过的还是要标记为1,否则就会重复,这样不连接的还可以继续搜~
东西还是要反复的咀嚼,不然就会很生,忘得差不多0.0
详见代码。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 #include <cstring>
 5
 6 using namespace std;
 7
 8 int dir[8][2]= {0,1,0,-1,1,0,-1,0,1,1,1,-1,-1,1,-1,-1};
 9 char map[110][110];
10 int a,b,vis[110][110],k;
11
12 struct node
13 {
14     int x,y;
15     int t;
16 } s,ss;
17
18 queue<node>q,qq;
19 int bfs()
20 {
21     while (!q.empty())
22     {
23         s=q.front();
24         q.pop();
25         //vis[s.x][s.y]=1;
26         for (int i=0; i<8; i++)
27         {
28             int x=s.x+dir[i][0];
29             int y=s.y+dir[i][1];
30             //int t=s.t+1;
31             if (x>=0&&x<a&&y>=0&&y<b)
32             {
33                 if (!vis[x][y]&&map[x][y]=='@')
34                 {
35                     ss.x=x;
36                     ss.y=y;
37                     map[ss.x][ss.y]='*';
38                     //vis[x][y]=1;
39                     q.push(ss);
40                 }
41
42             }
43         }
44     }
45 }
46
47 int main ()
48 {
49     while (scanf("%d%d",&a,&b)!=EOF)
50     {
51         memset(vis,0,sizeof(vis));
52         if (a==0&&b==0)
53             break;
54         for (int i=0; i<a; i++)
55         {
56             getchar();
57             for (int j=0; j<b; j++)
58             {
59                 scanf("%c",&map[i][j]);
60             }
61         }
62         //int k=0;
63         for (int i=k=0; i<a; i++)
64         {
65             for (int j=0; j<b; j++)
66             {
67                 if (map[i][j]=='@')
68                 {
69                     k++;
70                     s.x=i;
71                     s.y=j;
72                     map[s.x][s.y]='*';
73                     vis[s.x][s.y]=1;
74                     q.push(s);
75                     bfs();
76                 }
77             }
78         }
79         printf ("%d\n",k);
80     }
81     return 0;
82 }

转载于:https://www.cnblogs.com/qq-star/p/4139845.html

hdu 1241Oil Deposits(BFS)相关推荐

  1. hdu 1241Oil Deposits(dfs模板)

    题目链接-- http://acm.hdu.edu.cn/showproblem.php?pid=1241 首先给出一个n*m的字符矩阵,'*'表示空地,'@'表示油井.问在这个矩阵中有多少组油井区? ...

  2. hdu - 1072 Nightmare(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...

  3. HDU 1242 Rescue BFS+优先队列

    题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1242 #include <stdio.h> #include <stri ...

  4. HDU - 1043 Eight(bfs打表)

    题目链接:点击查看 题目大意:八数码经典问题,给出一个3*3的矩阵,其中随机分布着1~8的数字以及一个空位(我们用x来表示空位),在整个矩阵中,每一次操作都可以将x和他附近的方块互换,问经过多少次操作 ...

  5. hdu 1240(三维bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1240 思路:就是一个简单的bfs,但我搞了好久啊,有一个trick一直没注意到,然后第二组数据就一直过 ...

  6. Tunnels HDU - 4856 (bfs状压dp)

    Tunnels HDU - 4856 Bob is travelling in Xi'an. He finds many secret tunnels beneath the city. In his ...

  7. HDU 1241Oil Deposits---(dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1241 题意:求几个油田 #include "iostream" #include " ...

  8. HDU 2216(简单BFS)

    使用四维数组进行状态判重  注意要用scanf("%s")或者gets  用cin莫名其妙WA  我查了30分钟错都没查出 改一下就好了 FUCK~~~ 这道DK有更有效率的BFS ...

  9. hdu 1495 非常可乐 (bfs)

    非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

最新文章

  1. 伍六七带你学算法 入门篇——最后一个单词的长度
  2. 使用mysql内连接查询年龄_Mysql的连表查询
  3. 如何打造基于比特币现金(BCH)的消费需求
  4. PHP和MySQL处理树状、分级、无限分类、分层数据的方法
  5. .ini文件中的PHP 5.6 error_reporting设置不起作用
  6. Mac电脑配置Alfred、Go2shell、iTerm2+Oh My Zsh
  7. mysql不复制数据_复制/复制数据库而不使用mysqldump
  8. SAP Cloud for Customer(C4C)前台显示的数据是如何从后台读取的
  9. mac 下更新python
  10. COM口总是有惊叹号怎么办
  11. Win10 镜像安装到新固态硬盘两法
  12. 干货 | 腾讯云智能语音行业落地探索与实践
  13. Java 接口编程题练习_JAVA学习日记每天进步一点点之接口再学习和内部类、编程题练习、异常学习...
  14. 第六章第三十一题(金融应用:信用卡号的合法性验证)(Financial: credit card number validation)
  15. linux socket 阻塞服务端 非阻塞客户端,Linux socket非阻塞connect方法
  16. XP下免U盘安装Ubuntu 18.04(持续更新遇到的问题,20200422更新)
  17. 如何在IDEA中操作数据库——导入驱动包
  18. python中pd是什么意思_python pd
  19. python中frame用法_python:pandas中dataframe的基本用法汇总
  20. HBaseAPI——IDEA操作HBase数据库HBase与Hive的集成

热门文章

  1. python工控怎么样_搞工控不了解python,好比雄鹰断了翅膀,理由在这里!
  2. Vue项目中使用 路由导航守卫 处理页面的访问权限
  3. Node 中的path模块
  4. 利用二分法解决 leetcode 378. Kth Smallest Element in a Sorted Matrix
  5. LeetCode 1897. 重新分配字符使所有字符串都相等
  6. LeetCode 528. 按权重随机选择(前缀和+二分查找)
  7. LeetCode 859. 亲密字符串
  8. jsonp跨域原理_Rust 搭建可跨域访问服务器JsonP(一)
  9. tar linux 举例,linux 的tar 命令详解举例
  10. Python中如何在一行里获取多个异常