zoukankan      html  css  js  c++  java
  • DFS or BFS --- 连通块

    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

    【题目大意】

    在一个郊区的空地里,分散着很多油田,要你求油田的数量。

    【题目分析】

    就是一个简单的图搜索,不断标记,不断统计。

    #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;
    }
  • 相关阅读:
    看完一本,加油
    一个简单的动作,让你的手机号码变成空号
    Goldwave心得
    UML设计初步 基本概念一(actor, use case)
    ASP.NET控件开发 概念和HelloWorld控件
    控件的呈现
    ASP.NET控件生命周期
    ASP老项目中如何搜索一个文件在哪些地方被引用
    PL/SQL语法 游标
    2009编程语言排名
  • 原文地址:https://www.cnblogs.com/crazyacking/p/3748890.html
Copyright © 2011-2022 走看看