油田问题 UVA 572
2017-11-19 11:32
453 查看
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 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 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. 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.
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
01
2
2
//#include<bits/stdc++.h> #include<iostream> #include<string> #include<algorithm> #include<cstring> #include<cstdio> typedef long long ll; using namespace std; int m,n; char a[100][100]; int jud[100][100]; void dfs(int x,int y) { if(jud[x][y]==1||a[x][y]!='@') return; if(x<0||x>=m||y>=n||y<0) return; ++jud[x][y]; for(int i=-1;i<=1;++i) { for(int j=-1;j<=1;++j) { if(i!=0||j!=0) dfs(x+i,y+j); } } } int main() { while(cin>>m>>n) { if(m==0&&n==0) break; int sum=0; getchar(); memset(jud,0,sizeof(jud)); for(int i=0;i<m;++i) { for(int j=0;j<n;++j) { cin>>a[i][j]; if(j==n-1) getchar(); } } for(int i=0;i<m;++i) for(int j=0;j<n;++j) if(jud[i][j]==0&&a[i][j]=='@') { ++sum; dfs(i,j); } cout<<sum<<endl; } return 0; }
相关文章推荐
- 油田问题 UVA 572
- UVA572(油田)简单的深搜问题
- UVA572油田问题(DFS求连通块)
- uva572 油田 连通块遍历
- UVA 572 POJ 2386 连通块问题
- UVa-572油田
- uva 572 Oil Deposits(油田)—— DFS
- Uva 572 Oil Deposits 油田 (DFS求连通块)
- 油田 (Oil Deposits,Uva 572)
- 6_12 油田(UVa572)<图的连通块DFS>
- 油田 uva 572
- UVa 572 - Oil Deposits(油田 dfs) 错的离谱的我
- 油田(Oil Deposits,UVa 572)
- UVa572油田(连通块个数)
- 油田UVA 572
- uva572(DFS 油田)
- UVa 572 油田
- [UVa]572 油田 Oil Deposits
- 油田(Oil Deposits, UVa 572)
- 油田(Oil Deposits, UVa 572)