为什么在进一步的暴力迭代中会出现分段错误

时间:2015-09-27 17:00:44

标签: c++ segmentation-fault

我想再次解决一个难题!这是链接:http://www.puzzleup.com/2015/puzzle/?9

我的方法需要矢量。因为我使用这种方法:

enter image description here

我重视所有的角落,并且我通过结束(第15个角落)跟踪每次迭代。我从1开始,记录我在可能性2d数组中经历的所有角落。

#include <iostream>
#include <vector>
#include <cmath>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

/*
possibilities will keep the possible combinations, routes like {1,4}{4,8}{8,7} etc till it arrives to 15
*/

vector< vector<int> > possibilities;

int cubes[15][6] = { {2,3,5,0,0,0}, {1,4,6,0,0,0}, {1,4,7,0,0,0}, {2,3,8,0,0,0}, {1,6,7,0,0,0}, {2,5,8,0,0,0}, {3,5,8,0,0,0}, {4,6,7,9,10,13}, {8,11,12,0,0,0}, {8,11,14,0,0,0}, {9,10,15,0,0,0}, {9,13,15,0,0,0}, {8,12,14,0,0,0}, {10,13,15,0,0,0}, {11,12,14,0,0,0} };

int counterSet, i, j, temp, counter, sizeOfVec ;

int routeCheck(int a, int b)
{
    //
    if(possibilities.size()!=0)
    {
        //
        sizeOfVec = 0 ;
        for(i=0; i<possibilities.size(); i++)
        {
            //
            sizeOfVec += possibilities[i].size();
        }
        if(sizeOfVec!=0)
        {
            //
            for(i=0; i< sizeOfVec; i++)
            {
                //
                if((possibilities[i][0] == a && possibilities[i][1] == b) || (possibilities[i][0] == b && possibilities[i][1] == a))
                {
                    //
                    return 0;
                }
            }
            return 1;
        }
        else
        {
            //
            return 1;
        }

    }
    else
    {
        //
        return 1;
    }

}

int routeKeeper(int a, int b)
{
    //
    if(routeCheck(a,b) == 0)
    {
        //
        return 0;
    }
    else if(routeCheck(a,b) == 1)
    {
        //
        possibilities.push_back({a,b});
    }
}

void createRouteMap(int start, int end)
{
    //
    temp = j;

    for(j=0; j<6; j++)
    {
        //
        cout << j << endl;
        if(cubes[start-1][j]==0)
        {
            //

        }
        else if(cubes[start-1][j]==end)   // if it is destination
        {
            //
            counter+=1;
            cout << "counter is: " <<counter << endl;
        }
        else if(routeCheck(start, cubes[start-1][j])==1)
        {
            //
            routeKeeper(start, cubes[start-1][j]);
            cout << "vector size is: " <<sizeOfVec << endl;
            createRouteMap(cubes[start-1][j], end);

        }
    }

    j=temp;
    possibilities.erase(possibilities.end());
}


int main(int argc, char** argv) {

    counter = 0;
    createRouteMap(1, 15);
    cout<< counter << endl;

    system("pause");
    return 0;
}

我的所有代码都在共享之上。我使用暴力来计算到第15个角落的所有可能方式。但是,尽管没有编译错误,终端也会不断崩溃。当我在调试模式下执行(我使用Dev-C ++ 5.11)时,我收到此错误:

程序接收信号SIGSEGV:分段故障

当然,我研究过并发现了与我的问题类似的问题。但是大多数建议者指出这是关于程序试图访问不属于它的内存。但疯狂的是,我实际上访问所有内存,你可以在任何编辑器中尝试,它运行前3次迭代并使用所有的函数和变量。输出是这样的:

enter image description here

为什么会这样?我会劝告任何建议和方向。感谢。

1 个答案:

答案 0 :(得分:1)

这段代码看起来很奇怪。

        sizeOfVec = 0 ;
        for(i=0; i<possibilities.size(); i++)
        {
            //
            sizeOfVec += possibilities[i].size();
        }
        if(sizeOfVec!=0)
        {
            //
            for(i=0; i< sizeOfVec; i++)
            {
                //
                if((possibilities[i][0]

sizeOfVec用于索引向量possibilities,但它会计算possibilities个元素的向量的总大小。 许多人建议您使用调试器。