文章标题
来源:互联网 发布:开户数据 编辑:程序博客网 时间:2024/06/05 18:25
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
题意:判断在整个矩阵中有多少个‘@’的连通域,可以向八个方向遍历,‘*’表示不能通过。
题解:直接使用深搜,最简单的深搜。
#include <stdio.h> #include <string.h> char map[105][105],mat[105][105]; int vis[105][105]; void dfs(int i,int j) { if(vis[i][j] || mat[i][j] == '*') return ; vis[i][j] = 1; dfs(i-1,j-1); dfs(i-1,j); dfs(i-1,j+1); dfs(i,j-1); dfs(i,j+1); dfs(i+1,j-1); dfs(i+1,j); dfs(i+1,j+1); } int main() { int n,m; while(~scanf("%d%d%*c",&n,&m)) { memset(map,0,sizeof(map)); memset(mat,'*',sizeof(mat)); memset(vis,0,sizeof(vis)); int i,j,cnt = 0; if(!m&&!n) break; for(i = 0;i<n;i++) { for(j = 0;j<m;j++) { scanf("%c",&map[i][j]); mat[i+1][j+1] = map[i][j]; } getchar(); } for(i = 1 ;i<=n;i++) { for(j = 1;j<=m;j++) { if(!vis[i][j] && mat[i][j] == '@') { dfs(i,j); cnt++; } } } printf("%d\n",cnt); } return 0; }
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- ACM天梯赛练习L1-001. Hello World
- C/C++文章目录
- 第十二周项目1(2)
- 8627 数独
- No positional parameters in query: queryUser
- 文章标题
- 四. 控件-UIPickerView
- Mysql在大型网站的应用架构演变
- C++中的String类
- iOS 第三方框架-Masonry介绍与使用实践
- Genymotion在ubuntu下登录,出现unknown generic error错误的解决方法
- JVM可支持的最大线程数
- Cocoa中层(layer)坐标系的极简理解
- 步步为营<一> C语言简介 和 编译环境搭建