Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.7 kB
12
Indexable
Never
#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORD(i, a, b) for(int i = a; i >= b; i--)
#define REP(i, b) for(int i = 0; i < b; i++)
#define PER(i, b) for(int i = b - 1; i >= 0; i--)
#define fi first
#define se second
#ifdef JASPER2
#include "debug.h"
#else
#define debug(...) 166
#endif

using pii = pair < int, int >;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const int N = 1e2 + 5;

int n;
int a[5];
// Dat quan hau thu i, hang 1-->(i - 1) da duoc dat, dat quan hau o hang thu i
bool col[N], d1[N], d2[N], queen[N][N];
void solve(int i) {
    // tim cot de dat
    for (int j = 1; j <= n; ++j) {
        if (!col[j] && !d1[i - j + n] && !d2[i + j]) {
            // Dat tai o (i, j)
            // DUong cheo chinh : i - j + n
            // Duong cheo phu : i + j - 1;
            col[j] = d1[i - j + n] = d2[i + j] = 1;
            // vi dat hau la o(i, j)
            queen[i][j] = 1;

            if (i == n) {
                for (int x = 1; x <= n; ++x) {
                    for (int y = 1; y <= n; ++y) {
                        cout << (queen[x][y]? 'Q' : '.');
                    }
                    cout << "\n";
                }
                cout << "\n";
            }
            else 
                solve(i + 1);

            queen[i][j] = 0;
            col[j] = d1[i - j + n] = d2[i + j] = 0;
        }
    }
} 
void run_case() {
    cin >> n;
    solve(1);
}

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    #ifdef JASPER2
        freopen("in1", "r", stdin);
    #endif

    int Test = 1;
    //cin >> Test;
    for (int test = 1; test <= Test; test++){

        run_case();
    }
}