Problem Description
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 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.
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
题意:给你一个字符矩阵,求连在一起的@符有多少堆;
解题思路:深度优先搜多,找到一个节点开始往下搜,搜到的全都标记,然后搜不到了就停止,记录搜索到的次数;
感悟:第一次自己写的搜索代码;成功跑的那一刻有点兴奋!!!
代码:
#include

#include

#include

#define maxn 110



using namespace std;

char mapn[maxn][maxn];

int
direction[8][2]={{-1,0},{1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,-1},{1,1}};

bool visit[maxn][maxn];

int m,n,sum=0;

bool isbound(int a,int b)//判断边界

{

   
if(a<1||a>m||b<1||b>n)return true;

    return
false;

}


void
dfs(int x,int y)

{

    for(int
i=0;i<8;i++)

    {

       
if(mapn[x+direction[i][0]][y+direction[i][1]]=='*')

           
continue;

       
if(isbound(x+direction[i][0],y+direction[i][1]))

           
continue;

       
if(visit[x+direction[i][0]][y+direction[i][1]])

           
continue;

       
visit[x+direction[i][0]][y+direction[i][1]]=true;

       
dfs(x+direction[i][0],y+direction[i][1]);

    }

}

int
main()

{

   
//freopen("in.txt","r",stdin);

   
while(~scanf("%d%d\n",&m,&n)&&(m||n))//这地方应该有一个回车

{

       
//printf("m=%d n=%d\n",m,n);

       
memset(visit,false,sizeof(visit));

       
sum=0;

       
for(int i=1;i<=m;i++)

       
{

           
for(int j=1;j<=n;j++)

           
{

               
scanf("%c",&mapn[i][j]);

           
}

           
scanf("\n");//每行输入结束都应该有一个回车

       
}

       
for(int i=1;i<=m;i++)

           
for(int j=1;j<=n;j++)

           
{

               
if(mapn[i][j]=='@'&&!visit[i][j])

               
{

                   
dfs(i,j);

                   
visit[i][j]=true;

                   
sum++;

               
}

           
}

       
printf("%d\n",sum);

    }

}






Oil Deposits的更多相关文章

  1. Oil Deposits

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

  2. Oil Deposits(dfs)

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

  3. 2016HUAS暑假集训训练题 G - Oil Deposits

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  4. uva 572 oil deposits——yhx

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

  5. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  6. hdu1241 Oil Deposits

    Oil Deposits                                                 Time Limit: 2000/1000 MS (Java/Others)  ...

  7. 杭电1241 Oil Deposits

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

  8. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  9. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

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

  10. UVa572 Oil Deposits DFS求连通块

      技巧:遍历8个方向 ; dr <= ; dr++) ; dc <= ; dc++) || dc != ) dfs(r+dr, c+dc, id); 我的解法: #include< ...

随机推荐

  1. spring cloud+dotnet core搭建微服务架构:服务发现(二)

    前言 上篇文章实际上只讲了服务治理中的服务注册,服务与服务之间如何调用呢?传统的方式,服务A调用服务B,那么服务A访问的是服务B的负载均衡地址,通过负载均衡来指向到服务B的真实地址,上篇文章已经说了这 ...

  2. 最详细的cookie和浏览隐私之间的关系

    本文所说的"cookie",指的是浏览器相关的 cookie(也叫"HTTP cookie"). 浏览器 cookie 的主要功能是:帮助网站保存一些小片段的信 ...

  3. taobao_api项目开坑,自主完成淘宝主要接口的开发-版本:卖家版(非淘宝api)

    项目名称:taobao_api 项目目的:独立实现各个淘宝操作的相关api,不依赖淘宝提供的api,而是自己实现接口 前期实现接口:已付款订单查询(自动更新), 订单发货 , 订单备注 应用场景:中小 ...

  4. javaWeb正则表达式

    对于web来说,字符串的处理特别重要,而正则表达式是对字符串处理的利器,在字符过滤,验证方面都能看到她的身影. 今天需要处理一段json字符串,在用String.replaceAll的过程中,遇到了正 ...

  5. EOutOfResources字符异常

    近日,用Delphi编程时,遇到一个莫名其妙的异常:EOutOfResources,这是一个可以重复再现的异常.开始以为是程序中创建的对象太多,导致占用了过多的资源,引起了这个异常.于是在代码中将许多 ...

  6. 【充分利用你的Azure】将Azure用作云计算平台(1)

    本文将围绕几个步骤来讲. 因为本人是MSP,微软送了150刀的额度给我随便使用.这篇文章是要讲将Azure用作云计算平台,对于我来说,我是做机器学习的,那么Azure就要有机器学习的平台. 本文的目的 ...

  7. 电脑IP地址被占用如何释放?

    回车后,关机,等待5分钟左右再开机,就释放掉了.

  8. C语言判断电脑的大、小端机

    #include int main() { int x = 0x1234; if (char(x) == 0x34)  {   printf("小端机!\n");  }  else ...

  9. Python之scrapy安装

    1.按照网上教程一步步实验,运行时报错: 'HtmlResponse' object has no attribute 'xpath' in scrapy 个人使用的是scrapy0.14.4,搜索得 ...

  10. Java高新技术 反射机制

     Java高新技术 反射机制 知识概要:                   (1)反射的基石 (2)反射 (3)Constructor类 (4)Field类 (5)Method类 (6)用反射方 ...