c++ - 我的 C++ 程序运行,但说 "8queens.exe has stopped working"

标签 c++ recursive-backtracking

此代码试图解决 4 皇后问题,将 4 个皇后放在一个 4*4 的棋盘上,但其中任何一个都无法捕获对方

#include <iostream>
using namespace std;

int Place(int Chess[][4], int collumn, int i);
bool Check(int Chess[][4], int collumn, int i);
int findrow(int Chess[][4], int collumn);
const int size = 3;
int main()
{
    int Chess[4][4];
    int collumn;
    int i = 0;
    collumn = 0;

    for(int s = 0; s < 4; s++)
    {
        for(int j = 0; j < 4; j ++)
        {
            Chess[s][j] = 0;

        }
    }
    //Chess[0][0] = 1;
    //Chess[3][3] = 1;
    //if(Check(Chess, 3, 3) == false)

    Place(Chess, collumn, i);
    for(int z = 0; z < 4; z++)
    {
        for(int a = 0; a < 4; a++)
        {

            if(Chess[z][a] == 1)

                cout<<"Row: "<<z<<"Collumn: "<<a<<"."<<endl;
        }
        cout<<endl;
    }
    system("pause");
    return 0;

}


int Place(int Chess[][4], int collumn, int i)
{
    if(collumn > size)
        return 0;
    while(i <= size)
    {
        if(Check(Chess, collumn, i) == true)
        {
            //cout<<"hi"<<endl;
            Chess[collumn][i] = 1;
            return(Place(Chess, (collumn + 1), i));
        }
        i ++;
    }
    if(i>= size)
    {
        //cout<<"hilo"<<endl;
        return Place(Chess, collumn-1, findrow(Chess, collumn-1));
    }
}

bool Check(int Chess[][4], int collumn, int i)//checks to see if it can be captured
{// very inneficitnt
    int x = collumn;// this is so we can now work in terms of x and y
    int y = i;

    bool found = true;


    // checks all the diagonal captures
    if(Chess[x -1 ][y -1]== 1&& x>=1 && y >=1 )
        found = false;
    if(Chess[x -2 ][y - 2]== 1&& x>=2 && y>=2 )
        found =  false;
    if(Chess[x - 3][y - 3]== 1 && x>=3 && y>=3 )
        found =  false;
    if(Chess[x + 1][y - 1] == 1&& x<=2 && y>=1 )
        found = false;
    if(Chess[x + 2][y -2]  == 1&& x<=1 && y>=2)
        found = false;
    if(Chess[x + 3][y - 3] == 1 && x<=0 && y>=3)
        found =  false;
    if(Chess[x + 1][y + 1] == 1 && x<=2 && y<=2)
        found = false;
    if(Chess[x + 2][y + 2]  == 1&& x<=1 && y<=1)
        found = false;
    if(Chess[x + 3][y + 3] == 1 && x<=0 && y<=0 )
        found =  false;
    if(Chess[x -1 ][y + 1]== 1 && x>=1 && y<=2 )
        found =  false;
    if(Chess[x - 2][y + 2] == 1&& x>=2 && y<=1 )
        found = false;
    if(Chess[x - 3][y + 3]  == 1&& x>=3 && y<=0)
        found = false;

    //checks all the horizontal captures.  We don't need to check for vertical captures
    if(Chess[x + 1][y] == 1 && x<=2)
        found = false;
    if(Chess[x + 2][y] == 1&& x<=1 )
        found = false;
    if(Chess[x+3][y] == 1 && x<=0)
        found = false;
    if(Chess[x -1 ][y]  == 1&& x>=1)
        found =  false;
    if(Chess[x-2][y] == 1&& x>=2 )
        found = false;
    if(Chess[x-3][y]  == 1 && x>=3)
        found =  false;

    if(found == false)
        return false;

    if(found == true)
        return true;
}

int findrow(int Chess[][4], int collumn)
{
    for(int z = 0; z < 4; z++)
    {
        if(Chess[collumn][z] == 1)
        {
            Chess[collumn][z] = 0;
            return z;
        }
    }
}

最佳答案

我首先看到的是可能的越界访问:

if(Chess[x -1 ][y -1]== 1&& x>=1 && y >=1 )

如果 x 的值为 0 怎么办?您正在访问超出范围的 Chess[-1][y]。您的 if 语句不会停止此操作,即使在 x>=1 条件下也是如此。

if 将首先测试 Chess[x-1][y-1]==1 条件。如果您不希望发生这种情况,请将 x>=1 的测试放在 Chess[x-1][y-1]==1 之前。

但即便如此,整段代码看起来也很可疑。如果有更多的越界访问,我不会感到惊讶。

关于c++ - 我的 C++ 程序运行,但说 "8queens.exe has stopped working",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23587535/

相关文章:

c++ - 模板化类 const 限定符构造函数

c++ - 用于创建新作用域的最佳 C++ 宏

c++ - 如何进行特定的位操作?

c++ - 如何检查boost消息队列是否存在

r - 构建一个没有重复但固定部分输入的随机矩阵

java - 骑士之旅 - java 回溯,出界

python - 在网格中回溯

c++ - 从 PNG 加载 Gdiplus 的 HICON 质量低

go - Go中的数独递归回溯

python - 如何收集递归回溯的结果?