kuangbin专题一 简单搜索 石油储备(HDU-1241)

发布时间 2023-04-15 20:40:55作者: Amαdeus

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Problem Description

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或者BFS都行,这里给一个DFS的代码。

/*   一切都是命运石之门的选择  El Psy Kongroo  */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<functional>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<double, double> pdd;
typedef pair<string, pii> psi;
typedef __int128 int128;
#define PI acos(-1.0)
#define x first
#define y second
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;

const int N = 110;
char g[N][N];
int vis[N][N];
int n, m;
int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1};


void dfs(int i, int j){
    vis[i][j] = 1;
    for(int k = 0; k < 8; k++){
        int ni = i + dx[k], nj = j + dy[k];
        if(ni < 0 || ni >= n || nj < 0 || nj >= m || vis[ni][nj]) continue;
        if(g[ni][nj] != '@') continue;
        dfs(ni, nj);
    }
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    while(cin >> n >> m && (n && m)){
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; i ++ )  cin >> g[i];

        int ans = 0;
        for(int i = 0; i < n; i ++ )
            for(int j = 0; j < m; j ++ )
                if(!vis[i][j] && g[i][j] == '@')
                    ans ++ , dfs(i, j);
        
        cout << ans << endl;
    }
    
    return 0;
}