Cod sursa(job #2373680)

Utilizator Alex18maiAlex Enache Alex18mai Data 7 martie 2019 14:48:33
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>

using namespace std;

ifstream cin("damesah.in");
ofstream cout("damesah.out");

int perm[15];
bool col[15];
bool d1[35];
bool d2[35];

int n , s=0;

void backt(int lv){
    if (lv > n){
        s++;
        if (s==1){
            for (int i=1; i<=n; i++){
                cout<<perm[i]<<" ";
            }
            cout<<'\n';
        }
        return;
    }
    for (int i=1; i<=n; i++){
        if (!col[i] && !d1[lv-i+n] && !d2[i+lv]){
            col[i] = true;
            d1[lv-i+n]=true;
            d2[i+lv]=true;
            perm[lv] = i;

            backt(lv+1);

            col[i] = false;
            d1[lv-i+n]=false;
            d2[i+lv]=false;
        }
    }
}

int main() {
    cin>>n;
    backt(1);
    cout<<s;
    return 0;
}