ZOJ 1709 Oil Deposits(dfs,连通块个数)
Oil Deposits
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
Source: Mid-Central USA 1997
分析:
dfs水题,问你@连通块的个数
@可以向8个方向扩展
和油田问题属于同一类问题
code:
#include<bits/stdc++.h>
using namespace std;
#define max_v 105
int n,m;
char a[max_v][max_v];
int used[max_v][max_v];
int dir[][]={{,},{,-},{,},{-,},{,},{,-},{-,},{-,-}};
void dfs(int x,int y,int z)
{
if(x<||x>=n||y<||y>=m)
return ;
if(used[x][y]>||a[x][y]!='@')
return ;
used[x][y]=z;
for(int i=;i<;i++)
dfs(x+dir[i][],y+dir[i][],z);
}
int main()
{
while(cin>>n>>m)
{
if(m==&&n==)
break;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>a[i][j];
}
}
memset(used,,sizeof(used));
int c=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(used[i][j]==&&a[i][j]=='@')
dfs(i,j,++c);
}
}
printf("%d\n",c);
}
}
ZOJ 1709 Oil Deposits(dfs,连通块个数)的更多相关文章
- POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)
题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...
- HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...
- 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 or BFS)
链接 : Here! 思路 : 搜索判断连通块个数, 所以 $DFS$ 或则 $BFS$ 都行喽...., 首先记录一下整个地图中所有$Oil$的个数, 然后遍历整个地图, 从油田开始搜索它所能连通多 ...
- 【BZOJ 1098】办公楼(补图连通块个数,Bfs)
补图连通块个数这大概是一个套路吧,我之前没有见到过,想了好久都没有想出来QaQ 事实上这个做法本身就是一个朴素算法,但进行巧妙的实现,就可以分析出它的上界不会超过 $O(n + m)$. 接下来介绍一 ...
- P1197 [JSOI2008]星球大战 [删边求连通块个数]
展开 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的 ...
- UVa572 Oil Deposits DFS求连通块
技巧:遍历8个方向 ; dr <= ; dr++) ; dc <= ; dc++) || dc != ) dfs(r+dr, c+dc, id); 我的解法: #include< ...
- HDU1241 Oil Deposits —— DFS求连通块
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
随机推荐
- 流畅的python和cookbook学习笔记(三)
1.双向队列 collections.deque 类(双向队列)是一个线程安全.可以快速从两端添加或者删除元素的数据类型. rotate和popleft操作,rorate可以把前后元素换位.pople ...
- 结构型--代理模式(Proxy)
一.代理模式是什么? 代理模式属于GOF23设计模式中结构型中的设计模式,通过代理对象来屏蔽(部分或者屏蔽)对真实对象的直接访问,下图为UML图: 在代理模式中组件包括:抽象角色接口.代理角色类.真实 ...
- docker 容器启动并自启动redis
centos7.5 背景:每次开机后都要自动启动redis,也就是宿主机开机,启动容器,然后启动redis 按照网上的做法是:修改redis.conf ,修改redis的启动脚本(utils/...s ...
- Spring课程 Spring入门篇 5-4 advice应用(上)
1 解析 1.1 通知执行顺序 2 代码演练 1 解析 1.1 通知执行顺序 aop执行方式为:前置通知==>所要增强的方法==>后置通知==>最终通知 在出现异常时会进行:前置通知 ...
- js表单快速取值/赋值 快速生成下拉框
1.表单取值/赋值公共方法 //表单序列化:文本框的name字段和数据源一致<form id="myForm" onsubmit="return false;&qu ...
- BootStrap:
BootStrap: * 响应式的HTML,CSS,JS的框架. * 响应式设计: * 设计一套页面,适配不同的设备,在手机,PAD,PC端都能够正常浏览. * 响应式原理: * 使用CSS3的媒体查 ...
- npm全局安装失效修复
一.windows下 先查看npm包的默认安装目录 npm config get prefix 修改配置 npm config set prefix "nodeJs的安装目录" 二 ...
- Git 学习记录
安装 1. https://git-for-windows.github.io下载(网速慢的同学请移步国内镜像),然后按默认选项安装即可.安装完成后,在开始菜单里找到“Git”->“Git B ...
- JSON中的安全问题
Web中使用JSON时最常见的两个安全问题: 1.跨站请求伪造: 即CSRF,是一种利用站点对用户浏览器信任发起攻击的方式.典型的就是JSON数组,更多信息请自行上网百度. 2.跨站脚本攻击. 是注入 ...
- 多线程 读写锁SRWLock
在<秒杀多线程第十一篇读者写者问题>文章中我们使用事件和一个记录读者个数的变量来解决读者写者问题.问题虽然得到了解决,但代码有点复杂.本篇将介绍一种新方法——读写锁SRWLock来解决这一 ...