Untitled

mail@pastecode.io avatarunknown
plain_text
2 months ago
957 B
0
Indexable
Never
#include <bits/stdc++.h> 
using namespace std;

int x[200];
int solution = 0;
int N = 5;

bool place(int k, int i)
{
    for (int j = 1; j <= k - 1; j++)
    {
        if (x[j] == i || abs(x[j] - i) == abs(j - k))
            return false;
    }

    return true;
}

void display(int n)
{
    solution++;
    cout << "Solution no: " << solution << endl;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            if (x[i] == j)
                cout << " Q |";
            else
                cout << "   |";
        }
        cout << endl;
    }
    cout << endl;
}


void NQueen(int k, int n)
{
    for (int i = 1; i <= n; i++)
    {
        if (place(k, i))
        {
            x[k] = i;
            if (k == n)
            {
                display(n);
            }
            else
            {
                NQueen(k + 1, n);
            }
        }
    }
}

int main()
{
    NQueen(1, N);

    return 0;
}