HDU1241 Oil Deposits(dfs)

来源:互联网 发布:二叉树遍历递归算法 编辑:程序博客网 时间:2024/05/19 05:32

Oil Deposits

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

翻译:
GeoSurvComp地质调查公司负责探测地下石油储量。GeoSurvComp公司在一段时间内在一大块矩形区域内工作,创造出一个网格把土地分成很多方块。如果每一方块单独分析,使用感应设备去测定方块中是否含有石油。含有油的方块称为口袋。如果两个口袋是相邻的,那它们属于同一个油床。油床可以相当大,可以包含众多口袋。你的任务就是测定出有多少不同的油床。

输入:

输入文件包括一个或多个网格。每一个网格输入的第一行含有m和n,分别代表网格的横坐标和纵坐标,被一个空格分开。如果m=0则标志着输入结束;否则1<=m<=100并且1<=n<=100。接下来输入m行n列个网格特征(不记录换行符)。每一个特征值跟一个方块相对应,并且要么是*(代表没油),要么是@代表一个石油口袋。

输出:

对于每一个方块,输出不同的油田数。两个不同的口袋属于同一个油田,如果它们垂直水平或对角相邻。一个油田包含少于100个口袋。

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>#include<cstdlib>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;const int maxn = 101;bool vis[maxn][maxn]; //标记是否走过int m, n;char oil[maxn][maxn];int dir[8][2] = {1,0, 1,1, 1,-1, 0,1, 0,-1, -1,1, -1,0, -1,-1}; //记录该点周围的方向void dfs(int x, int y){    vis[x][y] = 0;    int i;    int k, g;    for(i=0; i<8; i++){        k = x + dir[i][0];        g = y + dir[i][1];        if(oil[k][g]=='@' && vis[k][g] && k>=0 && k< m && g>=0 && g<n)            dfs(k, g);    }}int main(){    int i, j, sum;    while(scanf("%d%d", &m, &n),  m+n)    {        sum = 0;        memset(vis, 1, sizeof(vis));        for(i=0; i<m; i++){            for(j=0; j<n; j++){                cin >> oil[i][j];            }        }        for(i=0; i<m; i++){            for(j=0; j<n; j++){                if(oil[i][j]=='@' && vis[i][j]){                    sum++;                    dfs(i, j);                }            }        }        cout << sum << endl;    }    return 0;}
原创粉丝点击