博客
关于我
Oil Deposits
阅读量:792 次
发布时间:2023-02-22

本文共 3520 字,大约阅读时间需要 11 分钟。

The GeoSurvComp geologic survey company specializes in detecting underground oil deposits by analyzing large rectangular grids. The company divides the land into square plots and uses sensors to identify oil pockets, which are plots containing oil. Two adjacent pockets (including diagonally adjacent ones) are considered part of the same oil deposit. Your task is to determine the number of distinct oil deposits in a given grid.

Input Format

The input consists of one or more grids. Each grid starts with a line containing two integers, m and n, representing the number of rows and columns. If m is 0, it indicates the end of the input. Each subsequent line contains n characters, where '*' represents no oil and '@' represents an oil pocket. The input may contain multiple grids, each with its own m and n values.

Sample Input

1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0

Output

The output should be a sequence of numbers, each representing the number of oil deposits in the corresponding grid. For example:

0122

Key Points

  • Oil deposits are defined as groups of adjacent oil pockets, where adjacency includes horizontal, vertical, and diagonal connections.
  • Each oil deposit cannot contain more than 100 pockets.
  • The task is computationally intensive, as it involves analyzing grids of up to 100x100 size.

Approach

To solve this problem, a Depth-First Search (DFS) algorithm is typically used to explore and mark all connected pockets of oil. Here’s a step-by-step breakdown of the approach:

  • Grid Traversal: Iterate through each cell in the grid.
  • DFS Initialization: When an oil pocket '@' is found, initiate a DFS to mark all connected pockets.
  • Marking Visited Pockets: During the DFS, mark each visited pocket to avoid counting it multiple times.
  • Deposit Counting: Increment the deposit count each time a new DFS is initiated.
  • Solution Code

    #include 
    #include
    using namespace std;
    int main() {
    int m, n;
    vector
    > grid(100+1, vector
    (100+1));
    int count = 0;
    while (true) {
    cin >> m >> n;
    if (m == 0) break;
    for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
    grid[i][j] = cin.get();
    }
    }
    vector
    > oilDeposits; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == '@') { bool visited[m][n] = {false}; queue
    > q; q.push({i, j}); visited[i][j] = true; int currentDeposit = 0; while (!q.empty()) { auto current = q.front(); q.pop(); for (int dx = -1; dx <= 1; ++dx) { for (int dy = -1; dy <= 1; ++dy) { if (dx == 0 && dy == 0) continue; int x = current.first + dx; int y = current.second + dy; if (x >= 0 && x < m && y >= 0 && y < n && !visited[x][y] && grid[x][y] == '@') { visited[x][y] = true; q.push({x, y}); } } } } oilDeposits.push_back({i, j}); ++count; } } } cout << count; if (m == 0) break; } return 0; }

    Explanation

    The code reads multiple grids and counts the number of oil deposits in each grid. For each grid, it uses a DFS algorithm to explore all connected oil pockets, marking them as visited to avoid recounting. Each new DFS initiation corresponds to a new oil deposit. The result is printed as a sequence of numbers, each representing the count of oil deposits in the corresponding grid.

    This approach ensures that all adjacent pockets (including diagonally adjacent ones) are counted correctly, and it efficiently handles grids of up to 100x100 size.

    转载地址:http://stsfk.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现链表reverseTraversal反向遍历算法(附完整源码)
    查看>>
    Objective-C实现链表traversal遍历算法(附完整源码)
    查看>>
    Objective-C实现链表交换节点算法(附完整源码)
    查看>>
    Objective-C实现链表尾插法(附完整源码)
    查看>>
    Objective-C实现链表尾插法(附完整源码)
    查看>>
    Objective-C实现链表逆转(附完整源码)
    查看>>
    Objective-C实现键盘操控(附完整源码)
    查看>>
    Objective-C实现长短期记忆人工神经网络LSTM(附完整源码)
    查看>>
    Objective-C实现闭式函数计算特定位置的斐波那契数fibonacciNthClosedForm算法(附完整源码)
    查看>>
    Objective-C实现队列(附完整源码)
    查看>>
    Objective-C实现阶乘(附完整源码)
    查看>>
    Objective-C实现阶乘递归factorialRecursive算法(附完整源码)
    查看>>
    Objective-C实现阿特巴希密算法(附完整源码)
    查看>>
    Objective-C实现随机图生成器算法(附完整源码)
    查看>>
    Objective-C实现随机数生成器(附完整源码)
    查看>>
    Objective-C实现随机森林算法(附完整源码)
    查看>>
    Objective-C实现随机正态分布快速排序算法(附完整源码)
    查看>>
    Objective-C实现随机生成一个 RxC 列联表(附完整源码)
    查看>>
    Objective-C实现隐藏任务栏(附完整源码)
    查看>>
    Objective-C实现隔离数字的小数部分, 取这个数字并从底数中减去它,返回结果算法(附完整源码)
    查看>>