Cod sursa(job #2266440)

Utilizator GarboteialexGarbotei Alex Garboteialex Data 22 octombrie 2018 17:49:44
Problema Problema Damelor Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>

using namespace std;

ifstream fin("damesah.in");
ofstream fout("damesah.out");

int n;
int nrsol;
bool uc[14],udp[40],uds[40];
int sol[14];

void back(int lin) {
    if(lin == n + 1){
        if(!nrsol) {
            for(int i = 1; i <= n; i++) {
                fout << sol[i] << " ";
            }
            fout << '\n';
        }
        nrsol++;
        
        return;
    } else {
        for(int col = 1; col <= n; col++) {
            int x = col - lin + n - 1;
            int y = lin + col;
            if(!uc[col] && !udp[x] && !uds[y]) {
                sol[lin] = col;
                uc[col] = udp[x] = uds[y] = true;
                back(lin + 1);
                uc[col] = udp[x] = uds[y] = false;
            }
            
        }
    }
}

int main(){
    fin >> n;
    
    back(1);
    
    fout << nrsol;
}