题解报告:POJ 2386--Lake Counting(BFS+DFS)

这篇博客介绍了如何使用广度优先搜索(BFS)和深度优先搜索(DFS)解决一个图形问题,即在给定的二维网格中计算被'W'字符表示的池塘数量。题目要求池塘的八个相邻方向上如果有水,则只计算一个连通块。作者提供了两种算法的C++实现,并给出了样例输入和输出。通过这两种方法,可以有效地找出所有独立的池塘连通块。
摘要由CSDN通过智能技术生成

传送门
Description
Due to recent rains, water has pooled in various places in Farmer John’s field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water (‘W’) or dry land (’.’). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

Given a diagram of Farmer John’s field, determine how many ponds he has.
Input

Line 1: Two space-separated integers: N and M

Lines 2…N+1: M characters per line representing one row of Farmer John’s field. Each character is either ‘W’ or ‘.’. The characters do not have spaces between them.

Output

Line 1: The number of ponds in Farmer John’s field.
Sample Input
10 12
w…ww.
.www…www
…ww…ww.
…ww.
…w…
…w…w…
.w.w…ww.
w.w.w…w.
.w.w…w.
…w…w.
Sample Output
3

题意:给你一副图n行m列,其中’W’代表池塘,’.'代表土地,问现在这幅图中有多少个池塘,如果一个池塘的八个方向上如果有池塘,则只算一个,类似于连通块,求有多少个连通块。
思路:bfs和dfs

BFS

用队列实现,需要用到pair或者结构体存储下标,通过找到一个w然后用队列实现连通功能的块

#include<iostream>
#include<queue>
#include<map>
using namespace std;

const int maxn = 105;
char a [maxn][maxn];
pair<int ,int >m1;
typedef pair<int ,int >P;
queue<pair<int ,int > >mo;

void bfs(int x,int y ,int n ,int m){
    int i,j;
    while(!mo.empty()){
             P d = mo.front();
             mo.pop();
             for(i=-1;i<=1;i++){
                    for(j=-1;j<=1;j++){
                        int x1,y1;
                        x1=d.first+i,y1=d.second+j;
                        if(x1>=0&&x1<n&&y1>=0&&y1<m&&a[x1][y1]=='W'){
                                a[x1][y1]='.';
                                mo.push(P(x1,y1));
                        }
                    }
             }
    }
}

int solve(int  n,  int m ){
    int ans = 0,i,j;
    for(i = 0 ;i < n ; i++){
        for( j = 0 ;j < m ;j++){
            if(a[i][j]=='W'){
                ans++;
                mo.push(P(i,j));
                bfs(i,j,n,m);
            }
        }
    }
    return ans;
}
int  main()
{
    int n , i , j , m ;
    cin >> n >> m;
    for(i = 0; i < n; i ++){
         for(j = 0; j < m ;j++)
                cin >> a[i][j];
    }
    int ans=0;
    ans=solve(n,m);
    cout << ans <<endl;
}

DFS

查找到了为水坑的低点之后就继续搜索不断查找=。=

#include<iostream>
using namespace std;
const int maxn = 105;
char a [maxn][maxn];

void dfs(int x, int y,int n,int m  ){
    int i,j,b,c;
    for(i = -1;i <= 1;i++){
        for(j = -1 ;j <=1 ;j++){
               b=i+x;
               c=y+j;
               if(b>=0&&b<n&&c>=0&&c<m&&a[b][c]=='W'){
                    a[b][c]='.';
                    dfs(b,c,n,m);
               }
        }
    }

}
int solve (int n,int m ){
    int ans = 0,i,j;
    for(i = 0; i  < n ;i ++){
        for( j = 0 ; j < m ;j ++){
            if(a[i][j]=='W'){
                dfs(i,j,n,m);
                ans++;
            }
        }
    }
    return ans;
}
int  main()
{
    int n , i , j , m ;
    cin >> n >> m;
    for(i = 0; i < n; i ++){
         for(j = 0; j < m ;j++)
                cin >> a[i][j];
    }
    int ans=0;
    ans=solve(n,m);
    cout << ans <<endl;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值