HDU 1241 Oil Deposits(八方向联通块)
2015-08-03 16:22
211 查看
Oil Deposits
[align=left]Problem Description[/align]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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
#include<cstdio> #include<cstring> using namespace std; int d[8][2]={1,0,0,1,-1,0,0,-1,-1,-1,-1,1,1,1,1,-1}; char g[105][105]; int id,m,n; void dfs(int x,int y) { if(x<0||y<0||x>=m||y>=n||g[x][y]=='*') return; g[x][y]='*'; for(int i=0;i<8;i++) { int xx=x+d[i][0]; int yy=y+d[i][1]; dfs(xx,yy); } } int main() { int i,j; while(scanf("%d%d",&m,&n),m|n) { id=0; for(i = 0;i < m;i++) scanf("%s",g[i]); for(i = 0;i < m;i++) for(j = 0;j < n;j++) { if(g[i][j]!='*') { id++; dfs(i,j); } } printf("%d\n",id); } return 0; }
相关文章推荐
- Unity3d中制作Loading场景进度条所遇到的问题
- poj 4052 AC自动机
- Hibernate知识点总结
- hdu 4027 Can you answer these queries? (区间线段树,区间数开方与求和,经典题目)
- js模板
- android上传图片到PHP的过程详解
- iOS手势(拖动,缩放,旋转,点击,手势依赖,自定义手势)
- php将远程图片保存到本地服务器的实现代码
- xcode基础配置
- springMail发送Text简单邮件的方法
- 杭电OJ-1001-(Sum Problem大整数求和问题)
- Java byte数组 转short int
- SVN 权限配置详细说明
- Activity生命周期(二)——多个Activity交互
- php header解决乱码问题及其他乱码问题
- oracle 日期获取
- 更新yum源/apt-get源
- 避免野指针的产生
- poj 2299
- Linux命令之ls