HDU 1241 Oil Deposits DFS求连通块

来源:互联网 发布:sql server时间戳转换 编辑:程序博客网 时间:2024/06/10 04:36

HDU 1241 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.

题目意思:求所有独立的@的块有多少个,@周围的(八个方向)算一个块。

dfs计数就好

////  main.cpp//  whr_bfs////  Created by  41 on 17/8/7.//  Copyright (c) 2017年 henuwhr. All rights reserved.//#include <iostream>#include <cstdio>#include <algorithm>#include <queue>#include <map>#include <cstring>using namespace std;int n,m;char mp[105][105];int res = 0;int dir[][2]={1,0,1,-1,0,-1,-1,-1,-1,0,-1,1,0,1,1,1};void dfs(int x,int y){    int i,xx,yy;    mp[x][y] = '*';    for(i = 0 ;i<8;i++){        xx = x+dir[i][0];        yy = y+dir[i][1];        if(xx<0||yy<0||xx>=m||yy>=n)            continue;        if(mp[xx][yy]=='@')        dfs(xx,yy);    }}int main(int argc, const char * argv[]) {    while(cin>>m>>n,m+n){        res = 0;        memset(mp,0,sizeof mp);        for(int i = 0;i<m;i++){            cin>>mp[i];        }        for(int i = 0 ;i<m;i++){            for(int j = 0 ;j<n;j++){                if(mp[i][j]=='@'){                    dfs(i,j);                    res++;                }            }        }        cout << res <<endl;    }    return 0;}
原创粉丝点击