题干:

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中一定要先maze[][]='*',而不能后序赋值, 这样就会进入死循环,第二个样例就可以很明显的看出来.

AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n,m;
char maze[105][105];
int nx[8] = {0,1,1,1,0,-1,-1,-1};
int ny[8] = {1,1,0,-1,-1,-1,0,1};
void dfs(int x,int y) {if(x < 1 ||x > n || y <1 || y > m) return ;
//  if(maze[x][y] == '*') return ;for(int k = 0; k<8; k++) {int tx = x+nx[k];int ty = y+ny[k];if(maze[tx][ty] == '@') {maze[tx][ty] = '*';dfs(tx,ty);}}
//  maze[x][y] = '*';}
int main()
{while(cin>>n>>m) {if(n == 0 && m == 0) break;int cnt = 0;for(int i = 1; i<=n; i++) {scanf("%s",maze[i] + 1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == '@') {maze[i][j] = '*';dfs(i,j);cnt++;}}}cout << cnt <<endl;}return 0 ;} 

【POJ - 1562】Oil Deposits (dfs搜索,连通块问题)相关推荐

  1. UVa572 Oil Deposits DFS求连通块

    技巧:遍历8个方向 for(int dr = -1; dr <= 1; dr++)for(int dc = -1; dc <= 1; dc++)if(dr != 0 || dc != 0) ...

  2. DFS求连通块数目(深搜)

    DFS求连通块数目 这里认为,连通块是包括斜对角线的路径连通的块. 测试数据 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 计算通过@相连的连通块的个数 测试输出: 2 样例代码 ...

  3. [蓝桥杯2018初赛]全球变暖-dfs,bfs,连通块

    解题思路: bfs:遍历所有未遍历过的陆地,通过bfs计算出当前位置连通陆地的数量cnt,以及被淹没陆地的数量bound,若cnt == bound表示完整淹没的一个岛屿 dfs:将连通块全部标记,如 ...

  4. [uva]AncientMessages象形文字识别 (dfs求连通块)

    非常有趣的一道题目,大意是给你六种符号的16进制文本,让你转化成二进制并识别出来 代码实现上参考了//http://blog.csdn.net/u012139398/article/details/3 ...

  5. HDU-1241 Oil Deposits (DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  6. UVA572 Oil Deposits DFS求解

    小白书上经典DFS题目. 1. 递归实现 // from: https://www.cnblogs.com/huaszjh/p/4686092.html#include <stdio.h> ...

  7. 【LeetCode - 1254】统计封闭岛屿的数目(dfs,连通块)

    题目链接:https://leetcode-cn.com/problems/number-of-closed-islands/ 有一个二维矩阵 grid ,每个位置要么是陆地(记号为 0 )要么是水域 ...

  8. *【ZOJ - 3781】Paint the Grid Reloaded(dfs求连通块缩点,bfs求最短路,建图技巧)

    题干: Leo has a grid with N rows and M columns. All cells are painted with either black or white initi ...

  9. POJ 2386 dfs求连通块

    题目: 由于近期的降雨,雨水汇集在农民约翰的田地不同的地方.我们用一个 的网格图表示.每个网格中有水(W) 或是旱地(.).一个网格与其周围的八个网格相连,而一组相连的网格视为一个水坑.约翰想弄清楚他 ...

  10. UVa 352 - The Seasonal War ( DFS求连通块 )

    思路 基础DFS 和油田那题思路一毛一样 AC代码 #include <iostream> #include <cstdio> #include <cstring> ...

最新文章

  1. 解题报告(十五)莫比乌斯反演与积性函数(ACM / OI)
  2. 大家都能读懂的IT生活枕边书
  3. 凡科虚拟服务器怎样做301,虚拟主机301重定向怎么做?网站301重定向方法之一
  4. angularjs的双向绑定原理实现
  5. 结构体位域及联合体解析
  6. jenkins构建记录日志_构建企业日志记录层的清单
  7. android gdb 远程调试工具,gdb输入/输出错误远程调试到Android
  8. 【论文】Awesome Relation Extraction Paper(关系抽取)(PART V)
  9. Qt linux双屏,qt5 多屏显示
  10. app用http3与服务器_mqtt服务器搭建以及客户端Paho安装使用
  11. c语言文件包含试题,C语言文件练习题含答案
  12. Flash Player离线安装包下载
  13. AD查找相似对象使用进阶
  14. NPDP 产品经理国际资格认证
  15. 亲测豆果美食7.0版:美食食谱App的沉浸新味道
  16. 标准盒模型怪异盒模型
  17. java web视频学习-ServletContext和Request和Response
  18. JS高级笔记:CommonJs与ESModule的区别
  19. 【操作系统】进程-哲学家进餐问题
  20. c语言学生管理系统中人数,python下学生管理系统:从文件中读取30位学生的信息(含邮箱),并实现简单的增、删、查找、统计(邮箱使用人数)。---附程序哦!...

热门文章

  1. CodeForces - 888C K-Dominant Character 思维
  2. linux系统中使用pycharn,在pycharm中使用linux控制台
  3. x-lite for linux,Linux Lite 4.6正式发布:现基于Ubuntu 18.04.3 LTS
  4. springboot中获取bean_最新Spring Boot干货总结(超详细,建议收藏)
  5. python卸载opencv_20.Windows python,opencv的安装与卸载
  6. numpy数组按某一维度相加_Python数据分析之NumPy(高级篇)
  7. linux 4.9 内核 nptl,【linuxThread和NPTL】
  8. git 合并冲突_GIT提交记录和Revert commit过程分析
  9. Package ‘*****‘ has no installation candidate
  10. mysql 导致iis 假死_php使用MySql函数导致Apache(iis)崩溃的问题解决方案