HDUOJ---1241Oil Deposits(dfs)
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10644 Accepted Submission(s): 6176
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
0 1 2 2
Source
代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 char map[101][101];
5 int dir[8][2]={
6 {1,0},
7 {-1,0},
8 {0,1},
9 {0,-1},
10 {1,1},
11 {-1,-1},
12 {1,-1},
13 {-1,1}
14 };
15 int n,m;
16 void dfs(int st,int en)
17 {
18 for(int i=0;i<8;i++)
19 {
20 if(st+dir[i][0]>=n&&st+dir[i][0]<0) continue;
21 if(en+dir[i][0]>=m&&en+dir[i][0]<0) continue;
22 if(map[st+dir[i][0]][en+dir[i][1]]=='@')
23 {
24 map[st+dir[i][0]][en+dir[i][1]]='*';
25 dfs(st+dir[i][0],en+dir[i][1]);
26 }
27 }
28 }
29 int main()
30 {
31 int i,j,cnt;
32 while(scanf("%d%d",&n,&m),n+m)
33 {
34 cnt=0;
35 for(i=0;i<n;i++)
36 scanf("%s",map[i]);
37 for(i=0;i<n;i++)
38 {
39 for(j=0;j<m;j++)
40 {
41 if(map[i][j]=='@')
42 {
43 dfs(i,j);
44 cnt++;
45 }
46 }
47 }
48 printf("%dn",cnt);
49 }
50 return 0;
51 }
- Android 简单的代码混淆
- Android Service组件(1)
- Android学习之listview的下拉刷新、上拉载入
- Android Service 系统服务
- Android学习之sqlite与listview
- Android应用开发SharedPreferences存储数据的使用方法
- android 补间动画
- Android Geocoder(位置解析)
- Android服务之AIDL
- SQL语言学习-数据定义语言
- js获取url参数的方法
- JavaScript操作符(关系操作符、相等操作符和条件操作符)
- javascript运动功能-分享到
- SQL Server 2008 geometry 数据类型
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法