Пример #1
0
void PlaceQueen(int n, int row, int* columns, Solutions* solutions, int* returnSize)
{
    if (row == n)
    {
        pushBack(solutions, columns);
        *returnSize = solutions->size;
    }
    else
    {
        for (int col = 0; col != n; ++col)
        {
            if (CheckIfValid(columns, row, col))
            {
                columns[row] = col;
                PlaceQueen(n, row + 1, columns, solutions, returnSize);
            }
        }
    }
}
Пример #2
0
int PlaceQueens(int** chessboard, int size, int level)
{
	count++;
	int Q_placed = 0; /*
	Q_placed is a flag reflecting that whether a Queen
	is placed on the current level or not.
	This value is returned to preceeding levels.
	*/
	int QNextLevel; /*
	QNextLevel is a flag reflecting that whether a Queen
	is placed on the next level or not.
	During recurssion this Flag will allow recurssion
	 */
	int flag;    /*
	Determines whether a Queen can be placed at current
	position or not.
	*/
	int i;
	for (i = 0; i < size; i++)
	{
		 /*Breaks the loops after all the Queens have been placed*/
		if (Q_placed == 1)
			break;
		 /*Check whether Queen can be placed or not*/
		 flag = CheckKill(chessboard, size, level, i);
		 /*If Queen can be placed*/
		 if (flag == 0)
		 {
			 /*Queen Placed. This will be changed if Next level Queen is
			 not placed succesfully*/
			 Q_placed = 1;
			 /*Queen Placed. */
			 chessboard[level][i] = 1;
			 PlaceQueen(level, i);
			  /*Check for Last level*/
			 if (level == size - 1)
				;
			 else /*Place a next level Queen*/
			 {
				QNextLevel = PlaceQueens(chessboard, size, level + 1);
				/*If next level Queen is not Placed.*/
				if (QNextLevel == 0)
				{
					  /*Mark this level Queen as Not Placed*/
					  Q_placed = 0;
					  chessboard[level][i] = 0;
					  RemoveQueen(level, i);
					  if (i == size - 1)
						HomeQueen(level);
				}
			 }
		 }
		 else if (flag == 1)
		 {
			PlaceQueen(level, i);
			RemoveQueen(level, i);
			if (i == size - 1)
				HomeQueen(level);
		 }
	}
	return Q_placed;
}