题目:

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. OpenStack运维(四):OpenStack备份恢复

    1.备份注意事项 要保留多少备份? 是否需要异地备份? 备份间隔多久? 恢复策略? 2.备份什么 2.1 数据库备份 2.1.1  制定crond 每天备份一次 2.1.2 备份命令根据系统而定,可用 ...

  2. asp.net MVC里的 ModelState使用方法

    https://www.cnblogs.com/hohoa/p/5839993.html if (!ModelState.IsValid) { string error = string.Empty; ...

  3. leetcode — word-break

    import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * Source : https://oj.l ...

  4. js 对象的值传递

    一.变量赋值的不同 1.原始值 在将一个保存着原始值的变量复制给另一个变量时,会将原始值的副本赋值给新变量,此后这两个变量是完全独立的. 2.引用值: 在将一个保存着对象内存地址的变量复制给另一个变量 ...

  5. File API文件操作之FileReader

    近来研究点对点的文件传输,想到一种方案FileReader+WebRtc. 当我看到FileReader的时候,哎呀,不错的东西啊,仔细一看属于File API,或者叫做Web API. File A ...

  6. Fineui js getText

    需要自己写js的时候.需要取值. var ddlgenderid='<%=ddlgender.clientID%>';function reader(value){f(ddlgenderi ...

  7. Core Animation 文档翻译 (第一篇)

    Core Animation 文档翻译(第一篇)   2018-01-13  星期6 前言:作为iOS 开发,官方文档的阅读是很有必要的,值此周末便写下此文.作为iOS 实际经验3年的开发,之前的应用 ...

  8. python 学习源码练习(1)

    #编译方式,python3 文件名 #!/usr/bin/python3#print('hello world') mystring = 'hello world'print (mystring) # ...

  9. SQL Server 插入含有中文字符串出现乱码现象的解决办法

    ELECT  COLLATIONPROPERTY('Chinese_PRC_Stroke_CI_AI_KS_WS', 'CodePage')       --查询SQLServer编码格式的语句 下面 ...

  10. ZooKeeper 状态机

    ZAB状态机 QuorumPeer这个线程负责状态机的维护 @Override public void run() { ... try { /* * Main loop */ while (runni ...