1334: Oil Deposits

时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte
总提交: 555            测试通过:404

描述

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.

输入

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.

输出

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.

样例输入

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

样例输出

0
1
2
2

题目来源

Mid-Central USA 1997

题解:

题目意思是判断油带的数量,相邻油田属于同一油带,相邻,是指两个小正方形区域上下、左右、左上右下或左下右上同为’@’。

简单深搜。

#include<iostream>
using namespace std;
int x,y,i,j,s;
char arr[101][101];
int g[8]={0,0,1,1,1,-1,-1,-1};
int q[8]={1,-1,0,-1,1,0,1,-1};
int dfs(int a,int b)
{
if(a<0||b<0||a>=x||b>=y)return 0;
if(arr[a][b]=='*')return 0;
arr[a][b]='*';
for(int k=0;k<8;k++)
{
dfs(a+g[k],b+q[k]);
}
}
int main()
{
while(cin>>x>>y)
{
if(x==0&&y==0)break;
s=0;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cin>>arr[i][j];
}
}
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
if(arr[i][j]=='@')
{
s++;
dfs(i,j);
}
}
}
printf("%d\n",s);
}
}

TOJ1334的更多相关文章

随机推荐

  1. robots笔记以免忘记

    html头部标签写法: <meta name="robots" content="index,follow" /> content中的值决定允许抓取 ...

  2. MicroERP如何配置网络应用

    概述:        MicroERP支持多种数据库运行,其中单机版数据库格式为Access,网络版数据库可以SQLServer.Oracle.MySQL等.以下分别以Access和SQLServer ...

  3. aspnet Global文件概况

    <%@ Application Language="C#" %> <script runat="server">        void ...

  4. play(1) 第一个简单的应用

    去年去了一家公司,公司要求要回使用play,所以在几天的时间内猛学习了一段时间play,发现play!有很多优点:简单,小巧,高开发效率,集成很多功能,容易调试.现在虽然已经不在那家公司,没有使用pl ...

  5. [make]makefile使用积累

    [注]:文中所指手册皆为GNU make Version 4.1 1.make的一般特性 1.1.Makefiles的构成 Makefiles包含五种元素: 显式规则(explicit rules), ...

  6. C# 里的if/switch

    今天又重新翻了翻C# Step by Step if 语句 if(bool 表达式) { 语句块: } else { 语句块: } switch(day) { case 0: dayName=&quo ...

  7. Java 和 C+

    文不对题,啦啦啦~~~ 第一次感到在windows平台java应用发布的无力,平时自己自写自用都是在自己电脑上,没什么感觉.如今要发布个给人用,打包应用.附加jre,这过程还得多加几行字说明,另人特么 ...

  8. (转)Apache实现反向代理负载均衡

    说到负载均衡LVS这套技术,有很多种实现方法. 本文所说,主要就是利用apache服务器实现反向代理,实现负载均衡. 首先,传统的正向代理如下图所示,正如我们用的游戏加速代理,大多的个人PC把请求发给 ...

  9. Maven项目下java.lang.ClassNotFoundException的解决方法

    问题背景: Maven的project下,项目中已经引用了相应的jar包.Java class中没有语法错误,在执行时报ClassNotFound.检查了Maven的pom.xml,依赖引入正常. 错 ...

  10. Windows7+VS2010下OpenGL的环境配置

    http://johnhany.net/2014/01/environment-for-opengl-with-vs2010/ OpenGL(Open Graphics Library)是一个开放的. ...