UVA 572 dfs求连通块
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
题目大意 @代表油田 @相邻(水平 竖直 斜着 八个方向)的话代表同一个油田 给你m*n的区域求油田个数
分析 简单搜索 求连通块 bfs dfs都可以写
AC代码
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
const int maxn=+;
using namespace std;
typedef long long ll;
char a[maxn][maxn];
int m,n;
int idx[maxn][maxn]; //标记每个点所属连通块,同样可以记录是否访问过
void dfs(int r,int c,int id)
{
if(r<||r>=m||c<||c>=n) //注意边界
return;
if(idx[r][c]>||a[r][c]!='@') //不是‘@’或已经访问过
return;
idx[r][c]=id; //标记满足条件的点所属连通块
for(int i=-;i<=;i++)
{
for(int j=-;j<=;j++) //搜索八个方向 ,多种写法
{
if(i!=||j!=) //跳过 0,0 是本身
dfs(r+i,c+j,id);
}
}
}
int main(int argc, char const *argv[])
{
while(scanf("%d %d",&m,&n)== && m && n )
{
for (int i = ; i < m; ++i)
{
scanf("%s",a[i]);
}
memset(idx,,sizeof(idx)); //初始化为零,大于零说明已经标号,访问过
int cnt=;
for(int i=;i<m;i++)
{
for(int j=;j<n;j++)
{
if(idx[i][j]== && a[i][j]=='@') //枚举没被访问过且是‘@’的点
dfs(i,j,++cnt); //对该点进行深度优先搜索
}
}
printf("%d\n",cnt );
}
return ;
}
UVA 572 dfs求连通块的更多相关文章
- 【紫书】Oil Deposits UVA - 572 dfs求联通块
题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...
- UVA 572 Oil Deposits油田(DFS求连通块)
UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ...
- UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)
UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...
- DFS入门之二---DFS求连通块
用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...
- [C++]油田(Oil Deposits)-用DFS求连通块
[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...
- HDU1241 Oil Deposits —— DFS求连通块
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...
- 用DFS求连通块(种子填充)
[问题] 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横.竖或者对角线方向),就说它们属于同一个八连块.例如,图6-9中有两个八连块. 图6-9 [分 ...
- UVa 572 油田(DFS求连通块)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...
随机推荐
- java.lang.StringBuilder和java.lang.StringBuffer (JDK1.8)
这两个类都是继承自AbstractStringBuilder,AbstractStringBuilder有两个成员属性 char[] value; int count; 前者用于存储字符串,后者用于统 ...
- Anaconda多版本Python管理
Anaconda是一个集成python及包管理的软件,记得最早使用时在2014年,那时候网上还没有什么资料,需要同时使用py2和py3的时候,当时的做法是同时安装Anaconda2和Anaconda3 ...
- <tangmuchw>之新手vue项目小记--新建.vue文件,运行项目,出现error:This dependency was not found...
错误码: This dependency was not found: * !!vue-style-loader!css-loader?{"minimize":false,&quo ...
- .NET使用存储过程实现对数据库的增删改查
一.整体思路 先建立存储过程,再通过.net 调用存储过程,来实现对表的增删改查. 二.新建数据库及存储过程 打开SqlServer2008,新建数据库orm1,及表Student. 数据库和表建立好 ...
- DCL的失效:现实与初衷的背离
最近看了Brian Goetz写的一篇有关DCL的文章:Double-checked locking: Clever, but broken.( 2001年发表于JavaWorld上) 这篇文章讲述了 ...
- Visual Representation of SQL Joins
原文:http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins 从视图上介绍了7种不同类型的JOIN ...
- ADO.NET查询和操作数据库
stringbuilder 类 stringbuilder类:用来定义可变字符串 stringbulider Append(string value) 在结尾追加 stringbuilder in ...
- 如何使用MOQ进行单元测试
使用MOQ来伪装和隔离被依赖对象,从而提高被测对象的测试效果. 安装 通过http://code.google.com/p/moq可以下载MOQ的最新版本.在SSL项目中,我们使用的是MOQ 3.1. ...
- Life in Changsha College-第一次冲刺
第一次冲刺任务 基于大局的全面性功能框架定位,要求能实现用户基于自己的需求进行的一系列操作. 用户故事 用户打开"生活在长大"的界面 程序首页展示校园服务,论坛等相关信息 用户选择 ...
- Linux 定时任务不生效的问题
Linux 中定时任务不生效的问题屡见不鲜, 本质原因是: 登录式 shell & 非登录式 shell. 登录式 shell & 非登录式 shell 登录式 shell 有: su ...