Cod sursa(job #2617865)

Utilizator matthriscuMatei Hriscu matthriscu Data 23 mai 2020 09:46:48
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>
 
int abs(int x) {
    return x < 0 ? -x : x;
}
 
int main() {
    int n, k = 1, st[15], i, cnt = 0;
    bool as, ev;
    std::ifstream fin("damesah.in");
    std::ofstream fout("damesah.out");
    fin >> n;
    while(k) {
        do {
            as = 0;
            if(st[k] < n) {
                ++st[k];
                as = 1;
            }
            if(as) {
                ev = 1;
                for(i = 1; i < k; ++i)
                    if(k - i == abs(st[k] - st[i]) || st[i] == st[k])
                        ev = 0;
            }
        } while(as && !ev);
        if(as)
            if(k == n) {
                if(cnt == 0)
                    for(i = 1; i <= n; ++i)
                        fout << st[i] << ' ';
                ++cnt;
         	}            		
            else
                st[++k] = 0;
        else
            --k;        
    }
    fout << '\n' << cnt << '\n';
}