Cod sursa(job #1652553)

Utilizator MarianMMorosac George Marian MarianM Data 15 martie 2016 01:58:15
Problema Problema Damelor Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <utility>
#include <queue>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <sstream>

using namespace std;

#define DMAX 13
#define MOD 1999999973
#define ll long long
#define ull unsigned long long

int n, countSol;
int used[DMAX];
bool lin[DMAX], col[DMAX], diag[2][DMAX], written = false;

void writing(){
    for (int i = 0; i < n; i++){
        cout << used[i] + 1 <<  " ";
    }
    cout << '\n';
    written = true;
}

bool conflict(int x, int y){
    return lin[x] || col[y] || diag[0][x - y + n] || diag[1][x + y];
}

void setUsed(int x, int y, bool usage){
    lin[x] = col[y] = diag[0][x - y + n] = diag[1][x + y] = usage;
}

void bkt(int k){
    if (k == n){
        if(!written)
            writing();
        countSol++;
    } else {
        for (int j = 0; j < n; j++) {
            if (!conflict(k, j)) {
                setUsed(k, j, true);
                used[k] = j;
                bkt(k + 1);
                used[k] = -1;
                setUsed(k, j, false);
            }
        }
    }
}

int main() {
    freopen("damesah.in", "r", stdin);
    freopen("damesah.out", "w", stdout);

//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    cin >> n;
    for (int i = 0; i < n; i++) used[i] = -1;

    bkt(0);

    cout << countSol;

    return 0;
}