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. C# Request中修改header信息

    var headers = app.Context.Request.Headers; Type hdr = headers.GetType(); PropertyInfo ro = hdr.GetPr ...

  2. 学习asp.net比较完整的流程[转]

    如果你已经有较多的面向对象开发经验,跳过以下这两步: 第一步 掌握一门.NET面向对象语言,C#或VB.NET 我强烈反对在没系统学过一门面向对象(OO)语言的前提下去学ASP.NET. ASP.NE ...

  3. POJ 题目2411 Mondriaan's Dream(状压DP)

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13519   Accepted: 787 ...

  4. asp.net生成随机密码

    public string RandCode(int n) { char[] arrChar = new char[]{ 'a','b','d','c','e','f','g','h','i','j' ...

  5. 移动端1px

    移动端不同尺寸设备dpi不同,会造成1px线条不同程度的缩放,可利用媒体查询device-pixel-ratio,进行不同情况匹配: @media(-webkit-min-device-pixel-r ...

  6. js⑤

    ##返回在制定位置的的字符  var result = str.charAt(1);  console.log(result);  console.log(str[1]); ##返回在指定的位置的字符 ...

  7. 函数的caller属性

    今天我在这里通过一个例子介绍一下函数自身的call属性. 例: function whoCallMe(){ alert("My caller is" + whoCallMe.cal ...

  8. export a java project to runable jar

    When a java project needs to be transfered to another machine, e.g. vps, we need to export it to a r ...

  9. Why sql is called structured query language?1 - 12

    SQL has much to do with a researcher at IBM, Edgar F. (Ted) Codd, an Oxford-trained mathematician, w ...

  10. C语言程序设计第十一次作业

    同学们,一晃一个学期就过去了,第一节课时,我曾做过一个调查,没有一个同学在中学阶段接触过程序设计,也就是说,那时,大家都是零基础,或许只是听说过"C语言"这个词,但其他便一无所知了 ...