我的c ++程序运行,但说" 8queens.exe已停止工作"

时间:2014-05-10 23:52:26

标签: 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;
        }
    }
}

1 个答案:

答案 0 :(得分:3)

我看到的第一件事是可能的越界访问:

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的测试。

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