• ACM查找油田块


    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 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

    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
    解题思路:
    这个题目的大意是我们有许多的小分块,但是当这些小分块的水平连通,垂直连通或者是对角线连通时,这就是一个大的块。现在我们要求的就是这块油田有多少个分块。这是一个深度搜索的问题。当我们找到一个小块的时候,我们就将这个点的八个方向进行一次查找,当走过一个@后就把它标记为*,此时我们要用到递归的方法,再查找我们找到的@
    程序代码:
    #include <iostream>
    using namespace std;
    #define N 111
    char str[N][N];
    int a,b;
    int next[8][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};//八个方向
    void fun(int x,int y)
    {
        int m,n,k;
        for(k=0;k<8;k++)
        {
            n=x+next[k][0];
            m=y+next[k][1];
            if(n<0||n>a-1||m<0||m>b-1||str[n][m]=='*')
                continue;
            str[n][m]='*';//标记
            fun(n,m);//递归
        }
    }
    int main()
    {
        int flag;
        while(cin>>a>>b&&(a||b))
        {
            flag=0;
            for(int i=0;i<a;i++)
                for(int j=0;j<b;j++)
                    cin>>str[i][j];
            for(int x=0;x<a;x++)
            {
                for(int y=0;y<b;y++)
                {
                    if(str[x][y]=='@')
                    {
                        str[x][y]='*';//标记
                        fun(x,y);
                        flag++;//累加连通的块数
                    }
                }
            }
            cout<<flag<<endl;
        }
        return 0;
    }
    
    
    
    
    
  • 相关阅读:
    堆和栈的区别
    .net中类(class)与结构(struct)的不同
    CTS、CLS、CLR
    C#和.Net的关系
    装箱(boxing)和拆箱(unboxing)
    三层架构
    属性和public字段的区别(调用set方法为一个属性设值,然后用get方法读取出来的值一定是set进去的值吗?)
    override与重载(overload)的区别
    C#中的委托是什么?事件是不是一种委托?事件和委托的关系。
    json转树状菜单栏
  • 原文地址:https://www.cnblogs.com/xinxiangqing/p/4688441.html
Copyright © 2020-2023  润新知