织芜 2024-02-18 16:06 采纳率: 72%
浏览 5
已结题

C++随机prim算法生成迷宫,无法成功,调试时有SIGTRAP信号

C++随机prim算法生成迷宫,无法成功,调试时有SIGTRAP信号

namespace RandomPrim {
    Maze generate(int row, int col) {
        Maze result(row, col);
        vector<bool>U(row * col, false);
        U[0] = true;
        set<pair<int, int>>edges;
        edges.insert(make_pair(0, 1));
        edges.insert(make_pair(0, col));

        int current = 0;
        while (!edges.empty()) {
            int pos = rand() % edges.size();
            set<pair<int, int>>::iterator edge = edges.begin();
            advance(edge, pos);

            int f = edge->first;
            int s = edge->second;
            current = (!U[f]) ? f : s;//((!U[s]) ? s : 0);

            result.connect(f, s);
            U[current] = true;

            //更新edges
            vector<int>sur = result.surrounded(current);
            for (vector<int>::iterator it = sur.begin(); it != sur.end(); it++) {
                f = (current > *it ? *it : current);
                s = (current < *it ? *it : current);
                pair<int, int>path = make_pair(f, s);
                if (!U[f] || !U[s]) {
                    edges.insert(path);
                } else {
                    edges.erase(edges.find(path));
                }
            }
        }
        return result;
    }
}
  • 写回答

2条回答 默认 最新

  • GISer Liu 2024-02-18 16:15
    关注

    该回答引用自GPT-3.5,由博主GIS_Liu编写:

    这段代码中的SIGTRAP信号问题可能是由于迭代器失效或越界访问导致的。让我们逐步分析并解决这个问题:

    1. 迭代器失效问题
      在循环中,当你向 edges 集合插入或删除元素时,可能会导致迭代器失效。因为在标准C++中,当你修改了一个容器,它可能会导致迭代器失效,从而使之前获得的迭代器无效。这可能导致程序崩溃或行为不确定。

    2. 随机数生成问题
      使用 rand() 函数生成随机数可能不是最佳选择,因为它的随机性不是很好,并且在多次迭代中可能会出现相同的随机数。建议使用更好的随机数生成器,比如 C++11 中的 <random> 头文件提供的随机数引擎。

    下面是修改后的代码:

    #include <iostream>
    #include <vector>
    #include <set>
    #include <random>
    using namespace std;
    
    namespace RandomPrim {
        class Maze {
        public:
            Maze(int row, int col) : row(row), col(col) {
                // Initialize your maze here
            }
            
            void connect(int f, int s) {
                // Connect two cells in the maze
            }
            
            vector<int> surrounded(int current) {
                // Return the surrounded cells of the current cell
                return {}; // Placeholder
            }
    
        private:
            int row;
            int col;
        };
    
        Maze generate(int row, int col) {
            Maze result(row, col);
            vector<bool> U(row * col, false);
            U[0] = true;
            set<pair<int, int>> edges;
            edges.insert(make_pair(0, 1));
            edges.insert(make_pair(0, col));
    
            // Better random number generation
            random_device rd;
            mt19937 gen(rd());
            
            int current = 0;
            while (!edges.empty()) {
                uniform_int_distribution<int> dis(0, edges.size() - 1);
                int pos = dis(gen); // Generate a random position
    
                auto edge = next(edges.begin(), pos); // Advance to the selected position
    
                int f = edge->first;
                int s = edge->second;
                current = (!U[f]) ? f : s;
    
                result.connect(f, s);
                U[current] = true;
    
                // Update edges
                vector<int> sur = result.surrounded(current);
                for (vector<int>::iterator it = sur.begin(); it != sur.end(); it++) {
                    f = (current > *it ? *it : current);
                    s = (current < *it ? *it : current);
                    pair<int, int> path = make_pair(f, s);
                    if (!U[f] || !U[s]) {
                        edges.insert(path);
                    } else {
                        edges.erase(edges.find(path));
                    }
                }
            }
            return result;
        }
    };
    
    int main() {
        RandomPrim::Maze maze = RandomPrim::generate(5, 5);
        // Do something with the generated maze
        return 0;
    }
    

    这个修改后的代码使用了更好的随机数生成器,并且在迭代器的使用上更加谨慎,避免了可能的迭代器失效问题。请注意,在 Maze 类中的 connectsurrounded 函数需要根据你的实际需求进行实现。

    img

    如果该回答解决了您的问题,请采纳!如果没有,请详述问题所在!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月27日
  • 已采纳回答 2月19日
  • 创建了问题 2月18日

悬赏问题

  • ¥15 STM32多路复用器ADC采样
  • ¥20 Linux(ubuntu)操作相关问题
  • ¥15 python深度学习代码求跑
  • ¥100 对接美团闪购医药接口相关问题
  • ¥15 嵌入式软件电子烟开发
  • ¥15 职场 Excel 查重问题
  • ¥15 求怎么用idea2021.3.2创建web项目并配置tomcat
  • ¥100 or-tools的相关问题
  • ¥15 有可能用平板通过拓展坞来烧录程序吗(keil5的那种)
  • ¥15 状态图的并发态问题咨询