DFS or BFS --- 连通块
2014-05-23 21:33
232 查看
Oil Deposits
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64
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
【题目来源】
Mid-Central USA 1997
【题目大意】
在一个郊区的空地里,分散着很多油田,要你求油田的数量。
【题目分析】
就是一个简单的图搜索,不断标记,不断统计。
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64
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
【题目来源】
Mid-Central USA 1997
【题目大意】
在一个郊区的空地里,分散着很多油田,要你求油田的数量。
【题目分析】
就是一个简单的图搜索,不断标记,不断统计。
#include<iostream> #include<cstdio> #define MAX 150 using namespace std; char Map[MAX][MAX]; int n,m; int cnt; int dir[8][2]={-1,0, -1,1, 0,1, 1,1, 1,0, 1,-1, 0,-1, -1,-1}; void dfs(int x,int y) { Map[x][y]='*'; int xx; int yy; for(int i=0;i<8;i++) { xx=x+dir[i][0]; yy=y+dir[i][1]; if(Map[xx][yy]=='@') dfs(xx,yy); } } int main() { while(cin>>n>>m,m) { getchar(); int i,j; cnt=0; for(i=1;i<=n;i++) { scanf("%s",Map[i]+1); } for(i=0;i<=n+1;i++) Map[i][0]=Map[i][m+1]='*'; for(i=0;i<=m+1;i++) Map[0][i]=Map[n+1][i]='*'; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(Map[i][j]=='@') {dfs(i,j); cnt++;} } } cout<<cnt<<endl; } return 0; }
相关文章推荐
- UVA 439 Knight Moves --DFS or BFS
- POJ 2386 Lake Counting (DFS or BFS)
- POJ 3984 迷宫问题(BFS or DFS)
- 第四届 山东省ACM B Thrall’s Dream(连通图 OR BFS)
- ZZULI-1905 小火山的围棋梦想 【BFS or DFS】
- 图的基本操作:建立图,DFS,BFS-06-图1 列出连通集
- hdu 1241 bfs or dfs
- 通过DFS和BFS判断无向图是否连通
- HDU 5423 Rikka with Tree(bfs or dfs)——BestCoder Round #53(div.1 div.2)
- hdu 1074 dfs or bfs
- HDU 1242 Rescue(DFS or BFS)
- vijos 1051 BFS/DFS 连通块
- EOJ 1816 判断图连通的三种方法——dfs,bfs,并查集
- 数据结构—连通图的遍历—DFS和BFS
- 连通图的(领接矩阵储存)创建和遍历(包含BFS,DFS两种算法)
- 【BZOJ2208】[Jsoi2010]连通数【BFS/DFS】【SCC】
- hdu 1312 Red and Black DFS搜索 or BFS搜索
- 当然选择原谅她呀 【BFS】or 【DFS】
- hdu 4394 - Digital Square (dfs or bfs)
- 【2016杭电女生赛1005】【暴力 bfs or dfs or 模拟】girlcat 数女生数和猫咪数