Oil Deposits(基础dfs)
来源:互联网 发布:cad数控冲床编程 编辑:程序博客网 时间:2024/06/05 00:43
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21383 Accepted Submission(s): 12319
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
0122
题意:就是找出有多少块有石油的区域,就是数组中的@,这边相邻指的是是周围的八个位置。
分析:这一题可以说是搜索中最基础的一题之一。挨个点搜索标记就行了。
#include <stdio.h>char a[111][111];int dir[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};int m, n;int flag;int dfs(int x, int y){ if(a[x][y]=='*') return flag; flag=1; a[x][y]='*';//记得标记,这样不会重复计数 int tx,ty; for(int i=0;i<8;i++) { tx=x+dir[i][0]; ty=y+dir[i][1]; if(tx>=0&&tx<m&&ty>=0&&ty<n) dfs(tx,ty); } return flag;}int main(){ while(~scanf("%d %d",&m,&n)&&m+n) { int num=0; for(int i=0;i<m;i++) scanf("%s",a[i]); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) {//把每个点都进去搜索一遍 flag=0; if(dfs(i,j)) { num++; } } } printf("%d\n",num); }}
0 0
- Oil Deposits(基础dfs)
- Oil Deposits(DFS,基础题)
- Oil Deposits hdu1241 dfs
- Oil Deposits poj DFS
- POJ1562:Oil Deposits(DFS)
- HDU1241:Oil Deposits(DFS)
- hdu1241(DFS Oil Deposits )
- UVa572 Oil Deposits(DFS)
- Oil Deposits DFS水题
- Oil Deposits BFS+DFS
- Oil Deposits uva DFS
- HDU1241:Oil Deposits(DFS)
- 【搜索-DFS】Oil Deposits
- Oil Deposits(DFS)
- poj1562 Oil Deposits(DFS)
- HDU1241 Oil Deposits DFS
- HDU1241 Oil Deposits DFS
- Oil Deposits(dfs深搜)
- Android开发之Animation介绍(上)
- Linux c 操作Mysql简单实例
- 1016. 部分A+B (15)
- 【Spark工作机制详解】调度与任务分配
- android 中checkBox的onclik方法实现
- Oil Deposits(基础dfs)
- Qt 下使用OpenCV
- 排序和搜索
- 怎么才能知道自己电脑最大支持的内存是多少?
- java lang 包学习 http://blog.csdn.net/wong_judy/article/details/5318560
- C语言实现双向非循环链表(不带头结点)的节点插入
- javascript--事件绑定&模型
- CSU 1603: Scheduling the final examination
- ARC简介