Oil Deposits
发布日期:2025-04-27 23:45:14 浏览次数:16 分类:精选文章

本文共 3506 字,大约阅读时间需要 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.

    上一篇:oj2894(贝尔曼福特模板)
    下一篇:Ogre 插件系统

    发表评论

    最新留言

    表示我来过!
    [***.240.166.169]2026年06月11日 21时24分13秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章