题目:

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

题意描述:
输入n和m为矩阵的大小(1 <= m <= 100 and 1 <= n <= 100),以及n*m的矩阵
计算并输出该矩阵中@的组成的油田有多少个(上下左右斜对角都能相连)
解题思路:
遍历字符数组,遇到'@'对其进行一遍深搜,搜索过程中标记已经走过的地方,直到遍历完所有元素。
代码实现:
 #include<stdio.h>
#include<string.h>
char map[][];
int m,n,book[][];
void dfs(int x,int y,int c);
int main()
{
int i,j,num;
while(scanf("%d%d",&m,&n),m||n)
{
memset(map,'',sizeof(map));//初始化的位置
for(i=;i<=m;i++)
{
for(j=;j<=n;j++)
scanf(" %c",&map[i][j]);//处理行列数后多余的空格
getchar();
}
memset(book,,sizeof(book));
num=;
for(i=;i<=m;i++)
{
for(j=;j<=n;j++)
{
if(map[i][j]=='@'&&book[i][j]==)
{
num--;
book[i][j]=num;
dfs(i,j,num);
}
}
}
printf("%d\n",-num);
}
return ;
}
void dfs(int x,int y,int c)
{
int next[][]={,,,,,,,-,,-,-,-,-,,-,};//注意搜索的方向个数
int k,tx,ty;
book[x][y]=c;
for(k=;k<=;k++)
{
tx=x + next[k][];//位置在原来的基础上变化
ty=y + next[k][];
if(tx<||tx>m||ty>n||ty<)
continue;
if(map[tx][ty]=='@' && book[tx][ty]==)
{
book[tx][ty]=c; dfs(tx,ty,c);
}
}
return ;
}

易错分析:

1、处理地图的时候注意吃掉空格

2、注意搜索方向的个数

3、tx要在原来的基础上进行变化

HDU 1562 Oil Deposits的更多相关文章

  1. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  2. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  3. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...

  4. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  5. DFS(连通块) HDU 1241 Oil Deposits

    题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...

  6. HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题

    Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...

  7. hdu 1241:Oil Deposits(DFS)

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

  8. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

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

  9. HDU 1241 Oil Deposits (DFS/BFS)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

随机推荐

  1. css各种布局

    1.水平居中 前提:父容器.parent 和子容器.child 1)使用text-align和inline-block .parent{text-aling:center}; .child {disp ...

  2. 队列详解及java实现

    导读 栈和队列是有操作限制的线性表. 目录 1.队列的概念.特点.存储结构. 2.栈队列的java实现. 概念 队列是一种在一端进行插入,而在另一端进行删除的线性表.1.队列的插入端称为队尾:队列的删 ...

  3. Head First设计模式之桥接模式

    一.定义 桥接模式(Bridge Pattern),将抽象部分与它的实现部分分离,使的抽象和实现都可以独立地变化. 主要解决:在多维可能会变化的情况下,用继承会造成类爆炸问题,扩展起来不灵活. 何时使 ...

  4. Angular5系列教程:ng-book2-angular-5-r66 土家翻译,话糙理不糙

    嗯, 在工作还辣么忙之时,看了这本书,感觉很不错.想分享给国内朋友们.结合自己的理解和整理加翻译,可能有点糙,但是,话糙理不糙嘛.出系列,不知道会不会弃坑,不立Flag了.持续更新.....我会放在印 ...

  5. 通过 python的 __call__ 函数与元类 实现单例模式

    简单一句话,当一个类实现__call__方法时,这个类的实例就会变成可调用对象. 直接上测试代码 class ClassA: def __call__(self, *args, **kwargs): ...

  6. 理解css伪类和伪元素

    伪类就是可以通过直接添加一个类样式达到同等效果,而伪元素,则需要先添加一个元素,然后在元素上添加样式才能达到同等效果 伪类 :active 向被激活的元素添加样式. :focus 向拥有键盘输入焦点的 ...

  7. zabbix-server端与zabbix-agent端部署与监控

    环境: [root@redis ~]# uname -a Linux redis -.el6.x86_64 # SMP Tue Mar :: UTC x86_64 x86_64 x86_64 GNU/ ...

  8. python3基础(二)

    loops循环语句 一 if语句,if语句配合else使用,可以没有else. 单分支if语句 age = input('Age:') password = '67' if age == passwo ...

  9. 表单验证插件--formvalidation

    表单验证是一个非常基础的功能,当你的表单项少的时候,可以自己写验证,但是当你的表单有很多的时候,就需要一些验证的插件.今天介绍一款很好用的表单验证插件,formvalidation.其前身叫做boot ...

  10. WPF获取窗口句柄的方法

    通过WPF的互操作帮助类WindowInteropHelper,相关连接:https://msdn.microsoft.com/zh-cn/library/system.windows.interop ...