您的位置:首页 > 其它

【HDUOJ1241】bfs&&dfs

2017-08-05 13:48 274 查看
点击打开链接

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

题意:@表示油田,若有一个@,那它自己就是一个油田,若有好多@挨着,它们一起是一个油田两种方法写

AC代码:

//DFS深度搜索 递归调用 

/*
先找到第一个符合条件的,然后以它为起点寻找它周围与他相邻的,

找到一个相邻的就开始以这个相邻的为中心,依次找; 


#include<cstdio>

#include<cmath>

#include<cstring>

#include<algorithm>

using namespace std;

const int MAX = 1e2 + 10;

typedef long long LL;

char s[MAX][MAX];//输入字符串的数组 

int n,m,vis[MAX][MAX],ans;//vis数组用来标志油田 

int fx[8] = {0,0,-1,1,-1,1,-1,1},fy[8] = {-1,1,0,0,-1,-1,1,1}; // 8 个方向 

void dfs(int x,int y){

    vis[x][y] = 1;

    for(int i = 0; i < 8; i++){

        int xx = x + fx[i],yy  = fy[i] + y;

        if(xx >= 0 && yy >= 0 && xx < n && yy < m && !vis[xx][yy] && s[xx][yy] == '@')// 判断是否出界, 是否已经搜索过, 是否为油田 

            dfs(xx,yy);//自己调用自己,递归调用 

    }

}

int main()

{

    while(~scanf("%d %d",&n,&m),m){

        memset(vis,0,sizeof(vis));//函数原型void *memset(void*,int,unsigned);

        for(int i = 0; i < n; i++)//memset()的函数, 它可以一字节一字节地把整个数组设置为一个指定的值。
//memset()函数在mem.h头文件中声明,它把数组的起始地址作为其第一个参数,
//第二个参数是设置数组每个字节的值,
//第三个参数是数组的长度(字节数,不是元素个数)。 

            scanf("%s",s[i]);

        ans = 0;

        for(int i = 0; i < n; i++)

            for(int j = 0; j < m; j++)

                if(!vis[i][j] && s[i][j] == '@')

                    ans++,dfs(i,j); // 第一次搜到 + 1 

        printf("%d\n",ans);

    }

    return 0;

}

*/ 

//BFS :广度搜索 队列 ,先找到第一个符合条件的,然后以它为起点寻找它周围与他相邻的直到找完, 

#include<cstdio>         //然后以他周围与他相邻的为起点接着找,依次循环,直到没有与他们相邻的为止 

#include<queue>

#include<cstring>

#include<algorithm>

using namespace std;

const int MAX = 1e2 + 10;//const是一个C语言的关键字,它限定一个变量不允许被改变

typedef long long LL;

char s[MAX][MAX];

int n,m,vis[MAX][MAX],ans;

int fx[8] = {0,0,-1,1,-1,1,-1,1},fy[8] = {-1,1,0,0,-1,-1,1,1};

struct node{

    int x,y;

};

void bfs(int x,int y){

    vis[x][y] = 1;

    queue <node> q;

    node o;

    o.x = x,o.y = y;

    q.push(o);

    while(!q.empty()){

        o = q.front();

        q.pop();

        for(int i = 0; i < 8; i++){ // 8 个方向  

            int xx = o.x + fx[i],yy = o.y + fy[i];

            if(xx >= 0 && yy >= 0 && xx < n && yy < m && !vis[xx][yy] && s[xx][yy] == '@'){ // 判断是否出界, 是否已经搜索过, 是否为油田 

                node w;

                vis[xx][yy] = 1;

                w.x = xx,w.y = yy;

                q.push(w);

            }

        }

    }

}

int main()

{

    while(~scanf("%d %d",&n,&m),m){

        memset(vis,0,sizeof(vis));

        for(int i = 0; i < n; i++)

            scanf("%s",s[i]);

        ans = 0;

        for(int i = 0; i < n; i++)

            for(int j = 0; j < m; j++)

                if(!vis[i][j] && s[i][j] == '@')

                    ans++,bfs(i,j); // // 第一次搜到 + 1  

        printf("%d\n",ans);

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bfs dfs