L - Oil Deposits 深度优先搜素dfs

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即可。
首先判断是否为@,如果是,那么开始从@处dfs,并且计数一次。
如此往复,进行了几次dfs,那么就有几处油田。值得注意的是,这次是走八个方向

具体细节在注释说明

    //#include 
#include
#include
#include
using namespace std;
#define ll long long
const int  INF = 0x3f3f3f3f;
const ll LIMIT = 4294967295LL;
const ll maxn = -999999999;
const ll minn = 10000000010;
using namespace std;
const ll maxnn = 100000 + 100;
char a[120][120];
char c[100100];
bool vis[120][120];
int dir[8][2] = {
     {
     1, 1}, {
     1, -1}, {
     1, 0}, {
     0, 1}, {
     0, -1}, {
      -1, 0}, {
      -1, 1}, {
      -1, -1}};//定义八个方向
int  sum = 0, cnt = 0,n,m;
int v;
void dfs(int i, int j) {
     
    int dx, dy, x;
    vis[i][j] = 1;
    for(int x = 0; x < 8; x++) {
     
        dx = i + dir[x][0];
        dy = j + dir[x][1];
        if(dx<0||dx>=n||dy<0||dy>=m)//不剪枝可能会wa。
        continue ;
        if(a[dx][dy] != '@')
            continue;
        if(vis[dx][dy])
            continue;
        dfs(dx, dy);
    }
    return ;

}
int main() {
     
    int i,j;
    while(cin >> n >> m,n&&m) {
     
        for(i = 0; i < n; i++)
            cin>>a[i];
            sum=0;
        for(i = 0; i < n; i++)
            for(j = 0; j < m; j++)
                if(a[i][j] == '@' && vis[i][j]!= 1) {
     //判断是否是油田,并且从未标记,那么它就是一个崭新的整体,从它开始dfs寻找可以跟它构成整体的油田。
                    dfs(i,j);
                    sum++;
                }
        cout << sum << endl;
        memset(a, 0, sizeof(a));
        memset(vis, 0, sizeof(vis));
    }
    return 0;
}
///***Created by 软件我最菜 ***ACM******QWQ·***

你可能感兴趣的:(L - Oil Deposits 深度优先搜素dfs)