hdu 1241 Oil Deposits

来源:互联网 发布:大数据方面的论文 编辑:程序博客网 时间:2024/06/05 06:40

                                                     Oil Deposits

                                                                 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                 Total Submission(s): 30286    Accepted Submission(s): 17532


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
0122
 
简要题意及分析:
*代表空方格,@代表有石油,其中在与@相邻的八个方格中,如果也有@存在,则把它们视作同一个石油存储区,如此递推下去。在给定m*n的方格中,求所有的石油存储区的个数。
dfs求连通分量的个数即可,注意搜索八个方向,以及边界判断,此题较水。
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>using namespace std;int m,n;const int MAXN = 100+5;char map[MAXN][MAXN];bool vis[MAXN][MAXN];int ans=0;bool check(int x,int y){    return x>=0&&y>=0&&x<m&&y<n;}void dfs(int i,int j){    if( !check(i,j) || map[i][j]=='*' || vis[i][j] )        return;    vis[i][j]=true;    dfs(i+1,j+1);    dfs(i-1,j-1);    dfs(i+1,j-1);    dfs(i-1,j+1);    dfs(i,j+1);    dfs(i,j-1);    dfs(i+1,j);    dfs(i-1,j);}int main(){    while(~scanf("%d %d",&m,&n))    {        ans=0;        if(m==0)            break;        memset(vis,false,sizeof(vis));        memset(map,0,sizeof(map));        int i,j;        for(i=0;i<m;++i)            scanf("%s",map[i]);        for(i=0;i<m;++i)            for(j=0;j<n;++j)        {            if(map[i][j]=='@' && !vis[i][j])  // 核心代码            {                dfs(i,j);                ans++;            }        }        printf("%d\n",ans);    }    return 0;}

0 0
原创粉丝点击