ACM-DFS之Oil Deposits——hdu1241
2014-02-25 12:47
197 查看
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
[align=left]Sample Output[/align]
0
1
2
2
找石油,一道经典的dfs,也是基础的一道dfs,
需要注意的就是要向八个方向搜索,搜索完一处,将该处变成石头。
#include <iostream> using namespace std; char oil[101][101]; int m,n; void dfs(int a,int b) { // 判断越界与是否为石油 if(oil[a][b]!='@' || a<0 || b<0 || a>n-1 || b>m-1) return; //该处已经被采集 oil[a][b]='*'; //向八个方向搜索 dfs(a-1,b-1); dfs(a-1,b); dfs(a-1,b+1); dfs(a,b-1); dfs(a,b+1); dfs(a+1,b-1); dfs(a+1,b); dfs(a+1,b+1); return; } int main() { int i,j,sl; while(cin>>n>>m) { if(m==0 && n==0) break; // 输入 for(i=0;i<n;++i) for(j=0;j<m;++j) cin>>oil[i][j]; // 遍历 sl=0; for(i=0;i<n;++i) for(j=0;j<m;++j) { if(oil[i][j]=='@') { dfs(i,j); ++sl; } } // 输出 cout<<sl<<endl; } return 0; }
相关文章推荐
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- win2003分布式文件系统(dfs)配置方法[图文详解]
- win2003分布式文件系统及其部署 图文教程
- 简单的四则运算
- 数的奇偶性
- DFS使用方法总结
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- FastDFS注意事项
- 无忧技术带您预览DFS(分布式文件系统)管理控制台
- C 语言实现迷宫 DFS算法
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- 一幅图弄清DFT与DTFT,DFS的关系
- HDFS---Namenode
- HDFS ---- Services startup
- zoj3549 快速幂
- 浅谈manacher算法 最长回文子串(Longest Palindromic Substring)