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的更多相关文章

  1. ACM:油田(Oil Deposits,UVa 572)

    /* Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  2. 【紫书】Oil Deposits UVA - 572 dfs求联通块

    题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...

  3. 油田 (Oil Deposits UVA - 572)

    题目描述: 原题:https://vjudge.net/problem/UVA-572 题目思路: 1.图的DFS遍历 2.二重循环找到相邻的八个格子 AC代码: #include <iostr ...

  4. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  5. uva 572 oil deposits——yhx

    Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...

  6. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  7. UVa 572 Oil Deposits(DFS)

     Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil ...

  8. Oil Deposits

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  9. Oil Deposits(dfs)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

随机推荐

  1. javascript+jQuery补充

    一.jQuery事件绑定 <div class='c1'> <div> <div class='title'>菜单一</div> <div cla ...

  2. 2、大型项目的接口自动化实践记录--接口测试简介及RequestsLibrary关键字简介

    1.接口测试简介 1)先简单介绍下接口测试,那么什么是接口测试呢? 百科的回答:接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点. 看起来有 ...

  3. 如何思考博弈dp

    两个人的规则是否一致 若仅仅是先后的差别 我们可用dp解决一般思考一个子状态 对于当前的那个状态 我们进行什么样的操作 已知什么

  4. 手摸手,带你用vue实现后台管理权限系统及顶栏三级菜单显示

    手摸手,带你用vue实现后台管理权限系统及顶栏三级菜单显示 效果演示地址 项目demo展示 重要功能总结 权限功能的实现 权限路由思路: 根据用户登录的roles信息与路由中配置的roles信息进行比 ...

  5. JavaScript数据结构——树的实现

    在计算机科学中,树是一种十分重要的数据结构.树被描述为一种分层数据抽象模型,常用来描述数据间的层级关系和组织结构.树也是一种非顺序的数据结构.下图展示了树的定义: 在介绍如何用JavaScript实现 ...

  6. Eclipse高级操作 远程调试

    Eclipse高级操作 远程调试 JPDA是SUN JDK自带的远程调试机制.它提供了一套标准的调试接口,可以从虚拟机一级允许外界用特定协议探测虚拟机内部的运作细节.只要你装了JDK1.2以上的SUN ...

  7. Javaweb表格加载---DataTable

    Datatables是一款jquery表格插件.它是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能. 使用 jQuery Datatable 构造数据列表,并且增加或者隐藏相应的列,已达 ...

  8. 解决树莓派烧录系统后没有boot文件,只出现盘符问题

    首先,如果下图情况,说明你没有烧录好,继续向下看 放一张安装成功的图片 出现这个的原因是因为前期没有烧录好,它会回滚到img文件中,如果中途退出,它会写入到img文件中 正确文件大小(Raspbian ...

  9. javaScript基础-04 对象

    一.对象的基本概念 对象是JS的基本数据类型,对象是一种复合值,它将很多值(原始值或者对象)聚合在一起,可通过名字访问这些值,对象也可看做是属性的无序集合,每个属性都是一个名/值对.对象不仅仅是字符串 ...

  10. 用原生JS实现AJAX和JSONP

    前端开发在需要与后端进行数据交互时,为了方便快捷,都会选择JQuery中封装的AJAX方法,但是有些时候,我们只需要JQuery的AJAX请求方法,而其他的功能用到的很少,这显然是没必要的.其实,原生 ...