Cod sursa(job #1391957)

Utilizator daniel.grosuDaniel Grosu daniel.grosu Data 18 martie 2015 12:08:46
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>
#include <bitset>
#include <cstdlib>

using namespace std;

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

// Dame bitwise.

int sol[100];

int n,s,line=1;
int done;

int dame(int ld, int col, int rd)
{
    if(col == done){
        s++;
    }else{
        int poss = ~(ld|col|rd);
        while(poss & done)
        {
            int bit = poss & -poss;
            int c=bit;
            if(!s){
                int i=0;
                while(c)
                    c>>=1, ++i;
                sol[line]=i;
                line++;
            }
                
            poss-=bit;
            dame((ld|bit)>>1, col|bit, (rd|bit)<<1);
        }
    }
    line--;
}

int main(){
    fin>>n;
    done=(1<<n);
    done-=1;
    dame(0,0,0);
    
    for(int i=1; i<=n; ++i)
        fout<<sol[i]<<" ";
    fout<<"\n";
    fout<<s;
    return 0;
}