Oil Deposits

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

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

Output

For 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

*

*@*@*
**@**
*@*@* @@****@* ****@
*@@*@
*@**@
@@@*@
@@**@

Sample Output

0
1
2
2

Source

Mid-Central USA 1997

 
  这是道经典dfs搜索题,难度属于入门级,没有太难的地方。
  下面是代码:
 #include <iostream>
using namespace std;
char a[][];
int dx[] = {,,,,,-,-,-};
int dy[] = {,,,-,-,-,,};
int m,n;
void dfs(int x,int y) //将(x,y)坐标有联系的所有坐标都标记为 *
{
a[x][y]='*';
for(int i=;i<;i++){
int nx = x+dx[i];
int ny = y+dy[i];
if( nx< || ny< || nx>m || ny>n) //如果越界
continue;
if(a[nx][ny]!='@') //如果这样走的下一步不是 @
continue;
dfs( nx , ny );
}
}
int main()
{
while(cin>>m>>n,m!=){
int _count=;
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
cin>>a[i][j];
for(int i=;i<=m;i++)
for(int j=;j<=n;j++){
if(a[i][j]=='@'){
_count++;
dfs(i,j);
}
}
cout<<_count<<endl;
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 1241:Oil Deposits(DFS)的更多相关文章

  1. HDU 1241 Oil Deposits (DFS)

    题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...

  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. HDU 1241 Oil Deposits --- 入门DFS

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

  5. hdu 1241 Oil Deposits (简单搜索)

    题目:   The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. ...

  6. HDOJ/HDU 1241 Oil Deposits(经典DFS)

    Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...

  7. HDU 1241 Oil Deposits【DFS】

    解题思路:第一道DFS的题目--- 参看了紫书和网上的题解-- 在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时, ...

  8. HDU 1241 Oil Deposits(经典DFS)

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

  9. poj1562 Oil Deposits(DFS)

    题目链接 http://poj.org/problem?id=1562 题意 输入一个m行n列的棋盘,棋盘上每个位置为'*'或者'@',求'@'的连通块有几个(连通为8连通,即上下左右,两条对角线). ...

随机推荐

  1. tomcat管理员配置

    纸上得来终觉浅,绝知此事要躬行 博客园 首页 新闻 新随笔 联系 管理 随笔- 458  文章- 0  评论- 38  Tomcat的Manager显示403 Access Denied   管理to ...

  2. hdu 1241 Oil Deposits(水一发,自我的DFS)

    解题思路: 1. 遍历扫描二维数组,遇到‘@’,结果ans++; 2. 将当前 i,j 位置置为‘*’,将当前‘@’的 i,j 传人到DFS函数中,开始遍历八个方向的字符 如果碰到 '@' 则先将当前 ...

  3. iOS 刚刚,几分钟前,几小时前,几天前,几月前,几年前

    - (NSString *)compareCurrentTime:(NSDate*) compareDate { NSTimeInterval timeInterval = [compareDate ...

  4. php中文字符串翻转

    转自:http://www.oschina.net/code/snippet_613962_17070 <?php header("content-type:text/html;cha ...

  5. linux 多线程 LinuxThreads(转)

    Linux 线程实现机制分析 http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/ 上面文章分析的非常透彻 按照教科书上的定义,进程是 ...

  6. Visual Studio Online Integrations-Sync and migration

      原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-directory-vs

  7. 看过《大湿教我写.net通用权限框架(1)之菜单导航篇》之后发生的事(续)——主界面

    引言 在UML系列学习中的小插曲:看过<大湿教我写.net通用权限框架(1)之菜单导航篇>之后发生的事 在上篇中只拿登录界面练练手,不把主界面抠出来,实在难受,严重的强迫症啊.之前一直在总 ...

  8. js实现把网页table导成Excel

    //导出excel function exportExcel(DivID,strTitle){ if(DivID==null) { return false; } var jXls, myWorkbo ...

  9. [BZOJ2724][Violet 6]蒲公英

    [BZOJ2724][Violet 6]蒲公英 试题描述 输入 修正一下 l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n + 1 输出 输入示 ...

  10. python 的编码问题

    老是碰到这个问题,决定好好给整理一番思路. 翻阅资料和实践证明,以下论述为真理: 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将 ...