Melodicsoul

Life is a chain of moments of enjoyment

转载 N个皇后算法收藏

新一篇: 手工检测可否注入  | 旧一篇: ASP.NET保持用户状态的九种选择

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


long sum 
= 0, upperlim = 1;

void test(long row
, long ld, long rd)
{

if (row != upperlim)
{
long 
pos = upperlim & ~(row | ld | rd);
while (pos)
{
long p 
= pos & -pos;

pos -= p;
test(row 
+ p, (ld + p) << 1, (rd + p) >> 1);
}
else
sum
++;
}

int main(int argc, char *argv[])
{
time_t tm;
int n = 16;

if (argc != 1)
= atoi(argv[1]);
tm 
= time(0);
if ((n < 1|| (n > 32))
{
printf(" 只能计算1-32之间 ");
exit(-1);
}
printf("%d 皇后 ", n);
upperlim 
= (upperlim << n) - 1;

test(
0, 0, 0);
printf("共有%ld种排列, 计算时间%d秒  ", sum, (int) (time(0- tm));
}
 具体参见
http://community.csdn.net/Expert/TopicView3.asp?id=4709025

发表于 @ 2006年11月02日 15:03:00|评论(loading...)|编辑

新一篇: 手工检测可否注入  | 旧一篇: ASP.NET保持用户状态的九种选择

评论:没有评论。

发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © 旋律游魂