11月 29, 2008

【解題】Oil Deposits

@
ACM Volume V 572 - Oil Deposits


The Problem

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.


Input

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.


Output

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.


Sample Input

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


Sample Output

0
1
2
2


解題思考

  一般來說,這一題我會使用遞迴,利用 DFS(depth-first search,深度優先搜尋)求解。不過,因為某人堅持要我用我不擅長的 BFS(breadth-first search,廣度優先搜尋)來寫......。

  Anyway,其實核心想法都是差不多的。


  顯然的,這一題的問題點在於:判斷哪幾個 pocket 屬於同一個 oil deposit。

  首先,我們需要利用迴圏找到一個 pocket,並把所有跟這個 pocket 相鄰的 pocket 推進一個 BFS 的佇列(或是 DFS 的堆疊)中。在推入的同時,我們還需要為這個 pocket 標上一個「搜尋過」的記號(在這裡我是利用把 pocket 標記消掉的方式)。

  直到佇列(或堆疊)為空,就代表我們已經搜遍同一個 oil deposit 的所有 pocket 了。所以,我們繼續執行迴圈,找到下一個沒有被搜尋過的 pocket,再同樣進行搜尋相鄰 pocket 的動作。再繼續執行迴圈,重覆相同的動作。直到找遍所有的 pocket 為止。

  有了這種想法,題目所要求的解也就呼之欲出了!


參考解答(C++)

#include <iostream>
#include <queue>

using namespace std;

struct coord
{
    coord(int i, int j) { x = i; y = j; };

    int x, y;
};

int main(void)
{
    while (1)
    {
        int m, n;
        cin >> m >> n;

        if (!m && !n) { break; }

        // 動態配置記憶體
        bool **oil = new bool*[m + 2];
        for (int i = 0; i < m + 2; i++)
        {
            oil[i] = new bool[n + 2];

            // 陣列初始化
            memset(oil[i], 0, n + 2);
        }

        // 讀入每一個 pocket
        char *get = new char[n + 1];
        for (int i = 1; i <= m; i++)
        {
            cin >> get;
            for (int j = 1; j <= n; j++)
            {
                oil[i][j] = (get[j - 1] == '@');
            }
        }

        delete [] get;

        int num = 0;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (oil[i][j])
                {
                    oil[i][j] = false;

                    num++;

                    // 利用 BFS 找出相鄰的 pocket
                    queue bfs;
                    bfs.push(coord(i, j));

                    do
                    {
                        int &x = bfs.front().x, &y = bfs.front().y;
                        for (int p = -1; p <= 1; p++)
                        {
                            for (int q = -1; q <= 1; q++)
                            {
                                if (oil[x + p][y + q])
                                {
                                    oil[x + p][y + q] = false;

                                    // 推入相鄰的 pocket 進佇列中
                                    bfs.push(coord(x + p, y + q));
                                }
                            }
                        }

                        bfs.pop();
                    } while (bfs.size());
                }
            }
        }

        cout << num << endl;

        // 釋放記憶體空間
        for (int i = 0; i < m + 2; i++)
        {
            delete [] oil[i];
        }

        delete [] oil;
    }

#ifndef ONLINE_JUDGE
    system("pause");
#endif
}

0 回覆:

張貼留言