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;
    }
  • 相关阅读:
    78. Subsets
    93. Restore IP Addresses
    71. Simplify Path
    82. Remove Duplicates from Sorted List II
    95. Unique Binary Search Trees II
    96. Unique Binary Search Trees
    312. Burst Balloons
    程序员社交平台
    APP Store开发指南
    iOS框架搭建(MVC,自定义TabBar)--微博搭建为例
  • 原文地址:https://www.cnblogs.com/tgycoder/p/5033518.html
Copyright © 2011-2022 走看看