Oil Deposits UVA - 572(深度优先搜索)
来源:互联网 发布:生物医学数据库使用 编辑:程序博客网 时间:2024/05/21 06:38
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits.
GeoSurvCompworkswithonelargerectangularregionoflandatatime,andcreatesagridthatdivides 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 containsone 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 <cstdio>#include <cstring>using namespace std;const int maxn = 100 + 10;int m, n, a[maxn][maxn];char ch[maxn][maxn];void dfs(int i, int j){ a[i][j] = 1; ch[i][j] = '*'; int xn, yn; for(int x = -1; x <= 1; x++) for(int y = -1; y <= 1; y++){ xn = i + x; yn = j + y; if(xn >= 0 && xn < m && yn >= 0 && yn < n && ch[xn][yn] == '@' && a[xn][yn] == 0) dfs(xn, yn); } return;}char readchar(){ char c; for(;;){ scanf("%c", &c); if(c == '@' || c == '*') return c; }}int main(void){ while(scanf("%d%d", &m, &n) == 2 && m){ for(int i = 0; i < m; i++) for(int j = 0; j < n; j++){ ch[i][j] = readchar(); } memset(a, 0, sizeof(a)); int num = 0; for(int i = 0; i < m; i++) for(int j = 0; j < n; j++){ if(ch[i][j] == '@' && a[i][j] == 0){ dfs(i, j); num ++; } } printf("%d\n", num); } return 0;}
- Oil Deposits UVA - 572(深度优先搜索)
- [ACM - 深度优先搜索]Oil Deposits
- POJ1562 - Oil Deposits - 深度优先搜索
- Oil Deposits(深度优先搜索DFS)
- HDU/HDOJ 1241 Oil Deposits (DFS)深度优先搜索
- Oil Deposits (poj 1241) 搜索 (深度优先搜素)
- UVa 572 - Oil Deposits 搜索专题
- Oil Deposits - UVa 572 简单搜索
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
- uva 572 - Oil Deposits
- uva 572 - Oil Deposits
- uva 572 - Oil Deposits
- uva-572-Oil Deposits
- UVa 572 - Oil Deposits
- UVa 572 - Oil Deposits
- UVa 572 - Oil Deposits
- uva 572Oil Deposits
- UVa 572Oil Deposits
- RabbitMQ之死信队列
- 1034. Head of a Gang
- C# 面板Panel
- 求函数f(n)=1/1!+1/2!+1/3!+...+1/n!的值
- 简述jpg、gif、png-8、png-24的区别,分别使用场景
- Oil Deposits UVA - 572(深度优先搜索)
- 初学Java知识点
- UDP服务器和客户端程序实例
- struts2.5通配符映射问题
- Android基本组件之单选按钮和复选框(自用)
- Android contentResolver得到本机联系人数据
- mybatis 批量更新(batchUpdate)
- 【C/C++开发】字符串操作
- 华为机试在线训练-牛客网(16)字符串排序