Enji_Xiong 2024-05-05 17:06 采纳率: 0%
浏览 5

POJ-2386,数湖泊问题

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.

这是POJ-2386题目,以下是我的代码,我不明白为什么在我输入
4 4
.WW.
WW.W
.(这一排是4个.号)
W.W.
的时候,它会输出 4 而不是正确答案 3。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;

const int maxn = 105;
struct node{
    int x,y;
}Node;

int n,m;
char farm[maxn][maxn];
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
bool vis[maxn][maxn] = {false};
int ans=0;
//判断当前这个点(x,y)是否合格
bool judge(int x,int y){
    if(x>=n||x<0||y>=m||y<0) return false;
    if(vis[x][y]) return false;
    if(farm[x][y]=='.') return false;
    return true;
}
//广搜:八个方向都搜一遍,满足相邻条件就标记为访问过
void bfs(int x, int y){
    queue<node> q;
    Node.x = x, Node.y = y;
    q.push(Node);
    vis[x][y] = true; 
    while(!q.empty()){
        node top = q.front();
        q.pop();
        for(int i = 0; i < 8; i++){
            int xx = top.x + dx[i];
            int yy = top.y + dy[i];
            if(judge(xx, yy)){
                Node.x = xx, Node.y = yy;
                q.push(Node);
                vis[xx][yy] = true; 
            }
        }
    }
}
int main(){
    cin>>n>>m;
    for(int x=0;x < n; x++){
        for(int y=0;y<m; y++){
            cin>>farm[x][y];
        }
    }
//遍历整个farm,如果存在没有访问过的点W,就开始广搜,直至辐射范围结束。
    for(int x=0;x < n; x++){
        for(int y=0;y<m; y++){
            if(farm[x][y]=='W'&&!vis[x][y]){
                bfs(x,y);
                ans++;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

  • 写回答

5条回答 默认 最新

  • 笑面虎 2024-05-05 17:35
    关注

    以下内容由CHATGPT及笑面虎共同生成、有用望采纳:


    【问题概括】 这个问题是关于图论中的广度优先搜索(BFS)算法的应用。目的是在一个二维矩阵(代表农夫John的田地)中,计算由'W'字符(代表水)组成的独立湖泊的数量。湖泊被定义为一组相连的水方块,其中每个方块可以与它的八个邻居相连。

    【问题出现原因】 根据提供的代码和描述,问题出现的原因是在进行广度优先搜索(BFS)时,可能没有正确地识别和计数独立的湖泊。代码中的bfs函数负责搜索湖泊,但是可能存在逻辑错误,导致某些湖泊没有被正确计数。

    【问题解决方案】 为了解决这个问题,我们需要确保在搜索过程中,每个湖泊都被独立地识别和计数。这通常涉及到使用一个访问标记数组来记录已经被访问过的节点,以避免重复计数。

    【提供代码】 以下是修改后的代码,它应该能够正确地计算独立湖泊的数量:

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    
    using namespace std;
    
    const int maxn = 105;
    struct node{
        int x, y;
    };
    
    int n, m;
    char farm[maxn][maxn];
    int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
    int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
    bool vis[maxn][maxn] = {false};
    int ans = 0;
    
    bool judge(int x, int y) {
        if (x >= n || x < 0 || y >= m || y < 0) return false;
        if (vis[x][y]) return false;
        if (farm[x][y] == '.') return false;
        return true;
    }
    
    void bfs(int x, int y) {
        queue<node> q;
        q.push({x, y});
        vis[x][y] = true;
        while (!q.empty()) {
            node top = q.front();
            q.pop();
            for (int i = 0; i < 8; i++) {
                int xx = top.x + dx[i];
                int yy = top.y + dy[i];
                if (judge(xx, yy)) {
                    q.push({xx, yy});
                    vis[xx][yy] = true;
                }
            }
        }
    }
    
    int main() {
        cin >> n >> m;
        for (int x = 0; x < n; x++) {
            for (int y = 0; y < m; y++) {
                cin >> farm[x][y];
            }
        }
        for (int x = 0; x < n; x++) {
            for (int y = 0; y < m; y++) {
                if (farm[x][y] == 'W' && !vis[x][y]) {
                    bfs(x, y);
                    ans++;
                }
            }
        }
        cout << ans << endl;
        return 0;
    }
    

    【代码运行方式】 这段代码是用C++编写的,需要一个支持C++编译的IDE或者编译器。你可以将代码复制到一个C++文件中,然后使用g++或clang++等编译器编译,生成可执行文件后运行。

    【代码预期运行结果】 如果你使用提供的输入:

    4 4
    .WW.
    WW.W
    ....
    W.W.
    

    预期的输出应该是:

    3
    

    【推荐相关链接】

    1. 广度优先搜索算法
    2. POJ-2386题目链接
    3. C++标准模板库(STL)
    4. 结构体在C++中的使用
    评论 编辑记录

报告相同问题?

问题事件

  • 修改了问题 5月5日
  • 创建了问题 5月5日

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见