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;
    }
    
    
  • 相关阅读:
    清除浮动的方式
    网页在线测试工具
    仿京东左侧菜单 hover效果-简易demo
    原生js,插入元素
    知识补漏
    css3动画
    java微信开发(wechat4j)——支持微信JS-SDK的jsapi_ticket中控服务器
    java微信开发(wechat4j)——access_token中控服务器实现
    java微信开发(wechat4j)——wechat4j配置文件解读
    java微信开发(wechat4j)——设置响应微信参数
  • 原文地址:https://www.cnblogs.com/tomjobs/p/10612576.html
Copyright © 2011-2022 走看看