Oil Deposits
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.
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 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
#include
#define maxn 110
using namespace std;
char mapn[maxn][maxn];
int
direction[8][2]={{-1,0},{1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,-1},{1,1}};
bool visit[maxn][maxn];
int m,n,sum=0;
bool isbound(int a,int b)//判断边界
{
if(a<1||a>m||b<1||b>n)return true;
return
false;
}
dfs(int x,int y)
{
for(int
i=0;i<8;i++)
{
if(mapn[x+direction[i][0]][y+direction[i][1]]=='*')
continue;
if(isbound(x+direction[i][0],y+direction[i][1]))
continue;
if(visit[x+direction[i][0]][y+direction[i][1]])
continue;
visit[x+direction[i][0]][y+direction[i][1]]=true;
dfs(x+direction[i][0],y+direction[i][1]);
}
}
main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d\n",&m,&n)&&(m||n))//这地方应该有一个回车
{
//printf("m=%d n=%d\n",m,n);
memset(visit,false,sizeof(visit));
sum=0;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%c",&mapn[i][j]);
}
scanf("\n");//每行输入结束都应该有一个回车
}
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
if(mapn[i][j]=='@'&&!visit[i][j])
{
dfs(i,j);
visit[i][j]=true;
sum++;
}
}
printf("%d\n",sum);
}
}
Oil Deposits的更多相关文章
- Oil Deposits
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Oil Deposits(dfs)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 2016HUAS暑假集训训练题 G - Oil Deposits
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- uva 572 oil deposits——yhx
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...
- hdu 1241:Oil Deposits(DFS)
Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- hdu1241 Oil Deposits
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) ...
- 杭电1241 Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次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< ...
随机推荐
- Java 简单实用方法二
整理以前的笔记,在学习Java时候,经常会用到一些方法.虽然简单但是经常使用.因此做成笔记,方便以后查阅 这篇博文先说明构造和使用这些方法. 1,判断String类型数据是否包含中文 可以通过正则表达 ...
- String类的替换方法(9)
1:String replace(char old,char new) 2: String replace(String old,String new) 3: trim();//去除字符串空格 ...
- .NET 动态脚本语言
Script.NET (S#) 是一种允许为你的应用程序自定义行为,与本地.NET对象.类型和组件交互动态的脚本语言.托管应用程序本身可以改变一个xml配置脚本运行时的默认行为,更换绑定的方法,属性, ...
- 谈javascript变量声明
之前的面试中遇到过一道面试题 var a =10;(function(){ console.log(a); var a =20;})() 短短5行代码log的结果是什么? 如果把var a = 20; ...
- 【转】Keberos认证原理
前几天在给人解释Windows是如何通过Kerberos进行Authentication的时候,讲了半天也别把那位老兄讲明白,还差点把自己给绕进去.后来想想原因有以下两点:对于一个没有完全不了解Ker ...
- Linux CentOS 7 防火墙/端口设置
CentOS升级到7之后用firewall代替了iptables来设置Linux端口, 下面是具体的设置方法: []:选填 <>:必填 [<zone>]:作用域(block.d ...
- 【转】HTTP Header 详解
HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服务器给与响应.就整个网络资源传 ...
- struts2中的结果视图类型
实际上在Struts2框架中,一个完整的结果视图配置文件应该是: <action name="Action名称" class="Action类路径" me ...
- Java面向对象 集合(上)
Java面向对象 集合(上) 知识概要: (1)体系概述 (2)共性方法 (3)迭代器 (4)list集合 (5)Set 集合 体系概述: 集 ...
- C#生成Code128码
using System; using System.Collections.Generic; using System.Data; using System.Drawing; namespace C ...