HDUOJ---1241Oil Deposits(dfs)

时间:2022-05-05
本文章向大家介绍HDUOJ---1241Oil Deposits(dfs),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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

代码:

 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("%dn",cnt);
49   }
50  return 0;
51 }