HDU 1241 Oil Deposits (基础DFS 或者 BFS)
来源:互联网 发布:淘宝恶意退款技巧 编辑:程序博客网 时间:2024/06/06 03:03
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 23051 Accepted Submission(s): 13296
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
Source
Mid-Central USA 1997
题解:给你一个油田的图,然后计算这个图的连通块。
图的遍历.有DFS和BFS.
由于DFS更容易编写,一般用DFS找连通块
:从每个"@"格子出发.递归遍历它周围的"@"格子.每次访问一个格子的时候就给它写上''连通分量编号",即标志一下就ok了,这样就可以在访问之前检查它是否有了编号,从而避免同一格子访问多次。
AC代码:DFS
#include<iostream>#include<stdio.h>#include<cstring>#include<algorithm>using namespace std;char map[110][110];int dir[8][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,-1},{-1,1},{1,-1}};int vis[110][110];int next_x,next_y;void dfs(int x,int y){map[x][y]='*'; //标志一下,避免重复计数 for(int k=0;k<8;k++){next_x=x+dir[k][0];next_y=y+dir[k][1];if(map[next_x][next_y]=='@'){dfs(next_x,next_y);}}}int main(){int n,m;while(cin>>n>>m,m&&n){int count=0;for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>map[i][j];}}for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(map[i][j]=='@'){count++;dfs(i,j);}}}printf("%d\n",count);}return 0;}
#include<stdio.h>#include<cstring>#include<queue>using namespace std;int n,m;char a[100][100];int sum;int dir[8][2]={1,0,1,1,1,-1,0,1,0,-1,-1,0,-1,1,-1,-1}; //8个方向struct node{ int x; int y;};int OK(int x,int y){ if(x>=0&&x<n&&y>=0&&y<m) return true; return false;}void bfs(int x,int y){ int i; node now,next; queue<node> q; now.x=x; now.y=y; q.push(now); while(!q.empty()) { now=q.front(); q.pop(); for(i=0;i<8;i++) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; if(OK(next.x,next.y)&&a[next.x][next.y]=='@') //判断油田和越界 { a[next.x][next.y]='*'; //油田群遍历,标记 q.push(next); } } }}int main(){ int i,j; while(scanf("%d%d",&n,&m)&&m) { sum=0; for(i=0;i<n;i++) scanf("%s",a[i]); for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(a[i][j]=='@') //找到油田 { sum++; bfs(i,j); } } } printf("%d\n",sum); } return 0;}
3 0
- HDU 1241 Oil Deposits (基础DFS 或者 BFS)
- HDU-#1241 Oil Deposits(BFS & DFS)
- HDU 1241 Oil Deposits[dfs || bfs]
- HDU 1241 Oil Deposits (DFS/BFS)
- HDU 1241 Oil Deposits (DFS+BFS)
- HDU 1241/UVa572 Oil Deposits(DFS&BFS)
- 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-Oil Deposits (DFS,BFS,水题)
- 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>
- Oil Deposits BFS+DFS
- Java集合干货系列-集合总体大纲
- 第十五周实践项目阅读程序————5
- 关于Android studio中os文件导入失败的解决方式
- Expectation Maximization Algorithm---opencv2.4.11
- 有的放矢,Liferay进军数字体验市场
- HDU 1241 Oil Deposits (基础DFS 或者 BFS)
- 乐学成语——显示学习列表
- hadoop、spark学习中常用的linux命令
- 用javascript实现本地图片预览(HTML5)
- 离散数学----数理逻辑
- 排序算法(3)-选择排序
- 【java网络】编程基础
- Linux find exec 记录
- python selenium