col[i]:=false; {列已放标志} if dep=max then output
else try(dep+1); {递归下一层} a[dep]:=0; {取走皇后,回溯} b[i+dep]:=true; {恢复标志数组} c[dep-i]:=true; col[i]:=true; end; end; begin
for i:=1 to max do begin a[i]:=0;col[i]:=true;end; for i:=2 to 2*max do b[i]:=true;
for i:=-(max-1) to max-1 do c[i]:=true; total:=0; try(1);
writeln('total:',total); end.
【测试数据】 n=8 八皇后问题 total:92
对于N皇后: ┏━━━┯━━┯━━┯━━┯━━┯━━┯━━┯━━┓ ┃皇后 N│ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 ┃ ┠───┼──┼──┼──┼──┼──┼──┼──┨ ┃方案数│ 2 │ 10 │ 4 │ 40 │ 92 │352 │724 ┃ ┗━━━┷━━┷━━┷━━┷━━┷━━┷━━┷━━┛
- 41 -