搜索Oil Deposits
2015-12-10 20:40
225 查看
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19962 Accepted Submission(s): 11465
[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 [code]#include <iostream> using namespace std; char a[101][101]; //油田 int m,n; void dfs(int x,int y) { a[x][y]='*'; //将现在的位置换为* for(int dx=-1;dx<=1;dx++) for(int dy=-1;dy<=1;dy++) //向x方向移动dx,向y方向移动dy 移动结果为(mx,ny); { int mx=x+dx,ny=y+dy; if(0<=mx&&mx<m&&0<=ny&&ny<n&&a[mx][ny]=='@')//判断是不是在油田内,以及是否有油 dfs(mx,ny); } return ; } void solve () { int r=0; for(int i=0;i<m;i++) for(int j=0;j<n;j++) //从有油的地方开始 { if(a[i][j]=='@') { dfs(i,j); r++; } } cout<<r<<endl; } int main () { while(cin>>m>>n) { if(m==0&&n==0) break; for(int i=0;i<m;i++) for(int j=0;j<n;j++) cin>>a[i][j]; solve(); } }
[/code]
相关文章推荐
- STL训练(12.10.15~12.13.15)
- C++ using关键字作用总结
- 求40+的阶乘
- #学习笔记#(11)四分圆环-photoshop
- 【2011图灵奖得主】我眼中的Judea Pearl
- reflect(反射)了解一点点
- OC 类对象和类加载
- ehcache
- UVA 10763 - Foreign Exchange【排序】
- python 中的counter类用法。重要
- python 中的counter类用法。重要
- Ubuntu 下matlab与 opencv混合编程
- spring 判断非空提示断言
- 给女票纪念日网站2015年12月10日
- BZOJ 1965 快速幂 + 拓展欧几里得
- Linux搜索文件命令
- 查找一串字符中有没有我想查找的字符
- java解惑--谜题2:找零时刻
- Codeforces Round #335 606A - Magic Spheres
- AutoCAD 2014 win 32bit破解版