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. [python学习笔记] pyqt5下载与安装

    下载 命令安装 pip3 install PyQt5 但是我这里老安装失败 失败问题 host='pypi.python.org', port=443): Read timed out 方案1:加大命 ...

  2. [转]iOS 应用程序的生命周期

    OS的应用程序的生命周期,还有程序是运行在前台还是后台,应用程序各个状态的变换,这些对于开发者来说都是很重要的. iOS系统的资源是有限的,应用程序在前台和在后台的状态是不一样的.在后台时,程序会受到 ...

  3. 匹配替换第n个字符串

    var name = "questions[0][question]",i=0; name.replace(/\[.+?\]/g, function(match, pos, ori ...

  4. session写入memcache

    1 <?php 2 class MemSession{ 3 private static $handler = null; 4 private static $lifetime = null; ...

  5. Linux入门之常用命令(14) kill

    Linux kill 命令使用详解 功能说明:删除执行中的程序或工作. 语 法:kill [-s <信息名称或编号>][程序] 或 kill [-l <信息编号>] 补充说明: ...

  6. Easy sssp

    Easy sssp 时间限制: 1 Sec  内存限制: 128 MB提交: 103  解决: 20[提交][状态][讨论版] 题目描述 输入数据给出一个有N(2  < =  N  < = ...

  7. oracle排序的几种方法

    1.创建数据库表 CREATE TABLE USER_INFO(  USERID      VARCHAR2(10 BYTE)                 NOT NULL,  USERNAME  ...

  8. 机器学习理论提升方法AdaBoost算法第一卷

    AdaBoost算法内容来自<统计学习与方法>李航,<机器学习>周志华,以及<机器学习实战>Peter HarringTon,相互学习,不足之处请大家多多指教! 提 ...

  9. yii2-swiftmailer入门

    1. 安装 用yii 2.0框架,默认会有这个扩展 composer require --prefer-dist yiisoft/yii2-swiftmailer 修改composer.json,re ...

  10. IPSec协议

    IPSec协议:IPsec将IP数据包的内容先加密再传输,即便中途被截获,由于缺乏解密数据包所必要的密钥,攻击者也无法获取里面的内容. 传输模式和隧道模式:IPsec对数据进行加密的方式有两种:传输模 ...