572 - Oil Deposits

来源:互联网 发布:java实例变量 编辑:程序博客网 时间:2024/05/19 02:28

Oil Deposits

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

输入一个m行n列的字符矩阵,统计字符“@”组成少个八连块。如果两个字符“@”所在的格子相邻(横、竖或者对角线方向),就说它们属于同一个八连块。

样例输入:

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

样例输出:

0
1
2
2

#include <iostream>#include <cstring>#include <string>using namespace std;const int maxNum = 105;// m行,n列int m, n;// 标记数组int mark[maxNum][maxNum];// 符号数组string oil[maxNum];// 深度优先遍历找到相邻油槽void dfs(int row, int column, int num) {    // 出界    if(row < 0 || row >=m || column < 0 || column >= n) {        return;    }    // 非油槽    if(oil[row][column] == '*') {        return;    } else if(oil[row][column] == '@') {        // 未访问过的才递归        if(mark[row][column] == 0) {            // 标记为第几个油槽            mark[row][column] = num;            // 遍历当前节点的上下左右,四个斜角            for(int offsetX = -1;offsetX <= 1; offsetX++) {                for(int offsetY = -1; offsetY <= 1; offsetY++) {                    dfs(row + offsetX, column + offsetY, num);                }            }        }    }}int main() {    while(cin >> m >> n) {        if(!m) {            break;        }        // 初始化        memset(mark, 0, sizeof(mark));        for(int i = 0; i < m; i++) {            cin >> oil[i];        }        // 油田数        int ans = 0;        for(int i = 0; i < m; i++) {            for(int j = 0; j < n; j++) {                // 找到没有访问过的油田的一个位置                if(mark[i][j] == 0 && oil[i][j] == '@') {                    // 进行递归                    dfs(i, j, ++ans);                }            }        }        cout << ans << endl;    }    return 0;}
0 0
原创粉丝点击