主题:[求助]n皇后
n到13我的算法就来不及了,谁帮帮我!
var
s:array[1..14]of integer;
a,b,c:array[-14..28]of boolean;
i,j,k,l,m,n,sum:integer;
procedure serch(m:integer);
var
i:integer;
begin
if m=n+1 then begin inc(sum);
if sum<=3 then begin write(s[1]);for i:=2 to n do write(' ',s[i]);
writeln; end;
exit;
end;
for i:=1 to n do
if (a[i])and(b[m-i])and(c[m+i]) then begin
a[i]:=false;b[m-i]:=false;c[m+i]:=false;
s[m]:=i;
serch(m+1);
a[i]:=true;b[m-i]:=true;c[m+i]:=true;
end;
end;
begin
assign(input,'checker.in');reset(input);
assign(output,'checker.out');rewrite(output);
readln(n);sum:=0;
for i:=-14 to 28 do
begin a[i]:=true;b[i]:=true;c[i]:=true; end;
serch(1);
writeln(sum);
close(input);close(output);
end.
var
s:array[1..14]of integer;
a,b,c:array[-14..28]of boolean;
i,j,k,l,m,n,sum:integer;
procedure serch(m:integer);
var
i:integer;
begin
if m=n+1 then begin inc(sum);
if sum<=3 then begin write(s[1]);for i:=2 to n do write(' ',s[i]);
writeln; end;
exit;
end;
for i:=1 to n do
if (a[i])and(b[m-i])and(c[m+i]) then begin
a[i]:=false;b[m-i]:=false;c[m+i]:=false;
s[m]:=i;
serch(m+1);
a[i]:=true;b[m-i]:=true;c[m+i]:=true;
end;
end;
begin
assign(input,'checker.in');reset(input);
assign(output,'checker.out');rewrite(output);
readln(n);sum:=0;
for i:=-14 to 28 do
begin a[i]:=true;b[i]:=true;c[i]:=true; end;
serch(1);
writeln(sum);
close(input);close(output);
end.