Wednesday, 29 April 2015

UVA 11195 Another n-Queen Problem

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

string board[16];
int n, cnt;

void nq(int row, int col, int lft, int rgt)
{
    if(row==n)
    {
        cnt++;
        return;
    }

    int all = col | lft | rgt;

    for(int i=0;i<n;i++)
    {
        if(!((1<<i) & all) && board[row][i]=='.')
            nq(row+1, col | (1<<i), (lft | (1<<i))<<1, (rgt | (1<<i))>>1 );
    }
return;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1;
    string s;
    while(cin>>n && n)
    {
        for(int i=0;i<n;i++)
            cin>>board[i];
        cnt=0;
        nq(0, 0, 0, 0);
        cout<<"Case "<<kk++<<": "<<cnt<<"\n";
    }
return 0;
}

No comments:

Post a Comment