YIFEI 欢迎你

多一份不为什么的坚持!

hdu 1241(DFS/BFS)

Oil Deposits

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


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
 

 

Recommend
Eddy
 

 

Statistic | Submit | Discuss | Note

思路:'@'代表有石油,然后只要上下相邻或者左右相邻,或者对角相邻都算是一块石油,问有几块石油储备。

就是用dfs或者bfs就行了,得用栈或者队列来模拟。我写的是DFS版

不知道为什么在用scanf读入字符数组时,题目的第四组测试数据总是读入回车。。。getchar也不管用。

换cin好了。。。

 

复制代码
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <stack>
 5 #include <queue>
 6 #include <conio.h>
 7 
 8 using namespace std;
 9 
10 typedef struct info{
11     int i,j;
12 }location;
13 
14 int m,n;
15 int countt;
16 char t;
17 char oilmap[105][105];
18 stack<location> s;
19 
20 int calx[8]={-1,-1,-1,0,0,1,1,1};
21 int caly[8]={-1,0,1,-1,1,-1,0,1};
22 
23 void exploit(int i,int j){
24     location temp={i,j};
25     location now,next;
26     s.push(temp);
27     while(!s.empty()){
28         now=s.top();
29         s.pop();
30         oilmap[now.i][now.j]='*';
31         for(int k=0;k<8;k++){
32             next.i=now.i+calx[k];
33             next.j=now.j+caly[k];
34             if(next.i>=0 && next.i<m && next.j>=0 && next.j<n){
35                 if(oilmap[next.i][next.j]=='@'){
36                     s.push(next);
37                 }
38             }
39         }
40     }
41 
42 }
43 
44 int main()
45 {
46     while(~scanf("%d %d",&m,&n)){
47         getchar();
48         if(m==0) {break;}
49         for(int i=0;i<m;i++){
50             for(int j=0;j<n;j++){
51                 cin>>oilmap[i][j];
52             }
53         }
54         countt=0;
55         for(int i=0;i<m;i++){
56             for(int j=0;j<n;j++){
57                 if(oilmap[i][j]=='@'){
58                     exploit(i,j);
59                     countt++;
60                 }
61             }
62         }
63         printf("%d\n",countt);
64     }
65     return 0;
66 }
复制代码
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27179557/article/details/79962823
上一篇以后转到博客园去了
下一篇HDU - 1495 非常可乐(bfs)
想对作者说点什么? 我来说一句

DFSBFS的C++实现

2012年11月30日 1.07MB 下载

DFSBFS用来干什么

2013年05月01日 210KB 下载

无向图的DFSBFS遍历

2010年07月05日 888B 下载

图的dfsbfs遍历-c语言版

2017年06月11日 239KB 下载

DFS \BFS生成树

2009年06月28日 4KB 下载

C语言实现图的BFSDFS

2014年02月21日 220KB 下载

数据结构图bfsDfs遍历算法

2014年12月09日 1KB 下载

图的遍历示例(包括DFSBFS

2012年03月16日 6KB 下载

没有更多推荐了,返回首页

关闭
关闭