Oil Deposits (dfs)深度搜索
2017-07-24 18:41
232 查看
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 32295 Accepted Submission(s): 18740Problem DescriptionThe 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 eachplot 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 numerouspockets. Your job is to determine how many different oil deposits are contained in a grid. InputThe 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. Followingthis 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. OutputFor 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 Input1 1*3 5*@*@***@***@*@*1 8@@****@*5 5****@*@@*@*@**@@@@*@@@**@0 0 Sample Output0122 SourceMid-Central USA 1997题意:英语不好, 能懂题意就行。说@是一个有油的地方,@与@相邻就表示同一个油口袋,斜的也算;比如@**@ 这两个@也是同一个油口袋;这题是一道典型的dfs的题,每次从有@的地方开始dfs向八个方向深搜,直到找不到@为止,将访问过的位置做标记,下次不再访问;看看能进行几个dfs就会有几个油口袋;#include<iostream> #include<algorithm> #include<string.h> using namespace std; int ivs[110][110];//访问的标志,0为未访问,1为访问过了 char a[110][110]; //方向数组 int b[]={1,-1,0,0,1,-1,1,-1}; int q[]={0,0,1,-1,-1,1,1,-1}; int r,c; int dfs(int x,int y) { int i,j; ivs[x][y]=1; //访问了 //向x,y的8个方向深度搜索 for(i=0;i<8;i++) { int ax=x+b[i]; int ay=y+q[i]; //查看ax,ay是否越界 以及是否被访问了 if(ax>=1&&ax<=r&&ay>=1&&ay<=c&&a[ax][ay]=='@'&& 4000 ;!ivs[ax][ay]) { dfs(ax,ay); } } return 0; } int main() { while(cin>>r>>c,r&&c) { int i,j; //初始化ivs memset(ivs,0,sizeof(ivs)); for(i=1;i<=r;i++) for(j=1;j<=c;j++) cin>>a[i][j]; //统计次数 int sum=0; for(i=1;i<=r;i++) { for(j=1;j<=c;j++) { if(a[i][j]=='@'&&!ivs[i][j]) { dfs(i,j); ivs[i][j]=1; sum++; } } } cout<<sum<<endl; } return 0; }人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想。
相关文章推荐
- HDU/HDOJ 1241 Oil Deposits (DFS)深度优先搜索
- Oil Deposits(深度优先搜索DFS)
- hdu 1241 Oil Deposits (深度搜索)
- DFS 搜索 Problem 1011 Oil Deposits 探索石油储量
- 搜索dfs-hdu Oil Deposits
- Num 35 : HDOJ : 1241 Oil Deposits [ 深度优先搜索算法 ] [ 非回溯 ][ 图论 ]
- Oil Deposits (poj 1241) 搜索 (深度优先搜素)
- 杭电1241 HDU acm --Oil Deposits---DFS深度优先算法实现详解
- POJ1562 - Oil Deposits - 深度优先搜索
- C语言对深度搜索的简单应用(Oil Deposits
- Oil Deposits poj DFS
- 深度优先搜索(DFS)
- DFS(深度搜索)——基于回溯法和递归
- 深度搜索DFS-Lake Counting(POJ NO.2386)
- HDU 1241/UVa572 Oil Deposits(DFS&BFS)
- 深度搜索dfs感悟
- poj1562--Oil Deposits(DFS)
- uva 572 Oil Deposits(DFS遍历图)
- HDU-#1241 Oil Deposits(BFS & DFS)
- poj 1562 Oil Deposits (dfs)