Oil Deposits(dfs)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15291 Accepted Submission(s): 8787
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 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.
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.
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,m;
char grid[120][120];
int vis[120][120];
struct node{
int x,y;
};
void in_put()
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;++i)
scanf("%s",grid[i]+1);
}
int check(node v)
{
if(!vis[v.x][v.y]&&v.x>=1&&v.x<=n&&v.y>=1&&v.y<=m&&grid[v.x][v.y]=='@')
return 1;
else return 0;
}
void dfs(node v)
{
node nex;
vis[v.x][v.y]=1;grid[v.x][v.y]='?'; nex.x=v.x+1;nex.y=v.y;if(check(nex)) dfs(nex);
nex.x=v.x+1;nex.y=v.y+1;if(check(nex)) dfs(nex);
nex.x=v.x+1;nex.y=v.y-1;if(check(nex)) dfs(nex);
nex.x=v.x;nex.y=v.y-1;if(check(nex)) dfs(nex);
nex.x=v.x;nex.y=v.y+1;if(check(nex)) dfs(nex);
nex.x=v.x-1;nex.y=v.y-1;if(check(nex)) dfs(nex);
nex.x=v.x-1;nex.y=v.y+1;if(check(nex)) dfs(nex);
nex.x=v.x-1;nex.y=v.y;if(check(nex)) dfs(nex);
}
int main()
{
while(scanf("%d%d",&n,&m))
{
int cnt=0;node now;
if(!n&&!m) break;
in_put();
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(grid[i][j]=='@')
{now.x=i;now.y=j;dfs(now);cnt++;} printf("%d\n",cnt);
}
}
2
Oil Deposits(dfs)的更多相关文章
- 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(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- UVa572 Oil Deposits DFS求连通块
技巧:遍历8个方向 ; dr <= ; dr++) ; dc <= ; dc++) || dc != ) dfs(r+dr, c+dc, id); 我的解法: #include< ...
- HDU 1241 Oil Deposits (DFS/BFS)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 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深搜)
Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...
- UVa 572 Oil Deposits(DFS)
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil ...
- [POJ] 1562 Oil Deposits (DFS)
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16655 Accepted: 8917 Des ...
- Oil Deposits(dfs水)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...
随机推荐
- 【linux】学习7
鸟哥 22章内容 单个源代码编译运行 设有一个hello.c 可以用下面方式运行 生成可执行文件a.out [localhost scripts]$ gcc hello.c [localhost sc ...
- java中方法参数的一些总结(1)
1.问题说明 在C++中,函数调用时有传值调用和传址调用两种方式,但在Java中只有传值调用一种方式.Java中的方法参数为那几种基本数据类型的情况跟C++中一样,传入的只是变量的拷贝. ...
- IOS - 消息推送原理和实现
一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1.Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Pr ...
- 带你熟悉CSS浮动
一.概念理解 浮动:顾名思义先浮后动,浮动的对象会先漂浮起来,离开自己原来的位置(也就是所谓的脱离文档流),后动的意思是,它的后面的元素会向它原来的位置动起来. 二.注意事项 1.当元素有浮动属性时, ...
- 关于Cookie和Session的优缺点
关于Cookie和Session的优缺点 具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态的方案. Cookie的优缺点:优点:极高的扩展性和可用 ...
- supersr--九宫格公式(根据多少行多少列排版)
- (void)layoutSubviews{ [super layoutSubviews]; NSUInteger count = self.subviews.count; NSInteger ma ...
- 决绝Capturing 'demo' strongly in this block is likely to lead to a retain cycle
- (IBAction)onTest:(id)sender { BlockDemo *demo = [[BlockDemo alloc]init]; __weak typeof(BlockDemo) ...
- Centos7 设置Swap分区
1.使用dd命令创建一个swap交换文件 dd if=/dev/zero of=/home/swap bs=1024 count=1024000 2.制作为swap格式文件: mkswap /home ...
- 合唱队形2(洛谷U5874)
题目背景 上次老师挑出来的(N-K)位同学很不高兴,于是他们准备自己组建合唱队形.他们请了kkk来帮忙. 题目描述 他们安排了一个动作--手拉着手唱一首歌(就是他们围成一个圈).如果有两个相邻的同学的 ...
- XSLT教程
XSL 指扩展样式表语言(EXtensible Stylesheet Language), 它是一个 XML 文档的样式表语言. XSLT 指 XSL 转换.即使用 XSLT 将 XML 文档转换为其 ...