Cod sursa(job #2360570)

Utilizator SemetgTemes George Semetg Data 1 martie 2019 22:17:58
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>
using namespace std;

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

const int N_MAX = 15;

int N;
int poz[N_MAX];
bool hasC[N_MAX], hasP[2 * N_MAX], hasS[2 * N_MAX];
int sol;

inline int hashP(int i, int j) { return N + i - j; }
inline int hashS(int i, int j) { return i + j; }

void place(int line) {
    if (line == N + 1) {
        ++sol;
        
        if (sol == 1) {
            for (int i = 1; i <= N; ++i)
                out << poz[i] << ' ';
            out << '\n';
        }
        
        return;
    }
    
    for (int i = 1; i <= N; ++i)
        if (!hasC[i] && !hasP[hashP(line, i)] && !hasS[hashS(line, i)]) {
            hasC[i] = hasP[hashP(line, i)] = hasS[hashS(line, i)] = true;
            poz[line] = i;
            
            place(line + 1);
            
            hasC[i] = hasP[hashP(line, i)] = hasS[hashS(line, i)] = false;
        }
}

int main() {
    in >> N;
    place(1);
    out << sol;
}