油田 Oil Deposits
油田
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/L
题意:
输入一个m行n列的字符矩形,统计字符“@”组成多少个八连块。如果两个字符“@”所在的格子相邻(横,竖或者对角线方向),
就说题目属于同一个八连块。
样例:
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2 分析:
用dfs遍历
从每个‘@’格子出发,递归遍历它周围的‘@’格子。每次访问一个格子都进行标记,防止重复遍历。
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=;
char pic[maxn][maxn];
int m,n,d[maxn][maxn];
void dfs(int x,int y,int z)
{
if(x<||x>=m||y<||y>=n) return; //格子的边界
if(d[x][y]>||pic[x][y]!='@') return; //遍历过的格子和没在八连块中的格子
d[x][y]=z; //对遍历过的格子进行标记
for(int r=-;r<=;r++)
for(int c=-;c<=;c++)
if(r!=||c!=) dfs(x+r,y+c,z);
}
int main()
{
int i;
while(scanf("%d%d",&m,&n)==&&m&&n)
{
for( i=;i<m;i++)
cin>>pic[i];
memset(d,,sizeof(d));
int c=;
for( i=;i<m;i++)
for(int j=;j<n;j++)
if(d[i][j]==&pic[i][j]=='@')
dfs(i,j,++c);
cout<<c<<endl;
}
return ;
}
油田 Oil Deposits的更多相关文章
- [C++]油田(Oil Deposits)-用DFS求连通块
[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...
- ACM:油田(Oil Deposits,UVa 572)
/* Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- 洛谷 题解 UVA572 【油田 Oil Deposits】
这是我在洛谷上的第一篇题解!!!!!!!! 这个其实很简单的 我是一只卡在了结束条件这里所以一直听取WA声一片,详细解释代码里见 #include<iostream> #include&l ...
- Oil Deposits(油田)(DFS)
题目: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. G ...
- 2016HUAS暑假集训训练题 G - Oil Deposits
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- 暑假集训(1)第七弹 -----Oil Deposits(Poj1562)
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- HDOJ/HDU 1241 Oil Deposits(经典DFS)
Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...
- HDU_1241 Oil Deposits(DFS深搜)
Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...
随机推荐
- python学习-爬虫
转载自 静觅的博客 最普通下载网页 import urrlib2 response = urllib2.urlopen("http://www.baidu.com") print ...
- 【项目经验】navicat工具 SQLServer数据库迁移MySQL
新近领了一个任务,就是把SQL Server的数据库迁移到My Sql上,经过查资料,圆满完成任务.分享一下流程. 1.首先,在自己的My Sql数据库上新建一个数据库. 2.打开新建的My Sql数 ...
- HDU 4162 Shape Number (最小表示法)
题意:给你一串n个数,求出循环来看一阶差的最小字典序:数字串看成一个顺时针的环,从某一点开始顺时针循环整个环,保证字典序最小就是答案 例如给你 2 1 3 就会得到(1-2+8 注意题意负数需要加8) ...
- 编译报错dereferencing pointer to incomplete type
关于编译报错“dereferencing pointer to incomplete type... 多是没找到结构体的定义,可以在本地复制其定义试试. 参考: http://my.oschina.n ...
- express-18 路由
简介 路由是网站或Web服务中最重要的一个方面:路由是将请求(由URL和HTTP方法指定)路由到处理它们的代码去的一种机制. 路由过去是基于文件的,这很简单,但不灵活. IA 是指内容的概念性组织.在 ...
- idea 使用
1.idea对maven的兼容性优于eclipse,idea对于程序内部管理是模块,个人感觉也是优于eclipse. 2.idea默认设置编辑器字体是比较差的. 可以参考文章 http://www.3 ...
- 阻止Ajax多次提交
1.Ajax的abort() xhr = $.ajax({}) if (xhr){ xhr.abort(); } 2.通过在Ajax的beforeSend()方法以及complete()方法添加删除类 ...
- JS小函数
join()\toString(): join()函数和toString()函数都是返回字符串类型. 针对一个数组: var arr = ["I","love" ...
- iOS UIImageView设置为圆形
UIImageView设置为圆形的方法(效率比较低下,当需要显示很多圆形view的时候,非常不推荐这种方式): imageView.layer.masksToBounds = YES; imageVi ...
- weblogic应用平台常见问题小结
http://blog.sina.com.cn/s/blog_4aa08b7c010004yc.html 1.当以另一用户登录系统后,系统却显示的是前一用户的身份. 这是由于页面信息没有刷新造成 ...