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;
    }
  • 相关阅读:
    splay
    开车旅行(2012day1T3)
    LCT入门
    最小瓶颈路
    poj 3041 Asteroids
    sql waitfor 延时执行
    [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server 不存在或访问被拒绝
    SQL Server中行列转换
    sql中 with rollup 、with cube、grouping 统计函数用法
    sql 分组后 组内排名
  • 原文地址:https://www.cnblogs.com/crazyacking/p/3748890.html
Copyright © 2011-2022 走看看