2011年6月14日星期二

2009年09月17日 09:10 阅读(57) 评论(2) 分类:program 《Nqueen》

N皇后终极位运算版(显然是从M67那里看的)外加自己的一点小优化14皇后0.35秒出解好帅

晒图

[一张图片,用时0.52秒]

晒program

procedure test(row,ld,rd:longint);
var pos,p:longint;
begin
  if row<>upperlim then
  begin
     pos:=upperlim and not (row or ld or rd);
     while pos<>0 do
     begin
        p:=pos and -pos;
        pos:=pos-p;
        test(row+p,(ld+p)shl 1,(rd+p)shr 1);
     end;
  end
  else inc(sum);
end;

begin
 readln(n);
 sum:=0;
 upperlim:=(1 shl n)-1;
 for i:=1 to n shr 1 do
   begin
    j:=1 shl (i-1);
    test(j,j shl 1,j shr 1);
   end;
 sum:=sum shl 1;
 if n and 1=1 then
   begin
    j:=1 shl (n shr 1);
    test(j,j shl 1,j shr 1);
   end;
 writeln(sum);
end.        


不懂的点这里呀点这里
http://www.matrix67.com/blog/archives/266

没有评论:

发表评论