动不了一点 2024-03-18 10:48 采纳率: 17.2%
浏览 0

请帮我看一下这个广度搜索哪里有问题


#include "iostream"
#include "queue"
using namespace std;
char map[32][32][32];
int bookmap1[32][32][32];
int next1[6][3] = { {0,1,0},{0,-1,0},{0,0,1},{0,0,-1},{1,0,0},{-1,0,0} };
int l, r, c;
int temp = 0;
struct point {
    int z;
    int x;
    int y;
    int step;
};
int main() {
    queue<point> q;
    cin >> l >> r >> c;
    while (1) {
        if (l == 0 && r == 0 && c == 0)break;
        for (int i = 0; i < l; i++, temp++) {
            {
                for (int j = 0; j < r; j++)
                    for (int z = 0; z < c; z++)
                    {
                        cin >> map[temp][j][z];
                        if (map[temp][j][z] == 'S')
                        {
                            q.push({ temp,j,z,0 });
                            map[temp][j][z] = '#';
                        }
                    }
                
            }
        }
        while (!q.empty()) {
            point t = q.front();
            q.pop();
            for (int i = 0; i < 6; i++) {
                int tz = t.x + next1[i][0];
                int tx = t.y + next1[i][1];
                int ty = t.z + next1[i][2];
                if (tz < 0 || tz >= l || tx < 0 || tx >= r || ty < 0 || ty >= c || map[tz][tx][ty] == '#' || bookmap1[tx][ty][tz] == 1)continue;
                if (map[tz][tx][ty] == '.') {
                    q.push(point{ tz,tx,ty,t.step + 1 });
                    bookmap1[tz][tx][ty] = 1;
                    continue;
                }
                if (map[tz][tx][ty] == 'E')
                {
                    printf("Escaped in %d minute(s).\n", t.step + 1);
                    
                }
            }

        }
        cout << "111";
        cin >> l >> r >> c;
    }

    return 0;
}
  • 写回答

4条回答 默认 最新

  • 关注

    动不了一点 上午好☀️☀️☀️️
    本答案参考ChatGPT-3.5

    在进行广度搜索时,需要注意以下几点问题:

    1. 在对map数组进行输入时,可以使用for循环嵌套,但是temp的增加需要在外部进行自增。另外,可以将输入字符与'S'进行比较时,使用单引号而不是双引号。

    2. 在判断tz、tx和ty是否越界时,需要将l、r和c作为判断条件,而不是使用x、y和z。

    3. 在判断map数组的值时,可以使用switch语句进行判断,而不是多个if语句。

    4. 在找到终点后,需要立即退出循环或者设置一个标志位进行退出,否则会继续进行搜索,导致出错。

    5. 在每一组测试数据结束后,需要重置bookmap1数组和temp变量的值,以便进行下一组测试数据。

    6. 在输出结果时,使用printf函数进行格式化输出。

    下面是修改后的代码:

    #include <iostream>
    #include <queue>
    using namespace std;
    
    char map[32][32][32];
    int bookmap1[32][32][32];
    int next1[6][3] = { {0,1,0},{0,-1,0},{0,0,1},{0,0,-1},{1,0,0},{-1,0,0} };
    int l, r, c;
    int temp = 0;
    
    struct point {
        int z;
        int x;
        int y;
        int step;
    };
    
    int main() {
        queue<point> q;
        cin >> l >> r >> c;
        while (l != 0 && r != 0 && c != 0) { // 1. 修改终止条件
            for (int i = 0; i < l; i++, temp++) {
                for (int j = 0; j < r; j++) {
                    for (int z = 0; z < c; z++) {
                        cin >> map[temp][j][z];
                        if (map[temp][j][z] == 'S') {
                            q.push({temp, j, z, 0});
                            map[temp][j][z] = '#';
                        }
                    }
                }
            }
    
            while (!q.empty()) {
                point t = q.front();
                q.pop();
                for (int i = 0; i < 6; i++) {
                    int tz = t.z + next1[i][0]; // 2. 修改越界条件
                    int tx = t.x + next1[i][1];
                    int ty = t.y + next1[i][2];
                    if (tz < 0 || tz >= l || tx < 0 || tx >= r || ty < 0 || ty >= c || map[tz][tx][ty] == '#' || bookmap1[tz][tx][ty] == 1)
                        continue;
                    switch (map[tz][tx][ty]) { // 3. 使用switch语句判断
                        case '.':
                            q.push(point{tz, tx, ty, t.step + 1});
                            bookmap1[tz][tx][ty] = 1;
                            break;
                        case 'E':
                            printf("Escaped in %d minute(s).\n", t.step + 1);
                            while (!q.empty()) q.pop(); // 4. 退出循环
                            break;
                    }
                }
            }
    
            // 5. 重置bookmap1数组和temp变量
            for (int i = 0; i < l; i++) {
                for (int j = 0; j < r; j++) {
                    for (int z = 0; z < c; z++) {
                        bookmap1[i][j][z] = 0;
                    }
                }
            }
            temp = 0;
    
            cin >> l >> r >> c;
        }
    
        return 0;
    }
    

    希望对你有帮助!

    评论

报告相同问题?

问题事件

  • 创建了问题 3月18日

悬赏问题

  • ¥15 微信小程序web-view嵌套H5页面IOS左滑会出现相同的页面,有什么解决方法吗?
  • ¥60 如何批量获取json的url
  • ¥15 comsol仿真压阻传感器
  • ¥15 Python线性规划函数optimize.linprog求解为整数
  • ¥15 llama3中文版微调
  • ¥15 pg数据库导入数据序列重复
  • ¥15 三分类机器学习模型可视化分析
  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
  • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
  • ¥30 python,LLM 文本提炼