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. ext container的使用的场景

    container 是 panel 简化,他称之为容器,而panel则是面板. 如果不需要类似Ext.panel.Panel,Ext.window.Window和Ext.tab.Panel 等功能,则 ...

  2. JVM系列(1)- JVM常见参数及堆内存分配

    常见参数配置 基于JDK1.6 -XX:+PrintGC 每次触发GC的时候打印相关日志 -XX:+UseSerialGC 串行回收模式 -XX:+PrintGCDetails 打印更详细的GC日志 ...

  3. Install eclipse ns3 in ubuntu 14.04

    1. NS3 install 参考NS3 tutorial即可. 2.eclipse 2.1下载 下载地址:http://www.eclipse.org/downloads/              ...

  4. 【0805 | Day 8】Python进阶(二)

    列表类型内置方法 一.列表类型内置方法(list) 用途:多个爱好.多个武器.多种化妆品 定义:[ ]内可以有多个任意类型的值,逗号分隔元素 # my_boy_friend = list(['jaso ...

  5. Redis集群与spring的整合

    上一篇详细的赘述了Redis的curd操作及集群的搭建.下面我们开始将他整合到我们实际的项目中去.我的项目采用的是标准的ssm框架,ssm框架这里不说,直接开始整合. 首先在maven管理中将我们的j ...

  6. 关于JSP页面的静态包含和动态包含

    JSP中有两种包含:静态包含:<%@include file="被包含页面"%> 和 动态包含:<jsp:include page="被包含页面&quo ...

  7. js五子棋游戏代码分享

    HTML代码 <canvas id="game"></canvas> CSS代码 * { margin: 0; padding: 0; } #game { ...

  8. threejs 学习之

    主要内容: 使用 threejs 创建 20x20 的网格,鼠标移动时,方块跟随移动,点击时在网格任意位置放置方块,按 shift 时,删除当前位置方块. 流程如下: 创建网格 创建一个与网格同样尺寸 ...

  9. Python --深入浅出Apriori关联分析算法(二) Apriori关联规则实战

    上一篇我们讲了关联分析的几个概念,支持度,置信度,提升度.以及如何利用Apriori算法高效地根据物品的支持度找出所有物品的频繁项集. Python --深入浅出Apriori关联分析算法(一) 这次 ...

  10. 【Fishing Master HDU - 6709 】【贪心】

    题意分析 题意:题目给出n条鱼,以及捕一条鱼所用的时间k,并给出煮每一条鱼的时间,问抓完并煮完所有鱼的最短时间. 附题目链接 思路: 1.捕第一条鱼的时间是不可避免的,煮每条鱼的时间也是不可避免的,这 ...