• 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;
    }
  • 相关阅读:
    【bzoj2962】序列操作 线段树
    【bzoj1922】[Sdoi2010]大陆争霸 堆优化Dijkstra
    .NET Core / C# 开发 IOT 嵌入式设备的个人见解
    C#中Equals和= =(等于号)的比较)
    VS 2017常用快捷键
    【你不一定知晓的】C#取消异步操作
    工程实践:给函数取一个"好"的名字
    接口测试入门篇
    博客园知名博主 Vamei 英年早逝!
    人生苦短,我用 Python
  • 原文地址:https://www.cnblogs.com/crazyacking/p/3748890.html
Copyright © 2020-2023  润新知