hdu 1241 Oil Deposits
2016-05-20 21:49
225 查看
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22931 Accepted Submission(s): 13213[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
#include <iostream> #include <cstring> using namespace std; int m,n,many; char s[105][105]; int visit[105][105]; const int dx[] = {0,0,1,1,1,-1,-1,-1}; const int dy[] = {1,-1,0,1,-1,0,1,-1}; void dfs(int i, int j){ visit[i][j] = 1; for(int k = 0; k < 8; k++) { int nx = i + dx[k]; int ny = j + dy[k]; if(nx >= 0 && nx < m && ny >= 0 && ny < n && visit[nx][ny] == 0 && s[nx][ny] == '@') dfs(nx,ny); } //return 1; } int main(){ while(cin >> m >> n) { many = 0; memset(s, 0, sizeof(s)); memset(visit,0,sizeof(visit)); if(m == 0) break; for(int i = 0; i < m; i++){ cin >> s[i]; } for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if(s[i][j] == '@' && visit[i][j] == 0) { many++; dfs(i, j); } } } cout << many << endl; } return 0; }
View Code
[align=left]Sample Output[/align]
0 1 2 2
相关文章推荐
- 设计模式 - Decorator模式
- LeetCode 337
- LeetCode 55
- 括号配对问题
- spark yarn分布式安装
- 牛客网的题,重建二叉树。(用树的先序,中序,还原二叉树)
- POJ 2253 Frogger(翻译/最短路/floyd/思维)
- Java并发教程(Oracle官方资料)
- OOM原因和解决方案
- dex文件入门(二)
- jsp 四大对象的作用域
- Java内存与垃圾收集知识总结
- ubuntu系统没有声音解决方法
- SQL SERVER 对权限的授予GRANT、拒绝DENY、收回REVOKE
- 【算法总结系列-1】快速排序-c语言实现
- THE TOWER OF HANOI
- 在一个数组中找差值最大数对,输出最大差值
- Swift-构造过程(Initialization)(十二)
- nyoj 715相邻的计数(dp)
- char *a和 char a[]