dfs 著名的油田分块的问题
2014-04-15 20:50
274 查看
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
0
1
2
2
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
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int m,n; struct node { int x,y,ans; }; char mapp[101][101]; int visit[101][101]; int dir[8][2] = {{0,1},{1,1},{1,0},{-1,0},{0,-1},{-1,-1},{-1,1},{1,-1}}; void dfs(int x,int y) { int i; for(i = 0; i<8; i++) { int tx = x+dir[i][0]; int ty = y+dir[i][1]; if(tx<m && tx>=0 && ty>=0 && ty<n && !visit[tx][ty] && mapp[x][y]=='@' )//顺序是个严重的问题。。 { visit[tx][ty] = 1; dfs(tx,ty); } } } int main() { int i,j; int countt = 0; while(scanf("%d %d",&m,&n)!=EOF) { if(m==0&&n==0)break; countt = 0; memset(visit,0,sizeof(visit)); for(i = 0; i<m; i++) { scanf("%s",&mapp[i]); } for(i = 0; i<m; i++) { for(j = 0; j<n; j++) { if(mapp[i][j]=='@' && !visit[i][j]) { visit[i][j] = 1; dfs(i,j); countt++; } } } printf("%d\n",countt); } return 0; } //终于明白了。。请允许我做一个感动的表情==、
相关文章推荐
- hadoop hdfs java api操作实战
- SQL SERVER 2008中输入汉字乱码的问题
- week 8
- 微软面试100题之19题:定义Fibonacci 数列如下,用最快的方法求该数列的第n 项
- [转]python开发_shelve_完整版
- windows8.1专业版 关闭ie11总是已停止工作
- MySQL 插入数据时,中文乱码问题的解决
- 折半插入排序
- 如何下载和安装Struts2框架
- linux shell(二)shell 中的变量
- WEEK8
- HDU 1027 Ignatius and the Princess II
- lua -- 在弹框中显示物品列表
- HTTP 2014.4.15
- 微软100题(第十七题)
- PHP RAWURLENCODE()和URLENCODE()函数区别
- 给两组年月日,怎么计算相差的天数(考虑闰年)
- Set 与 Multiset
- js正则表达式
- GoroSort