Cod sursa(job #1601804)

Utilizator Burbon13Burbon13 Burbon13 Data 16 februarie 2016 11:34:52
Problema 12-Perm Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>
#include <iostream>
#include <bitset>
#define c cout
using namespace std;

ifstream f("12perm.in");
ofstream o("12perm.out");

int n,total,sol[100];
bitset <100> viz;

int abs(int val) {
    return val > 0 ? val : -val;
}

bool verif() {
    for(int i = 1; i < n; ++i)
        if(abs(sol[i]-sol[i-1]) >= 3)
            return 0;
    return 1;
}

void afish() {
    for(int i = 0; i < n; ++i)
        printf("%d ", sol[i]);
    printf("\n");
}

void Back(int pos) {
    if(pos == n) {
        if(verif()) {
            //afish();
            ++ total;
        }
        return;
    }
    for(int i = 1; i <= n; ++i)
        if(not viz[i] && (pos == 0 || abs(i-sol[pos-1]) < 3)) {
            viz[i] = true;
            sol[pos] = i;
            Back(pos+1);
            viz[i] = false;
        }
}

int main() {
    f >> n;
    Back(0);
    o << total;
    return 0;
}