zoukankan      html  css  js  c++  java
  • Oil Deposit

    题目描述:

    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.

    输入:

    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.

    输出:

    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.

    样例输入:
    1 1
    *
    3 5
    *@*@*
    **@**
    *@*@*
    1 8
    @@****@*
    5 5
    ****@
    *@@*@
    *@**@
    @@@*@
    @@**@
    0 0
    
    样例输出:
    0
    1
    2
    2

    图的遍历

    #include "stdafx.h"
    #include <stdio.h>
    using namespace std;
    
    const int MAX_VERTEX_NUM = 101;
    
    char cube[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
    bool mark[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
    int n, m;
    int go[][2] = {
        { 1, 0 }, { -1, 0 }, { 0, 1 }, {0,-1},
        { 1, 1 }, { 1, -1 }, { -1, 1 }, {-1,-1}
    };
    
    void DFS(int i, int j)
    {
        for (int k = 0; k < 8; k++)
        {
            int nx = i + go[k][0];
            int ny = j + go[k][1];
            if (nx < 0 || nx >= n || ny < 0 || ny >= m)
                continue;
            if (cube[nx][ny] == '*' || mark[nx][ny] == true)
                continue;
            mark[nx][ny] = true;
    
            DFS(nx, ny);
        }
    }
    
    int main()
    {
        int result;
        while (scanf("%d%d", &n, &m) != EOF)
        {
            if (n== 0 && m == 0)
                break;
            result = 0;
    
            for (int i = 0; i < n; i++)
                scanf("%s", cube[i]);
    
            for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
            {
                mark[i][j] = false;
            }
    
            for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
            {
                if (mark[i][j] == true || cube[i][j] == '*')
                    continue;
    
                DFS(i, j);
                result++;
            }
    
            printf("%d
    ", result);
        }
    
        return 0;
    }
  • 相关阅读:
    Spring Data MongoDB 一:入门篇(环境搭建、简单的CRUD操作)
    如何大幅提升web前端性能之看tengine在大公司架构实践
    SSM+redis整合(mybatis整合redis做二级缓存)
    Spring中报"Could not resolve placeholder"的解决方案(引入多个properties文件)
    Windows下安装Redis并注册为服务
    关于Local System/Local Service/Network Service账户
    在Windows下将Redis注册为本地服务
    Windows服务已经标记为删除
    SpringBoot集成MyBatis的分页插件PageHelper
    【Tomcat】Tomcat下设置项目为默认项目
  • 原文地址:https://www.cnblogs.com/tgycoder/p/5033518.html
Copyright © 2011-2022 走看看