前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ---1241Oil Deposits(dfs)

HDUOJ---1241Oil Deposits(dfs)

作者头像
Gxjun
发布2018-03-22 13:08:08
5970
发布2018-03-22 13:08:08
举报
文章被收录于专栏:mlml

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10644    Accepted Submission(s): 6176

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

Source

Mid-Central USA 1997

代码:

代码语言:javascript
复制
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 char map[101][101];
 5 int dir[8][2]={
 6     {1,0},
 7     {-1,0},
 8     {0,1},
 9     {0,-1},
10     {1,1},
11     {-1,-1},
12     {1,-1},
13     {-1,1}
14 };
15  int n,m;
16 void dfs(int st,int en)
17 {
18      for(int i=0;i<8;i++)
19      {
20          if(st+dir[i][0]>=n&&st+dir[i][0]<0) continue;
21          if(en+dir[i][0]>=m&&en+dir[i][0]<0) continue;
22          if(map[st+dir[i][0]][en+dir[i][1]]=='@')
23          {
24             map[st+dir[i][0]][en+dir[i][1]]='*';
25             dfs(st+dir[i][0],en+dir[i][1]);
26          }
27      }
28 }
29 int main()
30 {
31   int i,j,cnt;
32   while(scanf("%d%d",&n,&m),n+m)
33   {
34       cnt=0;
35       for(i=0;i<n;i++)
36           scanf("%s",map[i]);
37       for(i=0;i<n;i++)
38       {
39           for(j=0;j<m;j++)
40           {
41               if(map[i][j]=='@')
42               {
43                 dfs(i,j);
44                 cnt++;
45               }
46           }
47       }
48       printf("%d\n",cnt);
49   }
50  return 0;
51 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-04-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Oil Deposits
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档