hdu 1241dfs Oil Deposits

hdu 1241dfs Oil Deposits

 

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

Total Submission(s): 10610    Accepted Submission(s): 6151

 

 

Problem Description

The GeoSurvComp geologic survey company isresponsible for detecting underground oil deposits. GeoSurvComp works with onelarge rectangular region of land at a time, and creates a grid that divides theland into numerous square plots. It then analyzes each plot separately, usingsensing equipment to determine whether or not the plot contains oil. A plotcontaining oil is called a pocket. If two pockets are adjacent, then they arepart of the same oil deposit. Oil deposits can be quite large and may containnumerous pockets. Your job is to determine how many different oil deposits arecontained 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 columnsin the grid, separated by a single space. If m = 0 it signals the end of theinput; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following thisare m lines of n characters each (not counting the end-of-line characters).Each character corresponds to one plot, and is either `*', representing the absenceof oil, or `@', representing an oil pocket.

 

 

Output

For each grid, output the number ofdistinct oil deposits. Two different pockets are part of the same oil depositif they are adjacent horizontally, vertically, or diagonally. An oil depositwill not contain more than 100 pockets.

 

 

Sample Input

1 1

*

3 5

*@*@*

**@**

*@*@*

1 8

@@****@*

5 5

****@

*@@*@

*@**@

@@@*@

@@**@

0 0

 

 

Sample Output

0

1

2

2

题解:

@,表示石油,*表示石头,八个方向,上下左右对角,这道题是用不回溯的dfs来解决的,很简单每个点都搜它的八个方位,如果存在解决@则深搜下去。用visit记录。

其实时间复杂度很小,因为n,m<100,计算量最大也就是100*100*8。不存在超时的情况。

源代码:

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <string>

using namespacestd;

int n,m;

char map[105][105];

bool visit[105][105];

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

 

bool judge(intx,int y)

{

      if(x>= n ||x < 0||y >= m||y < 0)

           returnfalse;

      if(map[x][y]== '*')

           returnfalse;

      if(visit[x][y])

           returnfalse;

 

      returntrue;

}

void dfs(intx,int y)

{

      visit[x][y]= true;

      intxx,yy;

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

      {

           xx= x + dir[i][0];

           yy= y + dir[i][1];

           if(judge(xx,yy))

                 dfs(xx,yy);

      }

}

int main()

{

      while(scanf("%d%d",&n,&m)!= EOF&&m)

      {

           getchar();

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

           for(inti = 0;i < n;i++)

           {

                 scanf("%s",map[i]);

           }

 

           intnum = 0;

           for(inti = 0;i < n;i++)

                 for(intk = 0;k < m;k++)

                      if(!visit[i][k]&& map[i][k] == '@')

                      {

                            num++;

                            dfs(i,k);

                      }

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

      }

      return0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值