Question

this code attempts to solve the 4 queens problem, placing 4 queens on a 4*4 chessboard without any of them being able to capture eachother

#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;
        }
    }
}
Was it helpful?

Solution

The first thing I see is a probable out-of-bounds access:

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

What if the value of x is 0? You are accessing Chess[-1][y], which is out of bounds. Your if statement does not stop this, even with the x>=1 condition.

The if will first test the Chess[x-1][y-1]==1 condition. If you want this to not happen, place the test for x>=1 before Chess[x-1][y-1]==1.

But even with this, that entire section of code looks suspicious. I wouldn't be surprised if there were more out-of-bounds accesses.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top