摆脱邻接列表中的非唯一条目

时间:2013-04-05 14:03:58

标签: c++ maze adjacency-list

我正在为我的高级C ++课程开发一个项目,我们正在构建一个制作迷宫的程序,然后解决它,制作一个迷宫的PNG。很酷的东西。无论如何,我现在正处于我需要制作迷宫的位置。 我的程序使有效的迷宫很好,但我必须使每个数字输出唯一。输出只是在2d矩阵中吐出两个标记,它们之间有墙,3X4迷宫的样本输出如下:

  

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

     

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);
    }
}

1 个答案:

答案 0 :(得分:0)

如果您在添加到列表中时进行检查,则可以删除后期处理的必要性并使其最终成为唯一。如果“b a”已经存在,请不要添加“a b”。