• L


    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
    0
    1
    2
    2

    思路:flood fill算法,很简单,很重要,就是算连通块,找到一个油田就把他附近8个方向和自己全部标记一遍,对周围找的的油田也对其8个方向标记一遍(注意开vis数组,不然可能会T)。

    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    char maze[105][105];
    int vis[105][105];
    int dirx[] = {1,-1,0,0,1,-1,1,-1};
    int diry[] = {0,0,-1,1,-1,1,1,-1};
    int n,m;
    int ans = 0;
    int check(int x,int y)
    {
        if(x < 0 || y < 0 || x >= n || y >= m)
            return 1;
        if(maze[x][y] == '*')
            return 1;
        return 0;
    }
    void dfs(int x,int y)
    {
        if(check(x,y))//思考这个东西的顺序,我觉得不一定就要在这里,代码顺序并不是重点,但是代码本身的逻辑是不能变动的。
            return;
    
        maze[x][y] = '*';
        for(int i = 0;i < 8;i++)
        {
            int tx = x + dirx[i];
            int ty = y + diry[i];
            dfs(tx,ty);
        }
    }
    
    int main()
    {
        while(~scanf("%d%d",&n,&m)&&n&&m)
        {
            ans = 0;
            for(int i = 0;i < n;i++)
            {
                scanf("%s",maze[i]);
            }
            for(int i = 0;i < n;i++)
            {
                for(int j = 0;j < m;j++)
                {
                    if(maze[i][j] == '@')
                    {
                        ans++;
                        dfs(i,j);
                    }
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    
    
  • 相关阅读:
    LeetCode Algorithm
    实现QObject与JavaScript通讯(基于QWebEngine + QWebChannel)
    Linux工具参考篇(网摘)
    GO 语言简介(网摘)
    Qt窗体引用window自带阴影边框效果
    无插件VIM编程技巧(网摘)
    ASP.NET 5基础之中间件
    ASP.NET Core 1.0基础之应用启动
    理解ASP.NET 5 Web Apps
    DNX 概览
  • 原文地址:https://www.cnblogs.com/tomjobs/p/10612576.html
Copyright © 2020-2023  润新知