USACO Training Section1.5 Checker Challenge

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

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


/*
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值