什么会导致此函数中的堆栈溢出?

我正在写一个休闲扫雷艇,想实现一种方法来跟踪现场空单元格,所以我写了这个算法:

//bigger array was taken to prevent out of range,when init mines and numbers
 /*creating mines in 1 to FIELD_NUM range(0 -non-active field,1-active field)
 * {
 *  0   0   0   0   0   0
 *  0   1   1   1   1   0
 *  0   1   1   1   1   0
 *  0   1   1   1   1   0
 *  0   1   1   1   1   0
 *  0   0   0   0   0   0
 * }
 */
//view is a display vect,x and y are mouse interp. coord.

void MinerField::OpenCell(vector<vector<int>>& view, int x, int y)
{   
    if (gridLogic[x][y] == 9)
    {
        for (int i = 1; i <= FIELD_NUM; i++)
            for (int j = 1; j <= FIELD_NUM; j++)
            {
                view[i][j] = gridLogic[i][j];
            }
    }
    else
    {
        if (gridLogic[x][y] == 0)
            OpenVoidCells(view, x, y);
        else
            view[x][y] = gridLogic[x][y];
    }
}

而第二个函数,这会导致堆栈溢出:

void MinerField::OpenVoidCells(vector<vector<int>>& view, int x, int y)
{
    if (x >= (FIELD_NUM) || y >= (FIELD_NUM))//check out of range
        return;
    if (gridLogic[x][y] == 10 || gridLogic[x][y]==11 ||gridLogic[x][y]==-1)
        return;
    if ((gridLogic[x][y] <= 8) && (gridLogic[x][y] >= 1))
    {
        view[x][y] = gridLogic[x][y];
        return;
    }
    view[x][y] = gridLogic[x][y];
    OpenVoidCells(view,x + 1, y); //North;
    OpenVoidCells(view,x - 1, y); //South
    OpenVoidCells(view,x, y + 1); //East
    OpenVoidCells(view, x, y - 1); //West

    OpenVoidCells(view, x - 1, y - 1); //South-West
    OpenVoidCells(view, x + 1, y + 1);  //North-East
    OpenVoidCells(view, x - 1, y + 1);  //South-East
    OpenVoidCells(view, x + 1, y - 1);  //North-West
}

gridLogic向量是MinerField局部的并且与 具有相同的大小view。运行失败并显示FIELD_NUM=10. 什么会导致堆栈溢出?

回答

OpenVoidCells没有任何东西可以阻止一遍又一遍地访问同一个广场。它会向北、向南、向北、向南、向北、向南走……永远,直到你用完堆栈。您需要跟踪访问过的方块并避免重新检查它们。


以上是什么会导致此函数中的堆栈溢出?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>