1. 程式人生 > >Oil Deposits hdu-1241 DFS

Oil Deposits hdu-1241 DFS

lib part str std math.h row nal sent print

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.

InputThe 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.
OutputFor 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

DFS 最基本的題目 兩個for循環,如果地圖上出現@,那麽把他變成*,用遞歸查找他附近的@,直到找不到為止。

技術分享


技術分享

 1 #include <iostream>
 2 using namespace std;
 3 #include<string.h>
 4 #include<set>
 5 #include<stdio.h>
 6 #include<math.h>
 7
#include<queue> 8 #include<map> 9 #include<algorithm> 10 #include<cstdio> 11 #include<cmath> 12 #include<cstring> 13 #include <cstdio> 14 #include <cstdlib> 15 #include<stack> 16 char a[110][110]; 17 int b[8][2]={1,0,-1,0,0,1,0,-1,1,1,1,-1,-1,1,-1,-1
}; 18 int n,m; 19 int sum=0; 20 void dfs(int x,int y) 21 { 22 a[x][y]=*; 23 //cout<<x<<"_"<<y<<"_"<<sum<<endl; 24 for(int i=0;i<8;i++) 25 { 26 if(x+b[i][0]>0&&x+b[i][0]<=n&&y+b[i][1]>0&&y+b[i][1]<=m&&a[x+b[i][0]][y+b[i][1]]==@) 27 { 28 int x1,y1; 29 x1=x+b[i][0]; 30 y1=y+b[i][1]; 31 dfs(x1,y1); 32 } 33 } 34 35 return ; 36 } 37 int main() 38 { 39 while(scanf("%d%d",&n,&m),n,m) 40 { 41 for(int i=1;i<=n;i++) 42 for(int j=1;j<=m;j++) 43 cin>>a[i][j]; 44 sum=0; 45 for(int i=1;i<=n;i++) 46 for(int j=1;j<=m;j++) 47 { 48 if(a[i][j]==@) 49 { 50 sum++; 51 dfs(i,j); 52 } 53 } 54 printf("%d\n",sum); 55 } 56 return 0; 57 }
View Code

優化了輸入 時間變0.

技術分享

技術分享
 1 #include <iostream>
 2 using namespace std;
 3 #include<string.h>
 4 #include<set>
 5 #include<stdio.h>
 6 #include<math.h>
 7 #include<queue>
 8 #include<map>
 9 #include<algorithm>
10 #include<cstdio>
11 #include<cmath>
12 #include<cstring>
13 #include <cstdio>
14 #include <cstdlib>
15 #include<stack>
16 char a[110][110];
17 int b[8][2]={1,0,-1,0,0,1,0,-1,1,1,1,-1,-1,1,-1,-1};
18 int n,m;
19 int sum=0;
20 void dfs(int x,int y)
21 {
22     a[x][y]=*;
23     //cout<<x<<"_"<<y<<"_"<<sum<<endl;
24     for(int i=0;i<8;i++)
25     {
26         if(x+b[i][0]>0&&x+b[i][0]<=n&&y+b[i][1]>0&&y+b[i][1]<=m&&a[x+b[i][0]][y+b[i][1]]==@)
27         {
28             int x1,y1;
29             x1=x+b[i][0];
30             y1=y+b[i][1];
31             dfs(x1,y1);
32         }
33     }
34 
35     return ;
36 }
37 int main()
38 {
39     while(scanf("%d%d",&n,&m),n,m)
40     {
41         for(int i=1;i<=n;i++)
42                 scanf("%s",a[i]+1);
43         sum=0;
44          for(int i=1;i<=n;i++)
45             for(int j=1;j<=m;j++)
46                 {
47                     if(a[i][j]==@)
48                     {
49                         sum++;
50                         dfs(i,j);
51                     }
52                 }
53         printf("%d\n",sum);
54     }
55     return 0;
56 }
View Code

Oil Deposits hdu-1241 DFS