zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
  • 相关阅读:
    YC创始人格雷厄姆为何不喜欢大学生创业?
    在创业的道路上,让公司生存下去是做重要的!
    农村90后李传帅的创业故事
    创业公司的出头之日在哪里?
    创业者不能盲目的跟风,不然结局很凄凉
    小程序到底适不适合创业者
    共享经济的涨潮与退潮就在一瞬间
    互联网行业进入焦虑时代,如何才能做到攻守有道?
    88
    JZOJ.5331【NOIP2017模拟8.23】壕游戏
  • 原文地址:https://www.cnblogs.com/tomjobs/p/10612576.html
Copyright © 2011-2022 走看看