K - Oil Deposits(dfs或bfs解决)

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


题意:有一个或多个网格里面含有石油,如果石油是相邻的我们就认为它们属于同一个油藏,问在给定的石油网格中有多少不同的石油油藏。


解题思路:由于我们要求的是不同的石油油藏,没有任何限制,得到结果就行,那么我们既可以用bfs又可以用dfs,我会贴出两种方法的代码,先说思路。
首先,我们在这个题中不能设置辅助数组判断是否已访问。这是为什么呢?因为我们是从石油网格中进行搜索,即从每个有油处的地方开始搜索,如果我们只是对搜索过的有油处设立已访问的表示,那么在接下来,还会通过那个已访问的有油处进行搜索,显然这是行不通的,最后得到的结果就是在这个石油网格中有多少油腔。
那么,我们就来点直接的,直接将已访问的有油处‘@’至为无油处‘*’,那么下次到这的时候由于无油无法从这点开始搜索,避免了重复搜索。OK,那么就很简单了 ,对于dfs直接用递归来实现,对于bfs用队列来实现。


AC代码

bfs:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<cstring>
#include<memory.h>
#include<map>
#include<iterator>
#include<list>
#include<set>
#include<functional>

using namespace std;


const int maxn=102;
char oil_map[maxn][maxn];//地下石油
int go[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};//操作行为。
struct node{
    int x,y;//坐标
};
node temp,head;//辅助变量。
int n,m;//n*m的地下石油
void bfs(int x,int y){
    oil_map[x][y]='*';
//可以这样理解,相当于这里已经归属自己的地方了,对别人来说是不能取的,那不就跟没油一样,为了方便我们直接改为'*';
	queue<node> q;
	head.x=x;head.y=y;
	q.push(head);
	while(!q.empty()){
	head=q.front();
	q.pop();
    for(int i=0;i<8;i++){
			temp.x=head.x+go[i][0];temp.y=head.y+go[i][1];
			if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m&&oil_map[temp.x][temp.y]=='@'){
            //看看是否越界和是否被访问过以及是否有油。
            q.push(temp);
            oil_map[temp.x][temp.y]='*';
        }
    }
	}
}
int main() {
    int sum;//统计油藏数量。
    while(cin>>n>>m&&n){
        sum=0;
        for(int i=0;i<n;i++)
            cin>>oil_map[i];
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            if(oil_map[i][j]=='@'){
                bfs(i,j);
                sum++;
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

dfs:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<cstring>
#include<memory.h>
#include<map>
#include<iterator>
#include<list>
#include<set>
#include<functional>

using namespace std;


const int maxn=102;
char oil_map[maxn][maxn];//地下石油
int go[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};//操作行为。
struct node{
    int x,y;//坐标
};
node temp;//辅助变量。
int n,m;//n*m的地下石油
void dfs(int x,int y){
    oil_map[x][y]='*';
    //可以这样理解,相当于这里已经归属自己的地方了,对别人来说是不能取的,那不就跟没油一样,为了方便我们直接改为'*';
    for(int i=0;i<8;i++){
			temp.x=x+go[i][0];temp.y=y+go[i][1];
			if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m&&oil_map[temp.x][temp.y]=='@'){
            //看看是否越界和是否被访问过以及是否有油。
            dfs(temp.x,temp.y);
            //继续寻找。
        }
    }
}
int main() {
    int sum;//统计油藏数量。
    while(cin>>n>>m&&n){
        sum=0;
        for(int i=0;i<n;i++)
            cin>>oil_map[i];
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            if(oil_map[i][j]=='@'){
                dfs(i,j);
                sum++;
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hzf0701/article/details/107589145