Cod sursa(job #2266428)

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

using namespace std;

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

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

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

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