[dfs]HDU 1241 Oil Deposits
2015-07-27 15:31
344 查看
L - Oil Deposits
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
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.
简单的dfs深搜,只不过是把四联通的边界改成了八个方向都能联通,对每一个访问过的块只增加一次就可以搞定。
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
简单的dfs深搜,只不过是把四联通的边界改成了八个方向都能联通,对每一个访问过的块只增加一次就可以搞定。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue> using namespace std; int vis[105][105]; int m,n; char mat[105][105]; int dx[] = {-1,1,-1, 1,0, 0,-1,1}; int dy[] = {-1,1, 1,-1,1,-1, 0,0}; void dfs(int x,int y) { // printf("%d %d\n",x,y); vis[x][y] = 1; for (int i = 0; i < 8;i++) { int nx = x + dx[i]; int ny = y + dy[i]; if( nx>=0 && nx < m && ny >=0 && ny < n && mat[nx][ny]=='@' && !vis[nx][ny]) dfs(nx,ny); } return; } int main() { // freopen("in.txt","r",stdin); while(scanf("%d%d",&m,&n) != EOF) { if (m==0 && n==0) break; memset(vis,0,sizeof(vis)); for(int i = 0;i<m;i++) for(int j = 0 ; j<n;j++) cin>>mat[i][j]; int ans = 0; for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if (mat[i][j] =='@' && !vis[i][j] ) { dfs(i,j); ans++; } } } printf("%d\n",ans); } return 0; }
相关文章推荐
- ST算法
- 【DATAGUARD】物理dg的failover切换(六)
- sketchup技巧分享-如何利用组件创建无缝图案峥
- hdu 1022Tran Problem
- hdu 5312 Sequence(三角形数的利用)
- set env和export这三个命令的区别
- java IO流解析(三)
- 高德地图API使用效果图
- Ajax实现页面自动刷新与进度条制作
- 从文件结构认识HLS
- 关于tomcat端口的修改,以及同时部署多个应用,自己遇到的问题以及解决办法
- LVS Nginx HAProxy 优缺点
- poj 3734 矩阵快速幂模板清晰版
- Partition List
- 表单标签form的简单使用
- Oracle SqlPlus 方向键的方法和解决的退格键失效
- uva 10726 Coco Monkey(数论)
- 【DATAGUARD】物理dg的switchover切换(五)
- sketchup技巧分享-如何利用组件创建无缝图案艺
- 三个引号的区别和用法