HDU 1241 Oil Deposits <BFS>
来源:互联网 发布:吸引的淘宝优惠券名字 编辑:程序博客网 时间:2024/06/09 18:58
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3087 Accepted Submission(s): 1765
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
0122
这个题BFS,DFS都可以求解,这里用BFS尝试做了一次
思路:
对于每个有油区域,找出所有与它同属一个oil pocket的有油区域,最后计算一共有多少个oil pocket。
?怎样去找出所有与它同属一个oil pocket
BFS:找到一个起点;
从这个点出发,枚举四周寻找有油区域;
顺序从找到的新的区域出发,循环上述过程,直到没有新的区域加入。
?怎样去标志同属一个oil pocket的有油区域
设置一个访问标志代表此区域有没有被包含过,这样的话调用BFS的次数就= oil pocket的数目。
当然DFS也是可以这样做的
对于每个有油区域,找出所有与它同属一个oil pocket的有油区域,最后计算一共有多少个oil pocket。
?怎样去找出所有与它同属一个oil pocket
BFS:找到一个起点;
从这个点出发,枚举四周寻找有油区域;
顺序从找到的新的区域出发,循环上述过程,直到没有新的区域加入。
?怎样去标志同属一个oil pocket的有油区域
设置一个访问标志代表此区域有没有被包含过,这样的话调用BFS的次数就= oil pocket的数目。
当然DFS也是可以这样做的
#include <iostream>#include <cstring>#include <stack>#include <cstdio>#include <cmath>#include <queue>#include <algorithm>#include <vector>#include <set>#include <map>const double eps=1e-8;const double PI=acos(-1.0);using namespace std;struct Node{ int x,y;}node;int n,m;char a[150][150];int c[][2]={{0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};int vis[105][105];void bfs(int i,int j){ queue<Node> q; node.x=i; node.y=j; q.push(node); vis[i][j]=1; while(!q.empty()){ Node temp,tp=q.front(); q.pop(); for(int k=0;k<8;k++){ temp.x=tp.x+c[k][0]; temp.y=tp.y+c[k][1]; if(temp.x<0||temp.y<0||temp.x>=n||temp.y>=m) continue; if(!vis[temp.x][temp.y]&&a[temp.x][temp.y]=='@'){ vis[temp.x][temp.y]=1; q.push(temp); } } }}int main(){ while(~scanf("%d%d",&n,&m)&&(n||m)) { memset(vis,0,sizeof(vis)); int ans=0; int p1,p2; for(int i=0; i<n; i++) { scanf("%s",a[i]); } for(int i=0;i<n;i++) for(int j=0; j<m; j++) { if(a[i][j]=='@'&&!vis[i][j]){ ans++; bfs(i,j); } } printf("%d\n",ans); } return 0;}
阅读全文
0 0
- hdu 1241 Oil Deposits - bfs
- hdu 1241Oil Deposits(BFS)
- hdu 1241 Oil Deposits (BFS)
- HDU 1241 BFS-Oil Deposits
- 【BFS】HDU 1241 Oil Deposits
- HDU 1241 Oil Deposits <BFS>
- POJ 1562/HDU 1241 Oil Deposits(BFS)
- HDU-#1241 Oil Deposits(BFS & DFS)
- HDU 1241 Oil Deposits[dfs || bfs]
- HDU 1241 Oil Deposits (DFS/BFS)
- L - Oil Deposits HDU 1241 基础BFS
- HDU 1241 Oil Deposits (DFS+BFS)
- HDU 1241/UVa572 Oil Deposits(DFS&BFS)
- HDU-1241-Oil Deposits(BFS)
- hdu 1241 Oil Deposits (DFS)(BFS)
- HDU 1241:Oil Deposits(BFS)
- 【hdu 1241】 Oil Deposits (dfs+bfs)
- HDU 1241Oil Deposits
- Tensorflow一些常用基本概念与函数
- MySql计算两个日期的时间差函数
- Bootstrap基础9——导航条navbar,面包屑breadcrumb,分页pagination
- rac备份及恢复的重要概念之一——Redo Threads和Streams
- jquery 如何默认选中 input文本框-yellowcong
- HDU 1241 Oil Deposits <BFS>
- 利用java获取本机IP地址的方法
- 人脸识别在智慧城市建设中的应用如何?
- C# /"什么意思 \" ~\
- swagger2常用注解说明
- 基本数据类型和引用数据类型
- 二分图的最大匹配、完美匹配和匈牙利算法
- Java序列化的几种方式
- 检查ndk是否安装