码迷,mamicode.com
首页 > 其他好文 > 详细

Oil Deposits hdu-1241 DFS

时间:2017-09-05 23:12:27      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:img   put   sed   stream   output   images   .com   splay   return   

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

标签:img   put   sed   stream   output   images   .com   splay   return   

原文地址:http://www.cnblogs.com/dulute/p/7482286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!