Oil Deposits

HDU - 1241

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. 

InputThe input file 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. 
OutputFor each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they 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 注意:相邻是指8个方向
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<queue> using namespace std; int dx[] = {,-,,,,,-,-};
int dy[] = {,,,-,-,,,-};
char mp[][];
int vis[][];
int m, n; void dfs(int x, int y)
{
for(int i = ; i < ; ++i)
{
int xx = x + dx[i];
int yy = y + dy[i]; if(xx >= && xx < m && yy >= && yy < n && !vis[xx][yy] && mp[xx][yy] == '@')
{
vis[xx][yy] = ;
dfs(xx, yy);
}
}
} int main()
{
std::ios::sync_with_stdio(false);
while(cin >> m >> n)
{
if(m == )
break;
for(int i = ; i < m; ++i)
for(int j = ; j < n; ++j)
cin >> mp[i][j];
memset(vis, , sizeof(vis));
int ans = ;
for(int i = ; i < m; ++i)
{
for(int j = ; j < n; ++j)
{
if(mp[i][j] == '@' && !vis[i][j])
{
vis[i][j] = ;
dfs(i, j);
ans++;
} }
}
cout << ans << endl;
} return ;
}

Oil Deposits HDU - 1241 (dfs)的更多相关文章

  1. (深搜)Oil Deposits -- hdu -- 1241

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  2. Oil Deposits HDU 1241

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  3. kuangbin专题 专题一 简单搜索 Oil Deposits HDU - 1241

    题目链接:https://vjudge.net/problem/HDU-1241 题意:问有几个油田,一个油田由相邻的‘@’,组成. 思路:bfs,dfs都可以,只需要遍历地图,遇到‘@’,跑一遍搜索 ...

  4. HDU 1241 DFS

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

  5. hdu 1241(DFS/BFS)

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

  6. HDU 1241 Oil Deposits(经典DFS)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 很经典的一道dfs,但是注意每次查到一个@之后,都要把它变成“ * ”,然后继续dfs ...

  7. hdu 1241 Oil Deposits (一次dfs搞定有某有)

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...

  8. HDU 1241 (DFS搜索+染色)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...

  9. POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)

    题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...

随机推荐

  1. 【五校联考5day1】登山

    题目 描述 题目大意 给你一个n∗nn*nn∗n的网格图.从(0,0)(0,0)(0,0)开始,每次只可以向右或向上移动一格,并且不能越过对角线(即不能为x<yx<yx<y). 网格 ...

  2. C语言利用动态数组实现顺序表(不限数据类型)

    实现任意数据类型的顺序表的初始化,插入,删除(按值删除:按位置删除),销毁功能.. 顺序表结构体 实现顺序表结构体的三个要素:(1)数组首地址:(2)数组的大小:(3)当前数组元素的个数. //顺序表 ...

  3. JS里面function和Function的区别

    js里Function 与 function的不一样的,不仅仅是大小写的问题. 简单点说:大写的Function是一个类 ,而小写的function是一个对象. Function是一个构造器,func ...

  4. 服务器迁移部署OmsWeb

    绑定 基本设置 高级设置

  5. 安装hadoop伪分布式

    修改hosts cat /etc/hosts 127.0.0.1 mo.don.com 创建用户 useradd hadoop passwd hadoop sudo授权 visudo hadoop A ...

  6. 朴素贝叶斯算法的python实现方法

    朴素贝叶斯算法的python实现方法 本文实例讲述了朴素贝叶斯算法的python实现方法.分享给大家供大家参考.具体实现方法如下: 朴素贝叶斯算法优缺点 优点:在数据较少的情况下依然有效,可以处理多类 ...

  7. mysql内建命令快速手记 — 让手指跟上思考的速度(一)

    在微信公众号上看到一篇文章说的很好,意思是说,大牛在尝试各种方案的时候可能并没有超神的预测和筛选能力 只是你通常测试一种情况时,大神已经测试了好几种方案了,讲的是"为什么大多数程序员不喜欢写 ...

  8. [Turn]C# 强制关闭当前程序进程(完全Kill掉不留痕迹)

    C#代码 /// <summary> /// 运行DOS命令 /// DOS关闭进程命令(ntsd -c q -p PID )PID为进程的ID /// </summary> ...

  9. net.sf.json JSONObject与JSONArray总结

    JSONObject:json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value} JSONArray:json数组,使用中括号[ ],只不过数组里面的项也是json键值对格式 ...

  10. mysql三表联合查询,结果集合并

    参考: mysql 结果集去重复值并合并成一行 SQL 三表联查 数据库三表连接查询怎么做 合并: MySQL中group_concat函数 完整的语法如下: group_concat([DISTIN ...