Oil Deposits
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16655   Accepted: 8917

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

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 本题是经典的DFS题,利用DFS求连通块的数量,DFS利用的是递归实现
#include<iostream>
using namespace std; const int maxn = 100+5; char pic[maxn][maxn]; //图表
int m, n, idx[maxn][maxn]; //连通分量编号矩阵 //用DFS的方法为连通分量矩阵编号
void DFS(int r, int c, int id)
{
if(r<0||r>m||c<0||c>=n)return; //overload
if(idx[r][c]>0||pic[r][c]!='@')return; //no '@' or 已经访问
idx[r][c]=id; //连通分量编号
for(int dr=-1; dr<=1;dr++)
for(int dc=-1;dc<=1;dc++)
if(dr!=0||dc!=0)DFS(r+dr, c+dc, id);
} int main()
{
while(cin>>m>>n&&m!=0)
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>pic[i][j];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
idx[i][j]=0;
int cnt=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(idx[i][j]==0&&pic[i][j]=='@')DFS(i,j,++cnt);
cout<<cnt<<endl;
}
return 0;
}

 

可以参考floodfill算法

https://en.wikipedia.org/wiki/Flood_fill 

												

[POJ] 1562 Oil Deposits (DFS)的更多相关文章

  1. HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题

    Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...

  2. POJ 1562 Oil Deposits (并查集 OR DFS求联通块)

    Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14628   Accepted: 7972 Des ...

  3. (简单) POJ 1562 Oil Deposits,BFS。

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

  4. POJ 1562 Oil Deposits (HDU 1241 ZOJ 1562) DFS

    现在,又可以和她没心没肺的开着玩笑,感觉真好. 思念,是一种后知后觉的痛. 她说,今后做好朋友吧,说这句话的时候都没感觉.. 我想我该恨我自己,肆无忌惮的把她带进我的梦,当成了梦的主角. 梦醒之后总是 ...

  5. POJ 1562 Oil Deposits

    转载请注明出处:http://blog.csdn.net/a1dark 大规模的图论切题之旅正式开始了.由于今天停了一天的电.所以晚上才开始切题.直到昨晚才把图论大概看了一遍.虽然网络流部分还是不怎么 ...

  6. poj 1562 Oil Deposits (广搜,简单)

    题目 简单的题目,只是测试案例的输入后面可能有空格,所以要注意一下输入方式. #define _CRT_SECURE_NO_WARNINGS //题目的案例输入n,m后面有些貌似有空格... #inc ...

  7. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  8. Oil Deposits(dfs)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  9. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

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

随机推荐

  1. Git提交到多个远程仓库

    在已经习惯使用git同步写代码,github无疑是最的托管平台,但是国内由于"你懂的"原因,速度很慢,有时无法访问,于是想把自己的代码同步到多个不同的远程仓库备份. 我的主要仓库: ...

  2. MyEclipse 中文注释乱码

    window-->preference-->general-->content type然后在<Content Types>中展开每一个子项,并在<Default ...

  3. ACM沈化校赛

    答对两道题,因为比赛经验原因明明可以对四道,在这两道题又浪费过多时间,成绩不理想,但也是累积了一下经验.

  4. 低功耗蓝牙BLE外围模式(peripheral)-使用BLE作为服务端

    低功耗蓝牙BLE外围模式(peripheral)-使用BLE作为服务端 Android对外模模式(peripheral)的支持 从Android5.0开始才支持 关键术语和概念 以下是关键BLE术语和 ...

  5. HDU 5873 Football Games

    随便判了几个条件就过了,也不知道对不对的. 正解应该是: $[1].$${s_1} + {s_2} + {s_3} + ...... + {s_n} = n(n - 1)$ $[2].$${s_1} ...

  6. css-ie6下实现最小,最大宽度

    _width: expression((document.documentElement.clientWidth||document.body.clientWidth)<1040?"1 ...

  7. 3.struts2访问Servlet API,并和mybaits实现全套增删改查

    1.创建数据库脚本userinfo.sql prompt PL/SQL Developer import file prompt Created on 2016年5月19日 by pc set fee ...

  8. 在GNU/Linux下将CD音乐转为mp3

    以前我欣赏古典音乐都是听的CD,因而珍藏了不少光盘以及下载到电脑上的ape与flac格式的音乐文件.随着手机硬件性能(如电池续航能力.处理器速度.音质.存储容量等)和软件功能(音乐播放器对于曲目的管理 ...

  9. 数据库连接池druid

    推荐一个带监控的数据库连接池druid,阿里的,据说比c3p0连接池性能要好点,主要是带sql,spring,uri等访问监控,比较直观地址: https://github.com/alibaba/d ...

  10. CSS3 Filter滤镜效果

    关注到它是在一次分享会当中,很神奇,只需写一行代码就可以变身很美的视觉效果,这就是CSS3滤镜. 语法 filter:fuction(param); 如今浏览器支持情况相比以前乐观很多,点击查看兼容 ...