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< ...
随机推荐
- bzoj 3212 Pku3468 A Simple Problem with Integers
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Description You ...
- Markdown 编写规范
说明及目的 作为一个在博客园混迹了俩三年的人,一直在这里看别人的博客,现在准备开始写自己的博客,目的呢,就是一下几点吧: 项目过程中的历史经验教训积累记载,吃一堑长一智,不想在同一个坑掉进去好几次 学 ...
- Maven实战1
屏上得来终觉浅,绝知此事要躬行 总结: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px ".PingFang SC"; c ...
- axis和cxf集成Springmvc的使用
一.使用axis用wsdl生成Webservice: 工具:有axis插件的eclipse,wsdl文件: 操作步骤: 新建工程-->选择wsdl文件-->右键选择Webservice-- ...
- 基于Quartz实现简单的定时发送邮件
一.什么是Quartz Quartz 是一个轻量级任务调度框架,只需要做些简单的配置就可以使用:它可以支持持久化的任务存储,即使是任务中断或服务重启后,仍可以继续运行.Quartz既可以做为独立的应用 ...
- IO模型分析
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- poj2155一个二维树状数组
...
- 给tableView设置headerView时遇到的问题
在ViewDidLoad里面设置了 self.tableView.tableHeaderView = 自定义的View 然后在模拟器上运行后,发现这个HeaderView挡住了后面的Cell,也就是c ...
- C#无限分级实现,前端WEB页面接收,后台提供层级Json数据
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Te ...
- Sql语句构造类,多字段新增或修改时,拼装sql语句比较方便
using System; using System.Collections.Generic; using System.Text; namespace MSCL { #region 使用示例 /* ...