• 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;
    }
  • 相关阅读:
    购买成熟软件产品后的二次开发的问题
    outlook2010如何导入csv的通讯录?
    导入Excel数据时对数据校验提示方法
    系统开发中存储过程使用的优势和劣势
    FCKeditor.Net_2.5的使用
    [正则表达式]如何高亮显示搜索关键字
    国外网站模板网址集锦
    _NET 下 FCKeditor_2_5_1上传图片的配置
    用属性模拟多继承机制
    FCKeditor 2.6在ASP.NET中的配置方法
  • 原文地址:https://www.cnblogs.com/tgycoder/p/5033518.html
Copyright © 2020-2023  润新知