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;
    }
  • 相关阅读:
    JVM系列六(自定义插入式注解器).
    JVM系列五(Javac 字节码编译器).
    2019 — 求不得,放不下
    Mybatis 条件判断单双引号解析问题
    JVM系列四(对象分配策略).
    JVM系列三(垃圾收集器).
    Spring MVC -- Spring Tool Suite和Maven(安装Tomcat、JDK)
    Spring MVC -- 单元测试和集成测试
    Spring MVC -- 下载文件
    Spring MVC -- 上传文件
  • 原文地址:https://www.cnblogs.com/tgycoder/p/5033518.html
Copyright © 2011-2022 走看看