#include <iostream>
using namespace std;
const int NUMBER_OF_QUEENS = 8;
int queens[NUMBER_OF_QUEENS] = {-1, -1, -1, -1, -1, -1, -1, -1};
bool isValid(int row, int column)
{
for (int i = 1; i <= row; i++)
if (queens[row - i] == column
|| queens[row - i] == column - i
|| queens[row - i] == column + i)
return false;
return true;
}
void printResult()
{
cout << "\n---------------------------------\n";
for (int row = 0; row < NUMBER_OF_QUEENS; row++)
{
for (int column = 0; column < NUMBER_OF_QUEENS; column++)
printf(column == queens[row] ? "| Q " : "| ");
cout << "|\n---------------------------------\n";
}
}
int findPosition(int k)
{
int start = queens[k] + 1;
for (int j = start; j < NUMBER_OF_QUEENS; j++)
{
if (isValid(k, j))
return j;
}
return -1;
}
bool search()
{
int k = 0;
while (k >= 0 && k < NUMBER_OF_QUEENS)
{
int j = findPosition(k);
if (j < 0)
{
queens[k] = -1;
k--;
}
else
{
queens[k] = j;
k++;
}
}
if (k == -1)
return false;
else
return true;
}
int main()
{
search();
printResult();
return 0;
}