Oil Deposits HDU杭电1241
2015-08-03 19:52
225 查看
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
Sample Output
//挺容易理解的,解释在代码里
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
//挺容易理解的,解释在代码里
#include<stdio.h> int n,m; char a[105][105]; void DFS(int x,int y) { if(x>m || x<1 || y>=n || y<0)//越界 return ; if(a[x][y]!='@')//不是油 return ; a[x][y]='+';//这样就少执行了些,节省了点时间 DFS(x,y+1); //上下左右对角线 DFS(x,y-1); DFS(x-1,y); DFS(x+1,y); DFS(x-1,y-1); DFS(x+1,y+1); DFS(x-1,y+1); DFS(x+1,y-1); } int main() { int i,j; int x,y; while(scanf("%d%d",&m,&n),m+n) { getchar(); for(i=1;i<=m;++i) { scanf("%s",a[i]);//用这个更方便 } int cnt=0; for(i=1;i<=m;++i) { for(j=0;j<n;++j) { if(a[i][j]=='@') { cnt++; DFS(i,j); } } } printf("%d\n",cnt); } return 0; }
相关文章推荐
- ehcache缓存
- hdoj 1686 Oulipo【求一个字符串在另一个字符串中出现次数】
- html标签的语义化
- Learning Python Day3
- Uva 11324 The Largest Clique【强连通 DAG动规 spfa】
- Watch OS 2 Complication开发教程(一)基础(持续连载中)
- String类与StringBuffer类的研究
- 计算线段的三等分点
- 最全的常用正则表达式大全分享(转)
- 2032 杨辉三角【循环控制】
- 8月3日----UDP通信
- HDU - 1864 最大报销额
- C语言笔记
- IOS使用纯C语言创建静态库
- 【HDOJ 1532】 Drainage Ditches(最大流模板)
- Scanner扫描器工具类
- [leedcode 189] Rotate Array
- 黑马程序员--java基础--反射
- 黑马程序员--java基础--IO流概述
- UVA10110完全平方数的约数为奇数个