USACO Training Section1.5 Checker Challenge

经典的N皇后问题,要求一定的效率,干脆用了效率最高的位运算方式。只是想起了老顾就是N皇后发家的,Transaction上发的1秒解决百万皇后问题最后弄了个973稀里糊涂收场,连带我也下了水。往事不堪回首。如果,没有如果,呵呵。谨记!

原题链接:
http://ace.delos.com/usacoprob2?a=lRBMswb6NVi&S=checker

/*
ID: blackco3
TASK: checker
LANG: C++
*/
#include <fstream>
#include <stdlib.h>
#include <memory.h>

using namespace std;
#define _max_ 13
long total=0, upper=1, n_size; 
long fsol[3][_max_], trace[_max_] ;
void place(int row, long col_map, long r_diag, long l_diag)
{ 
	if( row==n_size ) {
		if( total<3 )
			memcpy( fsol[total], trace, sizeof(trace) );
		total++; 
		return ;
	}
	long poss = upper & ~(col_map | l_diag | r_diag); 
	while (poss) { 
		long check = poss & -poss;
		poss -= check;
		if( total<3 )
			trace[row] = check ;
		place(row+1, col_map+check, (r_diag+check)<<1, (l_diag+check)>>1); 
	}
} 
int main() {
    ofstream fout ("checker.out");
    ifstream fin ("checker.in");
	fin >> n_size ; 
	upper=(upper<<n_size) - 1;
	place(0, 0, 0, 0); 
	for( int i=0; i<3; i++ ){
		for( int row=0; row<n_size; row++ ){
			int check_bit=fsol[i][row], pos=0 ;
			while( !(check_bit&1) )
				pos++, check_bit >>= 1;
			if( row )
				fout << " " ;
			fout << pos+1 ; 
		}
		fout << endl ;
	}
	fout << total << endl; 
	return 0;
} 

你可能感兴趣的:(C++,c,C#)