0

此代码尝试解决 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;
        }
    }
}
4

1 回答 1

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>=1before进行测试Chess[x-1][y-1]==1

但即使这样,整个代码部分看起来也很可疑。如果有更多越界访问,我不会感到惊讶。

于 2014-05-10T23:57:43.963 回答