0

我目前正在为我的上层 C++ 类开发一个项目,我们正在构建一个程序来制作迷宫,然后解决它,制作所述迷宫的 PNG。很酷的东西。无论如何,我目前正在我需要制作迷宫的地方。我的程序使有效的迷宫很好,但我必须使每个数字输出都是唯一的。输出只是在 2d 矩阵中吐出两个指标,它们之间有墙,3X4 迷宫的样本输出如下:

rjeffor1:hydra20 ~/cs302/labs/lab5> ./mazemake 3 4 <- 9:49AM

1 2

1 5

2 1

2 3

3 2

5 1

5 9

6 7

7 6

8 9

9 8

9 5

但是,我的最后一个问题是我需要摆脱重复的墙,例如 1 2 和 2 1。编辑:我的意思是只要摆脱 2 1,我仍然需要墙,因此需要 1 2。

这是我尝试解决问题的函数:

void aL::make_unique()
{
    vector<int>::iterator it, it0;

    //need to iterate thru all but last index

    for (int i=0; i<(int)adjList.size()-1; i++) {

        for (int j=0; j<(int)adjList.size(); j++) {

            //find it

            if (i!=j) {
                it0 = std::find(adjList[i].begin(), adjList[i].end(), j);
                it = std::find(adjList[j].begin(), adjList[j].end(), i);
                if (it!=adjList[j].end() && it!=adjList[j].end())
                    //erase it if anything is there
                    adjList[j].erase(it);
            }
        }
    }
}

感谢您的帮助,我的大脑在这一点上已经完成了

编辑:这是我填充邻接列表的方式,基于每个索引正上方和下方的索引

aL::aL (const int &rows, const int &cols)
{
    adjList.resize(rows*cols);
    //run thru and figure out where indicies AREN'T
    //to fill in their adjacency list
    for (int i=0; i<(int)adjList.size(); i++) {
        //if not on the left edge
        if (i%cols!=0)
            adjList[i].push_back(i-1);
        //not on the right edge
        if ((i+1)%cols!=0)
            adjList[i].push_back(i+1);
        //not on the top edge
        if (i>=cols)
            adjList[i].push_back(i-cols);
        //not on the bottom edge
        if (i<(rows*cols)-cols)
            adjList[i].push_back(i+cols);
    }
}
4

1 回答 1

0

如果您在添加到列表时进行检查,则可以消除后处理的需要并在最后使其唯一。如果“b a”已经存在,则不要添加“a b”。

于 2013-04-05T14:09:02.393 回答