hdoj1241Oil Deposits
2016-07-28 20:46
435 查看
[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
[align=left]Sample Output[/align]
0
1
2
2
这个题跟poj的2386题几乎就是一摸一样的
代码如下:
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
[align=left]Sample Output[/align]
0
1
2
2
这个题跟poj的2386题几乎就是一摸一样的
代码如下:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m,ans,nx,ny; char map[110][110]; int used[110][110]; void dfs(int x,int y) { map[x][y]='*';//这里直接将其现在所在位置替换为'.'。以免下次重复搜索 for(int dx=-1;dx<=1;dx++) for(int dy=-1;dy<=1;dy++){ int nx=x+dx,ny=y+dy; if(nx>=0&&nx<n&&ny>=0&&ny<m&&map[nx][ny]=='@') { dfs(nx,ny); } } return ; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; int i,j; memset(used,0,sizeof(used)); ans=0; for(i=0;i<n;i++) scanf("%s",map[i]); for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(map[i][j]=='@') { dfs(i,j); ans++; } } } printf("%d\n",ans); } }
相关文章推荐
- 二维码
- 关于listview,gridview里面数据显示不全的问题
- Spark开发指南
- 需求分析方法及包装类
- Codeforces 115B Lawnmower
- 函数指针
- Bash Shell 基本特性
- xlistview 上拉刷新 下拉加载
- Java内存区域
- 园圆形图片 ImageLoder 实现
- servlet中request等中文乱码问题
- HDU 5311 Hidden String (DFS)
- 集合框架
- 字符数组,字符指针,sizeof,strlen总结
- POJ-2624 4th Point(计算几何)
- android开发工具类(草鸡好用)
- ThinkPHP基础
- python 函数的嵌套定义 and 函数的返回值是函数
- Zephyr OS 基础篇:系统简介
- java中递归算法和汉诺塔