题目:

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. ES6 正则的扩展

    1. RegExp构造函数 ES5中,RegExp构造函数的参数: 参数是字符串,这时第二个参数表示正则表达式的修饰符(flag) 参数是一个正则表示式,这时会返回一个原有正则表达式的拷贝.但是,ES ...

  2. Qt编写QUI皮肤生成器

    用Qt写项目写多了,为了满足不同客户的需求,需要定制不同样式的界面,QUI皮肤生成器应运而生.思考这个工具的架构花了一年时间,如何从复杂的配色方案中提取出共性,然后将共性转为具体的QSS文件.思考架构 ...

  3. Windows 7样式地址栏(Address Bar)控件实现

    介绍 从Vista开始,地址栏就有了很大的改变,不知道大家有什么感觉,笔者觉得很方便,同时又兼容之前的功能,是个很不错的创新.不过,微软并不打算把这一很酷的功能提供给广大的开发人员. 本文提供了一个简 ...

  4. Python Tkinter模块 Grid(grid)布局管理器参数详解

    在使用Tkinter模块编写图像界面时,经常用到pack()和grid()进行布局管理,pack()参数较少,使用方便,是最简单的布局,但是当控件数量较多时,可能需要使用grid()进行布局(不要在同 ...

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

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

  6. Tempdb总结

    Tempdb 系统数据库是一个全局资源,可供连接到 SQL Server 实例的所有用户使用,并可用于保存下列各项: 显式创建的临时用户对象,例如全局或局部临时表.临时存储过程.表变量或游标. SQL ...

  7. ubuntu16编译安装mysql5.7

    安装环境: linux版本: ubuntu-16.04.3-desktop-amd64 mysql版本:mysql-boost-5.7.20.tar.gz 下载地址:https://dev.mysql ...

  8. ssh整合开发

    ssh整合思想 ssh整合 第一步:导入ssh相关jar包 第二步:搭建struts环境   (1)创建 action  struts.xml配置文件, 配置action struts.xml约束 & ...

  9. File System 之本地文件系统

    上一篇文章提到了,最近做一个基于 File System/IndexedDB的应用,上一篇是定额和使用的查询. 因为LocalFileSystem只有chrome支持,有点尴尬,如果按需加载又何来尴尬 ...

  10. 在Ubuntu14.04上搭建自己的OpenVPN服务器并通过它上网

    背景 学校宿舍端口可以配置静态IP连校内网,也可以连到实验室的服务器:实验室的服务器可以连外网:但宿舍要连外网就要花钱买PPPoE账号了.作为壮哉我大计院的一员,本着发扬专(neng)业(sheng) ...