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. To the end

    身为一名初二狗的我也走过了半年.不管怎么说人生中也没有几个半年嘛.从九月到现在快四个月了,我也离中考越来越近了/郁闷/.但是还是要好好过唔.不过我想起这半学期还是挺充实的,至少没有浪费太多的时间.有些 ...

  2. 第17章 使用PHP和MySQL实现身份验证

    1.对密码进行加密:sha1(string str,bool raw_output) //将返回一个40个字符的伪随机字符串,若raw_output为true,着得到一个20个字符的二进制字符串数据 ...

  3. IOS开发调整UILabel的行间距

    CGFloat heih = 20;   NSString * cLabelString = @"这是测试UILabel行间距的text.这是测试UILabel行间距的text.n 这是测试 ...

  4. for循环执行步骤

    for循环的具体步骤: for(var i=0;i<5;i++){ alert(1); } 第一步--->初始化i(初始化只在for循环中执行一次); 第二步--->执行条件i< ...

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

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

  6. HTML a标签 target属性作用

    特殊的目标 有 4 个保留的目标名称用作特殊的文档重定向操作: _blank 浏览器总在一个新打开.未命名的窗口中载入目标文档. _self 这个目标的值对所有没有指定目标的 <a> 标签 ...

  7. 利用Rsync在windows和linux之间同步数据

    使用Rsync从windows同步文件到linux 1.windows服务端的安装与配置: 免费软件下载地址:http://linux.linuxidc.com/,用户名密码为:www.linuxid ...

  8. 杭州蓝松科技推出的安卓端的USB转串口调试助手, 欢迎下载使用

    杭州蓝松科技推出的安卓端的USB转串口调试助手, 欢迎下载使用 下载地址:http://files.cnblogs.com/guobaPlayer/%E8%93%9D%E6%9D%BEUSB%E4%B ...

  9. 《C程序设计语言》 squeeze函数(从字符串s中删除字符c)

    squeeze void squeeze(char string[], int ch) { int i, j; ; string[i] != '\0'; i++) { if (string[i] != ...

  10. Mac终端命令收集

    一.利用命令行执行文件 cd到要执行文件的文件夹 输入python xx.py   命令即可(xx表示要执行的文件名称)