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( ...
随机推荐
- ubuntu搭建环境
1.终端输入 sudo apt- add-apt-repository ppa:ondrej/php sudo add-apt-repository ppa:ondrej/php sudo apt ...
- 彻底理解kubernetes CNI
kubernetes各版本离线安装包 CNI接口很简单,特别一些新手一定要克服恐惧心里,和我一探究竟,本文结合原理与实践,认真读下来一定会对原理理解非常透彻. 环境介绍 我们安装kubernetes时 ...
- dubbo负载均衡是如何实现的?
dubbo的负载均衡全部由AbstractLoadBalance的子类来实现 RandomLoadBalance 随机 在一个截面上碰撞的概率高,但调用量越大分布越均匀,而且按概率使用权重后也比较均匀 ...
- 集合(Collection解析 Set List Map三大集合运用)
集合的概念: 集合是包含多个对象的简单对象,所包含的对象称为元素.集合里面可以包含任意多个对象,数量可以变化:同时对对象的类型也没有限制,也就是说集合里面的所有对象的类型可以相同,也 ...
- Android 虹软人脸识别SDK-人脸对比
准备 : 登录官方网站,获取SDK,进行个人验证后新建项目,获取APP_ID,和SDK_KEY: https://ai.arcsoft.com.cn/ucenter/resource/build/in ...
- RBF神经网络
RBF神经网络 RBF神经网络通常只有三层,即输入层.中间层和输出层.其中中间层主要计算输入x和样本矢量c(记忆样本)之间的欧式距离的Radial Basis Function (RBF)的值,输出层 ...
- Spring Cloud下基于OAUTH2+ZUUL认证授权的实现
Spring Cloud下基于OAUTH2认证授权的实现 在Spring Cloud需要使用OAUTH2来实现多个微服务的统一认证授权,通过向OAUTH服务发送某个类型的grant type进行集中认 ...
- Simple Windows Service in C++
本文是来自CodeProject中的一篇名为Simple Windows Service in C++的译文,原文地址为:https://www.codeproject.com/Articles/49 ...
- C#连接sqlserver分页查询的两个简单的方法
/// <summary> /// 分页查询函数 /// </summary> /// <param name="co ...
- 颜色下拉菜单(combox)
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using Sys ...