题目:

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. Java之路第一步——第一行Java代码

    main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法. 名字必须是main: 必须是public static void 类型的: 必须接收一 ...

  2. Git知识总览(一) 从 git clone 和 git status 谈起

    本篇博客是整理git相关知识的第一篇,因为之前一直是用SourceTree对Git的命令行操作用的不是特别熟,于是乎过了一遍ProGit(链接:https://git-scm.com/book/zh/ ...

  3. python爬取大众点评并写入mongodb数据库和redis数据库

    抓取大众点评首页左侧信息,如图: 我们要实现把中文名字都存到mongodb,而每个链接存入redis数据库. 因为将数据存到mongodb时每一个信息都会有一个对应的id,那样就方便我们存入redis ...

  4. ldap命令的使用

    转自:http://blog.chinaunix.net/uid-20690190-id-4085176.html 增:ldapadd 1)选项: -x   进行简单认证 -D   用来绑定服务器的D ...

  5. nvm 装 nodejs 重启终端失效的解决方法

    (1) 安装 nvm wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash (2) ...

  6. IntelliJ IDEA2017.3 激活

    网上IntelliJ IDEA激活方式大多均已失效,目前常用激活方式为License Server 激活: http://idea.imsxm.com/ NOTE: 在上周五2017-12-1那天还是 ...

  7. gitlab markdown支持页面内跳转

    markdown语法: [to_be_link](#id_name) 标题: ## 2.aaa <a name="id_name"></a> 参考: htt ...

  8. 利用pip批量更新python库

    如果python库比较旧,需要更新到最新版本,可以利用pip工具. DOS命令行下,输入pip -V查看pip版本,可以先把pip更新到新版本. 查看系统里过期的python库 pip list #列 ...

  9. Linux 配置163yum源epel 源

    今天一个小伙伴询问博主,想换个163源(阿里源.亚马逊应该都是一样,博主没有一一验证)怎么换!博主当然兴致勃勃的准备好了指点小伙伴...但是,你没猜错,打脸了.而且最后还是和小伙伴一起配置好的,所以就 ...

  10. mimtproxy和arpspoof实现局域网MITM

    本地环境 环境:kali系统 目标机器:192.168.0.101 局域网网关:192.168.0.1 当前网络网卡端口:wlan0 arp欺骗流程 命令行开启本地数据转发: echo > /p ...