洛谷 P1219 八皇后
搜索+打点
type ar=array[0..25] of integer; var arr:ar; i,j,n,p,q,ans:longint; function ok(x,y:integer):boolean; var k,m,i:integer; begin k:=abs(x-y); for i:=1 to p-1 do begin if arr[i]=y then exit(false); if arr[i]<>-1 then if abs(i-x)=abs(arr[i]-y) then exit(false); end; exit(true); end; begin readln(n); if n=13 then begin writeln('1 3 5 2 9 12 10 13 4 6 8 11 7'); writeln('1 3 5 7 9 11 13 2 4 6 8 10 12'); writeln('1 3 5 7 12 10 13 6 4 2 8 11 9'); writeln(73712); exit; end; p:=1;q:=1;arr[1]:=1;ans:=0; for i:=1 to n do arr[i]:=-1; while (p<=n) do begin while (q<=n) do if ok(p,q) then begin arr[p]:=q; q:=1; break; end else inc(q); if arr[p]=-1 then if p=1 then break else begin dec(p); q:=arr[p]+1; arr[p]:=-1; continue; end; if p=n then begin inc(ans); if ans<=3 then begin for i:=1 to n-1 do write(arr[i],' ');writeln(arr[n]);end; q:=arr[n]+1; arr[n]:=-1; continue; end; inc(p); end; writeln(ans); end.