HDOJ/HDU 1241 Oil Deposits(经典DFS)
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
The 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.
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
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
题意:
*代表荒地,@代表油田。@的上下左右,还有对角的4个如果还有@,就表示它们是一个油田,问-给出的图中,一共有多少油田。
分析:
本题用DFS(深搜)可以很好的解决问题!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char map[210][210];
int df[210][210];
int con,n,m;
void dfs(int x,int y,int t){
if(df[x][y]==1||x<0||y<0||x>=n||y>=m){
return;
}
if(map[x][y]=='*'){
return;
}
if(t==1&&map[x][y]=='@'){
con++;
//printf("con=%d\n",con);
}
//printf("%d %d\n",x,y);
if(map[x][y]=='@'){
map[x][y]='*';
df[x][y]=1;
dfs(x+1,y+1,0);
dfs(x-1,y-1,0);
dfs(x+1,y,0);
dfs(x,y+1,0);
dfs(x-1,y,0);
dfs(x,y-1,0);
dfs(x+1,y-1,0);
dfs(x-1,y+1,0);
df[x][y]=0;
}
}
int main()
{
while(~scanf("%d%d",&n,&m),(n||m)){
memset(df,0,sizeof(df));
con=0;
getchar();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%c",&map[i][j]);
}
getchar();
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
dfs(i,j,1);
}
}
printf("%d\n",con);
}
return 0;
}
HDOJ/HDU 1241 Oil Deposits(经典DFS)的更多相关文章
- HDU - 1241 Oil Deposits 经典dfs 格子
最水的一道石油竟然改了一个小时,好菜好菜. x<=r y<=c x<=r y<=c x<=r y<=c x<=r y<=c #include ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- hdu 1241:Oil Deposits(DFS)
Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- HDU 1241 Oil Deposits (DFS)
题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...
- HDU 1241 Oil Deposits【DFS】
解题思路:第一道DFS的题目--- 参看了紫书和网上的题解-- 在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时, ...
- HDU - 1241 Oil Deposits 【DFS】
题目链接 https://cn.vjudge.net/contest/65959#problem/L 题意 @表示油田 如果 @@是连在一起的 可以八个方向相连 那么它们就是 一块油田 要找出 一共有 ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
随机推荐
- eval(phpcode) 字符当代码执行
eval(phpcode)eval() 函数把字符串按照 PHP 代码来计算.相当于在字符串两边分别加上PHP语 法标签 该字符串必须是合法的 PHP 代码,且必须以分号结尾. 如果没有在代码字符串中 ...
- winform 两个TreeView间拖拽节点
/// <summary> /// 正在拖拽的节点 /// </summary> private TreeNode DragNode = null; /// <summa ...
- 列表显示数据 但是数据的字体颜色要js添加
1.需求:数据在前台显示,但是每个条记录的颜色要有点不同 1.java后台数据的处理 String ids=""; for(int x=0;x<sign.size();x++ ...
- 【python】元组的插入
>>> temp=(1,2,3,4,5)>>> temp=temp[:2]+(8,)+temp[2:]>>> temp(1, 2, 8, 3, 4 ...
- designated initializer和secondary initializer是什么?
仅在此简单记录概念,方便以后回顾... ===================================== designated initializer是指定初始化方法,提供所有参数: sec ...
- SQLMAP系列教程
1.SQLMAP安装及access注入: http://www.stronkin.com/en/CompHonorBig.asp?id=7 2.Mysql数据库注入 http://www.slib ...
- backbone showcase
http://www.mhtml5.com/2012/06/5119.html http://tieba.baidu.com/p/2389371223 http://www.jdon.com/tags ...
- NODE.JS玩玩
按一个网页的来,最好最后能到EXPRESS.JS. http://www.nodebeginner.org/index-zh-cn.html 这样就能对比DJANGO,看看两者的WEB框架,加深认识. ...
- 【HDU 1828】 Picture (矩阵周长并,线段树,扫描法)
[题目] Picture Problem Description A number of rectangular posters, photographs and other pictures of ...
- 如何解决C#编译中"csc不是内部或外部命令"的问题
安装完 VisualStudio 2010编译环境后,是不能用命令行直接编译写好的csc文件的,如果不配置环境变量,在命令提示符(cmd)中编译扩展名为cs的文件,会出现错误提示“csc不是内部或外部 ...