题干:

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. Zabbix 3.2.6 通过SNMP和iDRAC监控DELL服务器
  2. R语言使用pwr包的pwr.t.test函数对分组样本数相同的t检验进行效用分析(power analysis)、在已知效应量(effect size)、显著性水平、效用值的情况下计算需要的样本量
  3. 【消息中间件】浅谈中间件优缺RabbitMQ基本使用
  4. 懒惰和贪婪-正则回溯
  5. #9 case while until select语句的运用与例子
  6. 服装色差的识别成因与预防
  7. 远程调试运行在Resin上面的Web应用程序
  8. [转]网页板块设计研究
  9. 阿尔法贝塔剪枝——中国象棋人机对战
  10. html页面获取扫码枪参数,js获取扫码枪输入数据的方法
  11. postgres 禁止远程登录_Postgresql允许远程访问配置修改
  12. web -- 背景图片及文字
  13. python大鱼吃小鱼
  14. Java设计模式之——策略模式(Strategy)
  15. 手机扫描条形码二维码原理和实现等网上资料整理
  16. 树莓派制作路由器,手机连接wifi
  17. 从刘老师的进化的力量到有感,疫情阶段如何弯道超车
  18. How Gradual Typing System Helps Us
  19. 计算机开机图片怎么换,如何把电脑开机画面换成自己的图片?
  20. Flink实操 : 状态管理

热门文章

  1. [Leedcode][JAVA][第102题][二叉树的层序遍历][递归][迭代][BFS]
  2. 量子计算机具有天热的,量子绝热计算
  3. php 红包算法,PHP语言:实现微信红包拆分算法
  4. 状态模式和策略模式的区别
  5. 使用junit+mockito进行mock测试实例
  6. Asterisk拨号方案中变量的应用
  7. LDR 、ADR介绍
  8. mysql的seq2_DESeq2处理TCGA数据库Seq-count数据
  9. directx最终用户运行时_运维定位服务故障时,前5分钟都在忙啥?
  10. 【转】DICOM命令集和数据集解析!!