Oil Deposits UVA - 572
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
and
. 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
题目很简单,求连通块,只是自己开始没用vis打标记错了,贴出来长点记性,以后最好还是用vis数组打标记把、、、
这份代码运行错误,测试样例当然能过
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 105
using namespace std;
char mapn[maxn][maxn];
int num,n,m;
void dfs(int a,int b)
{
if(a>=n || a< || b>=m || b<)
return;
for(int i=-;i<=;i++)
for(int j=-;j<=;j++)
{
if(!i&&!j)
continue;
int xx = a + i;
int yy = b + j;
if(mapn[xx][yy] == '@')
{
mapn[xx][yy] = '*';
dfs(xx,yy);
}
}
}
int main()
{
while(cin >> n >> m)
{
if(!n&&!m)
break;
num = ;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
cin >> mapn[i][j];
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(mapn[i][j] == '@')
{
num++;
mapn[i][j] = '*';
dfs(i,j);
}
}
cout << num << endl;
}
return ;
}
用vis标记的代码
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 105
using namespace std;
char mapn[maxn][maxn];
int num,n,m,vis[maxn][maxn];
void dfs(int a,int b)
{
if(a>=n || a< || b>=m || b<)
return;
if(vis[a][b] || mapn[a][b] != '@')
return;
vis[a][b] = ;
for(int i=-;i<=;i++)
for(int j=-;j<=;j++)
{
if(!i&&!j)
continue;
int xx = a + i;
int yy = b + j;
if(mapn[xx][yy] == '@')
{
dfs(xx,yy);
}
}
}
int main()
{
while(scanf("%d%d",&n,&m))
{
if(!n&&!m)
break;
num = ;
memset(mapn,,sizeof(mapn));
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
for(int j=;j<m;j++)
scanf("\n%c",&mapn[i][j]);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(mapn[i][j] == '@' && !vis[i][j])
{
num++;
dfs(i,j);
}
}
printf("%d\n",num);
}
return ;
}
Oil Deposits UVA - 572的更多相关文章
- ACM:油田(Oil Deposits,UVa 572)
/* Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- 【紫书】Oil Deposits UVA - 572 dfs求联通块
题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...
- 油田 (Oil Deposits UVA - 572)
题目描述: 原题:https://vjudge.net/problem/UVA-572 题目思路: 1.图的DFS遍历 2.二重循环找到相邻的八个格子 AC代码: #include <iostr ...
- UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)
UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...
- uva 572 oil deposits——yhx
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...
- UVA 572 Oil Deposits油田(DFS求连通块)
UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ...
- UVa 572 Oil Deposits(DFS)
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil ...
- 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( ...
随机推荐
- Codeforces Round #192 (Div. 2) (329A)C.Purification
题意: 在一个正常的点可以净化该行该列的所有细胞,判断是否可以净化所有的细胞,并且输出所选的点. 思路: 如果可以的话,一定会选n个点. 先判断每一行是否有正常细胞,然后判断每一列是否有,如果都没有肯 ...
- 使用hexo中遇到的问题
- PID算法 旋转倒立摆与平衡车的区别。此贴后边会更新。
我做PID算法的背景和经历:本人之前电子信息科学与技术专业,对控制方向颇感兴趣,刚上大学时听到实验室老师说PID算法,那年在暑假集训准备全国电子设计竞赛,我正在练习做一个以前专科的题目,帆板角度控制系 ...
- Linux vim基本的使用方法
一.vim 的三种模式 (1) 插入模式 在插入模式中,才能输入文字:要进入插入模式,可以按键 “i”:如果要进入插入模式时,直接切换到下一行,可以输入“o”: (2) 命令模式 在命令模式中,主要进 ...
- JSON合并,并按时间排序
mergeJson: function (json1, json2) { var json = Object.assign([], json1, json2); return json.sort(fu ...
- [Pulsar系列] 10分钟学会Pulsar消息系统概念
Apache Pulsar Pulsar是一个支持多租户的.高性能的服务与服务之间消息通讯的解决方案,最初由雅虎开发,现在由Apache软件基金会管理. Pulsar的主要特性如下: Pulsar实例 ...
- 高性能MySQL之基础架构
一.背景 为什么我们需要先学习MYSQL的基础架构先呢? 原因很简单,当我们需要了解一件事物的时候,我们只有站在宏观的层面,才能层层剥丝抽茧的去理解问题.举个例子,我们要看一个框架的源码,一开始就想进 ...
- Selenium+java - 日期控件的处理
前言 一般的日期控件都是input标签下弹出来的,设置日期使用selenium中的sendKeys 方法就可以解决. 但是我们也会碰到下面的时间日期控件(这个时候这个文本框是不允许我们输入时间的)如图 ...
- java订单生成工具类
欢迎来到付宗乐个人博客网站.本个人博客网站提供最新的站长新闻,各种互联网资讯. 还提供个人博客模板,最新最全的java教程,java面试题.在此我将尽我最大所能将此个人博客网站做的最好! 谢谢大家,愿 ...
- Mybatis中使用PageHelper插件进行分页
分页的场景比较常见,下面主要介绍一下使用PageHelper插件进行分页操作: 一.概述: PageHelper支持对mybatis进行分页操作,项目在github地址: https://github ...