@ -3532,7 +3532,7 @@
<!-- Page content -->
< h1 id = "134-n" > 13.4 N 皇后问题< a class = "headerlink" href = "#134-n" title = "Permanent link" > ¶ < / a > < / h1 >
< h1 id = "134-n" > 13.4 n 皇后问题< a class = "headerlink" href = "#134-n" title = "Permanent link" > ¶ < / a > < / h1 >
< div class = "admonition question" >
< p class = "admonition-title" > Question< / p >
< p > 根据国际象棋的规则,皇后可以攻击与同处一行、一列或一条斜线上的棋子。给定 < span class = "arithmatex" > \(n\)< / span > 个皇后和一个 < span class = "arithmatex" > \(n \times n\)< / span > 大小的棋盘,寻找使得所有皇后之间无法相互攻击的摆放方案。< / p >
@ -3575,7 +3575,7 @@
< a id = "__codelineno-0-7" name = "__codelineno-0-7" href = "#__codelineno-0-7" > < / a > < span class = "n" > diags1< / span > < span class = "p" > :< / span > < span class = "nb" > list< / span > < span class = "p" > [< / span > < span class = "nb" > bool< / span > < span class = "p" > ],< / span >
< a id = "__codelineno-0-8" name = "__codelineno-0-8" href = "#__codelineno-0-8" > < / a > < span class = "n" > diags2< / span > < span class = "p" > :< / span > < span class = "nb" > list< / span > < span class = "p" > [< / span > < span class = "nb" > bool< / span > < span class = "p" > ],< / span >
< a id = "__codelineno-0-9" name = "__codelineno-0-9" href = "#__codelineno-0-9" > < / a > < span class = "p" > ):< / span >
< a id = "__codelineno-0-10" name = "__codelineno-0-10" href = "#__codelineno-0-10" > < / a > < span class = "w" > < / span > < span class = "sd" > " " " 回溯算法:N 皇后" " " < / span >
< a id = "__codelineno-0-10" name = "__codelineno-0-10" href = "#__codelineno-0-10" > < / a > < span class = "w" > < / span > < span class = "sd" > " " " 回溯算法:n 皇后" " " < / span >
< a id = "__codelineno-0-11" name = "__codelineno-0-11" href = "#__codelineno-0-11" > < / a > < span class = "c1" > # 当放置完所有行时,记录解< / span >
< a id = "__codelineno-0-12" name = "__codelineno-0-12" href = "#__codelineno-0-12" > < / a > < span class = "k" > if< / span > < span class = "n" > row< / span > < span class = "o" > ==< / span > < span class = "n" > n< / span > < span class = "p" > :< / span >
< a id = "__codelineno-0-13" name = "__codelineno-0-13" href = "#__codelineno-0-13" > < / a > < span class = "n" > res< / span > < span class = "o" > .< / span > < span class = "n" > append< / span > < span class = "p" > ([< / span > < span class = "nb" > list< / span > < span class = "p" > (< / span > < span class = "n" > row< / span > < span class = "p" > )< / span > < span class = "k" > for< / span > < span class = "n" > row< / span > < span class = "ow" > in< / span > < span class = "n" > state< / span > < span class = "p" > ])< / span >
@ -3597,7 +3597,7 @@
< a id = "__codelineno-0-29" name = "__codelineno-0-29" href = "#__codelineno-0-29" > < / a > < span class = "n" > cols< / span > < span class = "p" > [< / span > < span class = "n" > col< / span > < span class = "p" > ]< / span > < span class = "o" > =< / span > < span class = "n" > diags1< / span > < span class = "p" > [< / span > < span class = "n" > diag1< / span > < span class = "p" > ]< / span > < span class = "o" > =< / span > < span class = "n" > diags2< / span > < span class = "p" > [< / span > < span class = "n" > diag2< / span > < span class = "p" > ]< / span > < span class = "o" > =< / span > < span class = "kc" > False< / span >
< a id = "__codelineno-0-30" name = "__codelineno-0-30" href = "#__codelineno-0-30" > < / a >
< a id = "__codelineno-0-31" name = "__codelineno-0-31" href = "#__codelineno-0-31" > < / a > < span class = "k" > def< / span > < span class = "nf" > n_queens< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "p" > :< / span > < span class = "nb" > int< / span > < span class = "p" > )< / span > < span class = "o" > -> < / span > < span class = "nb" > list< / span > < span class = "p" > [< / span > < span class = "nb" > list< / span > < span class = "p" > [< / span > < span class = "nb" > list< / span > < span class = "p" > [< / span > < span class = "nb" > str< / span > < span class = "p" > ]]]:< / span >
< a id = "__codelineno-0-32" name = "__codelineno-0-32" href = "#__codelineno-0-32" > < / a > < span class = "w" > < / span > < span class = "sd" > " " " 求解 N 皇后" " " < / span >
< a id = "__codelineno-0-32" name = "__codelineno-0-32" href = "#__codelineno-0-32" > < / a > < span class = "w" > < / span > < span class = "sd" > " " " 求解 n 皇后" " " < / span >
< a id = "__codelineno-0-33" name = "__codelineno-0-33" href = "#__codelineno-0-33" > < / a > < span class = "c1" > # 初始化 n*n 大小的棋盘,其中 ' Q' 代表皇后,' #' 代表空位< / span >
< a id = "__codelineno-0-34" name = "__codelineno-0-34" href = "#__codelineno-0-34" > < / a > < span class = "n" > state< / span > < span class = "o" > =< / span > < span class = "p" > [[< / span > < span class = "s2" > " #" < / span > < span class = "k" > for< / span > < span class = "n" > _< / span > < span class = "ow" > in< / span > < span class = "nb" > range< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "p" > )]< / span > < span class = "k" > for< / span > < span class = "n" > _< / span > < span class = "ow" > in< / span > < span class = "nb" > range< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "p" > )]< / span >
< a id = "__codelineno-0-35" name = "__codelineno-0-35" href = "#__codelineno-0-35" > < / a > < span class = "n" > cols< / span > < span class = "o" > =< / span > < span class = "p" > [< / span > < span class = "kc" > False< / span > < span class = "p" > ]< / span > < span class = "o" > *< / span > < span class = "n" > n< / span > < span class = "c1" > # 记录列是否有皇后< / span >
@ -3610,7 +3610,7 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > n_queens.cpp< / span > < pre > < span > < / span > < code > < a id = "__codelineno-1-1" name = "__codelineno-1-1" href = "#__codelineno-1-1" > < / a > < span class = "cm" > /* 回溯算法:N 皇后 */< / span >
< div class = "highlight" > < span class = "filename" > n_queens.cpp< / span > < pre > < span > < / span > < code > < a id = "__codelineno-1-1" name = "__codelineno-1-1" href = "#__codelineno-1-1" > < / a > < span class = "cm" > /* 回溯算法:n 皇后 */< / span >
< a id = "__codelineno-1-2" name = "__codelineno-1-2" href = "#__codelineno-1-2" > < / a > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "nf" > backtrack< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > row< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > n< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > string< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "n" > state< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > string< / span > < span class = "o" > > > > < / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "n" > res< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "kt" > bool< / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "n" > cols< / span > < span class = "p" > ,< / span >
< a id = "__codelineno-1-3" name = "__codelineno-1-3" href = "#__codelineno-1-3" > < / a > < span class = "w" > < / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "kt" > bool< / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "n" > diags1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "kt" > bool< / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "n" > diags2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-1-4" name = "__codelineno-1-4" href = "#__codelineno-1-4" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当放置完所有行时,记录解< / span >
@ -3637,7 +3637,7 @@
< a id = "__codelineno-1-25" name = "__codelineno-1-25" href = "#__codelineno-1-25" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-1-26" name = "__codelineno-1-26" href = "#__codelineno-1-26" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-1-27" name = "__codelineno-1-27" href = "#__codelineno-1-27" > < / a >
< a id = "__codelineno-1-28" name = "__codelineno-1-28" href = "#__codelineno-1-28" > < / a > < span class = "cm" > /* 求解 N 皇后 */< / span >
< a id = "__codelineno-1-28" name = "__codelineno-1-28" href = "#__codelineno-1-28" > < / a > < span class = "cm" > /* 求解 n 皇后 */< / span >
< a id = "__codelineno-1-29" name = "__codelineno-1-29" href = "#__codelineno-1-29" > < / a > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > string< / span > < span class = "o" > > > > < / span > < span class = "w" > < / span > < span class = "n" > nQueens< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > n< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-1-30" name = "__codelineno-1-30" href = "#__codelineno-1-30" > < / a > < span class = "w" > < / span > < span class = "c1" > // 初始化 n*n 大小的棋盘,其中 ' Q' 代表皇后,' #' 代表空位< / span >
< a id = "__codelineno-1-31" name = "__codelineno-1-31" href = "#__codelineno-1-31" > < / a > < span class = "w" > < / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > string< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "n" > state< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > vector< / span > < span class = "o" > < < / span > < span class = "n" > string< / span > < span class = "o" > > < / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "s" > " #" < / span > < span class = "p" > ));< / span >
@ -3653,7 +3653,7 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > n_queens.java< / span > < pre > < span > < / span > < code > < a id = "__codelineno-2-1" name = "__codelineno-2-1" href = "#__codelineno-2-1" > < / a > < span class = "cm" > /* 回溯算法:N 皇后 */< / span >
< div class = "highlight" > < span class = "filename" > n_queens.java< / span > < pre > < span > < / span > < code > < a id = "__codelineno-2-1" name = "__codelineno-2-1" href = "#__codelineno-2-1" > < / a > < span class = "cm" > /* 回溯算法:n 皇后 */< / span >
< a id = "__codelineno-2-2" name = "__codelineno-2-2" href = "#__codelineno-2-2" > < / a > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "nf" > backtrack< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > row< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > n< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > String< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "n" > state< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > String< / span > < span class = "o" > > > > < / span > < span class = "w" > < / span > < span class = "n" > res< / span > < span class = "p" > ,< / span >
< a id = "__codelineno-2-3" name = "__codelineno-2-3" href = "#__codelineno-2-3" > < / a > < span class = "w" > < / span > < span class = "kt" > boolean< / span > < span class = "o" > []< / span > < span class = "w" > < / span > < span class = "n" > cols< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > boolean< / span > < span class = "o" > []< / span > < span class = "w" > < / span > < span class = "n" > diags1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > boolean< / span > < span class = "o" > []< / span > < span class = "w" > < / span > < span class = "n" > diags2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-2-4" name = "__codelineno-2-4" href = "#__codelineno-2-4" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当放置完所有行时,记录解< / span >
@ -3684,7 +3684,7 @@
< a id = "__codelineno-2-29" name = "__codelineno-2-29" href = "#__codelineno-2-29" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-2-30" name = "__codelineno-2-30" href = "#__codelineno-2-30" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-2-31" name = "__codelineno-2-31" href = "#__codelineno-2-31" > < / a >
< a id = "__codelineno-2-32" name = "__codelineno-2-32" href = "#__codelineno-2-32" > < / a > < span class = "cm" > /* 求解 N 皇后 */< / span >
< a id = "__codelineno-2-32" name = "__codelineno-2-32" href = "#__codelineno-2-32" > < / a > < span class = "cm" > /* 求解 n 皇后 */< / span >
< a id = "__codelineno-2-33" name = "__codelineno-2-33" href = "#__codelineno-2-33" > < / a > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > String< / span > < span class = "o" > > > > < / span > < span class = "w" > < / span > < span class = "nf" > nQueens< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > n< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-2-34" name = "__codelineno-2-34" href = "#__codelineno-2-34" > < / a > < span class = "w" > < / span > < span class = "c1" > // 初始化 n*n 大小的棋盘,其中 ' Q' 代表皇后,' #' 代表空位< / span >
< a id = "__codelineno-2-35" name = "__codelineno-2-35" href = "#__codelineno-2-35" > < / a > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > String< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "n" > state< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > ArrayList< / span > < span class = "o" > < > < / span > < span class = "p" > ();< / span >
@ -3707,7 +3707,7 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > n_queens.cs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-3-1" name = "__codelineno-3-1" href = "#__codelineno-3-1" > < / a > < span class = "cm" > /* 回溯算法:N 皇后 */< / span >
< div class = "highlight" > < span class = "filename" > n_queens.cs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-3-1" name = "__codelineno-3-1" href = "#__codelineno-3-1" > < / a > < span class = "cm" > /* 回溯算法:n 皇后 */< / span >
< a id = "__codelineno-3-2" name = "__codelineno-3-2" href = "#__codelineno-3-2" > < / a > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > Backtrack< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > row< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > n< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > string< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "n" > state< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > string< / span > < span class = "o" > > > > < / span > < span class = "w" > < / span > < span class = "n" > res< / span > < span class = "p" > ,< / span >
< a id = "__codelineno-3-3" name = "__codelineno-3-3" href = "#__codelineno-3-3" > < / a > < span class = "w" > < / span > < span class = "kt" > bool< / span > < span class = "p" > []< / span > < span class = "w" > < / span > < span class = "n" > cols< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > bool< / span > < span class = "p" > []< / span > < span class = "w" > < / span > < span class = "n" > diags1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > bool< / span > < span class = "p" > []< / span > < span class = "w" > < / span > < span class = "n" > diags2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-3-4" name = "__codelineno-3-4" href = "#__codelineno-3-4" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当放置完所有行时,记录解< / span >
@ -3738,7 +3738,7 @@
< a id = "__codelineno-3-29" name = "__codelineno-3-29" href = "#__codelineno-3-29" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-3-30" name = "__codelineno-3-30" href = "#__codelineno-3-30" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-3-31" name = "__codelineno-3-31" href = "#__codelineno-3-31" > < / a >
< a id = "__codelineno-3-32" name = "__codelineno-3-32" href = "#__codelineno-3-32" > < / a > < span class = "cm" > /* 求解 N 皇后 */< / span >
< a id = "__codelineno-3-32" name = "__codelineno-3-32" href = "#__codelineno-3-32" > < / a > < span class = "cm" > /* 求解 n 皇后 */< / span >
< a id = "__codelineno-3-33" name = "__codelineno-3-33" href = "#__codelineno-3-33" > < / a > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > string< / span > < span class = "o" > > > > < / span > < span class = "w" > < / span > < span class = "n" > NQueens< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > n< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-3-34" name = "__codelineno-3-34" href = "#__codelineno-3-34" > < / a > < span class = "w" > < / span > < span class = "c1" > // 初始化 n*n 大小的棋盘,其中 ' Q' 代表皇后,' #' 代表空位< / span >
< a id = "__codelineno-3-35" name = "__codelineno-3-35" href = "#__codelineno-3-35" > < / a > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > string< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "n" > state< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "p" > [];< / span >
@ -3761,7 +3761,7 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > n_queens.go< / span > < pre > < span > < / span > < code > < a id = "__codelineno-4-1" name = "__codelineno-4-1" href = "#__codelineno-4-1" > < / a > < span class = "cm" > /* 回溯算法:N 皇后 */< / span >
< div class = "highlight" > < span class = "filename" > n_queens.go< / span > < pre > < span > < / span > < code > < a id = "__codelineno-4-1" name = "__codelineno-4-1" href = "#__codelineno-4-1" > < / a > < span class = "cm" > /* 回溯算法:n 皇后 */< / span >
< a id = "__codelineno-4-2" name = "__codelineno-4-2" href = "#__codelineno-4-2" > < / a > < span class = "kd" > func< / span > < span class = "w" > < / span > < span class = "nx" > backtrack< / span > < span class = "p" > (< / span > < span class = "nx" > row< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > state< / span > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "p" > [][]< / span > < span class = "kt" > string< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > res< / span > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "p" > [][][]< / span > < span class = "kt" > string< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > cols< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > diags1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > diags2< / span > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "p" > []< / span > < span class = "kt" > bool< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-4-3" name = "__codelineno-4-3" href = "#__codelineno-4-3" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当放置完所有行时,记录解< / span >
< a id = "__codelineno-4-4" name = "__codelineno-4-4" href = "#__codelineno-4-4" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "nx" > row< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
@ -3792,59 +3792,29 @@
< a id = "__codelineno-4-29" name = "__codelineno-4-29" href = "#__codelineno-4-29" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-4-30" name = "__codelineno-4-30" href = "#__codelineno-4-30" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-4-31" name = "__codelineno-4-31" href = "#__codelineno-4-31" > < / a >
< a id = "__codelineno-4-32" name = "__codelineno-4-32" href = "#__codelineno-4-32" > < / a > < span class = "cm" > /* 回溯算法: N 皇后 */< / span >
< a id = "__codelineno-4-33" name = "__codelineno-4-33" href = "#__codelineno-4-33" > < / a > < span class = "kd" > func< / span > < span class = "w" > < / span > < span class = "nx" > backtrack< / span > < span class = "p" > (< / span > < span class = "nx" > row< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > state< / span > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "p" > [][]< / span > < span class = "kt" > string< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > res< / span > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "p" > [][][]< / span > < span class = "kt" > string< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > cols< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > diags1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > diags2< / span > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "p" > []< / span > < span class = "kt" > bool< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-4-34" name = "__codelineno-4-34" href = "#__codelineno-4-34" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当放置完所有行时,记录解< / span >
< a id = "__codelineno-4-35" name = "__codelineno-4-35" href = "#__codelineno-4-35" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "nx" > row< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-4-36" name = "__codelineno-4-36" href = "#__codelineno-4-36" > < / a > < span class = "w" > < / span > < span class = "nx" > newState< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([][]< / span > < span class = "kt" > string< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nb" > len< / span > < span class = "p" > (< / span > < span class = "o" > *< / span > < span class = "nx" > state< / span > < span class = "p" > ))< / span >
< a id = "__codelineno-4-37" name = "__codelineno-4-37" href = "#__codelineno-4-37" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > _< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "k" > range< / span > < span class = "w" > < / span > < span class = "nx" > newState< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-4-38" name = "__codelineno-4-38" href = "#__codelineno-4-38" > < / a > < span class = "w" > < / span > < span class = "nx" > newState< / span > < span class = "p" > [< / span > < span class = "nx" > i< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([]< / span > < span class = "kt" > string< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nb" > len< / span > < span class = "p" > ((< / span > < span class = "o" > *< / span > < span class = "nx" > state< / span > < span class = "p" > )[< / span > < span class = "mi" > 0< / span > < span class = "p" > ]))< / span >
< a id = "__codelineno-4-39" name = "__codelineno-4-39" href = "#__codelineno-4-39" > < / a > < span class = "w" > < / span > < span class = "nb" > copy< / span > < span class = "p" > (< / span > < span class = "nx" > newState< / span > < span class = "p" > [< / span > < span class = "nx" > i< / span > < span class = "p" > ],< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > *< / span > < span class = "nx" > state< / span > < span class = "p" > )[< / span > < span class = "nx" > i< / span > < span class = "p" > ])< / span >
< a id = "__codelineno-4-40" name = "__codelineno-4-40" href = "#__codelineno-4-40" > < / a >
< a id = "__codelineno-4-41" name = "__codelineno-4-41" href = "#__codelineno-4-41" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-4-42" name = "__codelineno-4-42" href = "#__codelineno-4-42" > < / a > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "nx" > res< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nb" > append< / span > < span class = "p" > (< / span > < span class = "o" > *< / span > < span class = "nx" > res< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > newState< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-43" name = "__codelineno-4-43" href = "#__codelineno-4-43" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-4-44" name = "__codelineno-4-44" href = "#__codelineno-4-44" > < / a > < span class = "w" > < / span > < span class = "c1" > // 遍历所有列< / span >
< a id = "__codelineno-4-45" name = "__codelineno-4-45" href = "#__codelineno-4-45" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "nx" > col< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "mi" > 0< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > col< / span > < span class = "w" > < / span > < span class = "p" > < < / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > col< / span > < span class = "o" > ++< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-4-46" name = "__codelineno-4-46" href = "#__codelineno-4-46" > < / a > < span class = "w" > < / span > < span class = "c1" > // 计算该格子对应的主对角线和次对角线< / span >
< a id = "__codelineno-4-47" name = "__codelineno-4-47" href = "#__codelineno-4-47" > < / a > < span class = "w" > < / span > < span class = "nx" > diag1< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nx" > row< / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "w" > < / span > < span class = "nx" > col< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-4-48" name = "__codelineno-4-48" href = "#__codelineno-4-48" > < / a > < span class = "w" > < / span > < span class = "nx" > diag2< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nx" > row< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "nx" > col< / span >
< a id = "__codelineno-4-49" name = "__codelineno-4-49" href = "#__codelineno-4-49" > < / a > < span class = "w" > < / span > < span class = "c1" > // 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后< / span >
< a id = "__codelineno-4-50" name = "__codelineno-4-50" href = "#__codelineno-4-50" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > !(< / span > < span class = "o" > *< / span > < span class = "nx" > cols< / span > < span class = "p" > )[< / span > < span class = "nx" > col< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > & & < / span > < span class = "w" > < / span > < span class = "p" > !(< / span > < span class = "o" > *< / span > < span class = "nx" > diags1< / span > < span class = "p" > )[< / span > < span class = "nx" > diag1< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > & & < / span > < span class = "w" > < / span > < span class = "p" > !(< / span > < span class = "o" > *< / span > < span class = "nx" > diags2< / span > < span class = "p" > )[< / span > < span class = "nx" > diag2< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-4-51" name = "__codelineno-4-51" href = "#__codelineno-4-51" > < / a > < span class = "w" > < / span > < span class = "c1" > // 尝试:将皇后放置在该格子< / span >
< a id = "__codelineno-4-52" name = "__codelineno-4-52" href = "#__codelineno-4-52" > < / a > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > *< / span > < span class = "nx" > state< / span > < span class = "p" > )[< / span > < span class = "nx" > row< / span > < span class = "p" > ][< / span > < span class = "nx" > col< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "s" > " Q" < / span >
< a id = "__codelineno-4-53" name = "__codelineno-4-53" href = "#__codelineno-4-53" > < / a > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > *< / span > < span class = "nx" > cols< / span > < span class = "p" > )[< / span > < span class = "nx" > col< / span > < span class = "p" > ],< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > *< / span > < span class = "nx" > diags1< / span > < span class = "p" > )[< / span > < span class = "nx" > diag1< / span > < span class = "p" > ],< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > *< / span > < span class = "nx" > diags2< / span > < span class = "p" > )[< / span > < span class = "nx" > diag2< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "kc" > true< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kc" > true< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kc" > true< / span >
< a id = "__codelineno-4-54" name = "__codelineno-4-54" href = "#__codelineno-4-54" > < / a > < span class = "w" > < / span > < span class = "c1" > // 放置下一行< / span >
< a id = "__codelineno-4-55" name = "__codelineno-4-55" href = "#__codelineno-4-55" > < / a > < span class = "w" > < / span > < span class = "nx" > backtrack< / span > < span class = "p" > (< / span > < span class = "nx" > row< / span > < span class = "o" > +< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > state< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > res< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > cols< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > diags1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > diags2< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-56" name = "__codelineno-4-56" href = "#__codelineno-4-56" > < / a > < span class = "w" > < / span > < span class = "c1" > // 回退:将该格子恢复为空位< / span >
< a id = "__codelineno-4-57" name = "__codelineno-4-57" href = "#__codelineno-4-57" > < / a > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > *< / span > < span class = "nx" > state< / span > < span class = "p" > )[< / span > < span class = "nx" > row< / span > < span class = "p" > ][< / span > < span class = "nx" > col< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "s" > " #" < / span >
< a id = "__codelineno-4-58" name = "__codelineno-4-58" href = "#__codelineno-4-58" > < / a > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > *< / span > < span class = "nx" > cols< / span > < span class = "p" > )[< / span > < span class = "nx" > col< / span > < span class = "p" > ],< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > *< / span > < span class = "nx" > diags1< / span > < span class = "p" > )[< / span > < span class = "nx" > diag1< / span > < span class = "p" > ],< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "o" > *< / span > < span class = "nx" > diags2< / span > < span class = "p" > )[< / span > < span class = "nx" > diag2< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "kc" > false< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kc" > false< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kc" > false< / span >
< a id = "__codelineno-4-59" name = "__codelineno-4-59" href = "#__codelineno-4-59" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-4-60" name = "__codelineno-4-60" href = "#__codelineno-4-60" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-4-61" name = "__codelineno-4-61" href = "#__codelineno-4-61" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-4-62" name = "__codelineno-4-62" href = "#__codelineno-4-62" > < / a >
< a id = "__codelineno-4-63" name = "__codelineno-4-63" href = "#__codelineno-4-63" > < / a > < span class = "kd" > func< / span > < span class = "w" > < / span > < span class = "nx" > nQueens< / span > < span class = "p" > (< / span > < span class = "nx" > n< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > [][][]< / span > < span class = "kt" > string< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-4-64" name = "__codelineno-4-64" href = "#__codelineno-4-64" > < / a > < span class = "w" > < / span > < span class = "c1" > // 初始化 n*n 大小的棋盘,其中 ' Q' 代表皇后,' #' 代表空位< / span >
< a id = "__codelineno-4-65" name = "__codelineno-4-65" href = "#__codelineno-4-65" > < / a > < span class = "w" > < / span > < span class = "nx" > state< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([][]< / span > < span class = "kt" > string< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-66" name = "__codelineno-4-66" href = "#__codelineno-4-66" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "mi" > 0< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "p" > < < / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "o" > ++< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-4-67" name = "__codelineno-4-67" href = "#__codelineno-4-67" > < / a > < span class = "w" > < / span > < span class = "nx" > row< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([]< / span > < span class = "kt" > string< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-68" name = "__codelineno-4-68" href = "#__codelineno-4-68" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "mi" > 0< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "p" > < < / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "o" > ++< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-4-69" name = "__codelineno-4-69" href = "#__codelineno-4-69" > < / a > < span class = "w" > < / span > < span class = "nx" > row< / span > < span class = "p" > [< / span > < span class = "nx" > i< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "s" > " #" < / span >
< a id = "__codelineno-4-70" name = "__codelineno-4-70" href = "#__codelineno-4-70" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-4-71" name = "__codelineno-4-71" href = "#__codelineno-4-71" > < / a > < span class = "w" > < / span > < span class = "nx" > state< / span > < span class = "p" > [< / span > < span class = "nx" > i< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > row< / span >
< a id = "__codelineno-4-72" name = "__codelineno-4-72" href = "#__codelineno-4-72" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-4-73" name = "__codelineno-4-73" href = "#__codelineno-4-73" > < / a > < span class = "w" > < / span > < span class = "c1" > // 记录列是否有皇后< / span >
< a id = "__codelineno-4-74" name = "__codelineno-4-74" href = "#__codelineno-4-74" > < / a > < span class = "w" > < / span > < span class = "nx" > cols< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([]< / span > < span class = "kt" > bool< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-75" name = "__codelineno-4-75" href = "#__codelineno-4-75" > < / a > < span class = "w" > < / span > < span class = "nx" > diags1< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([]< / span > < span class = "kt" > bool< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "mi" > 2< / span > < span class = "o" > *< / span > < span class = "nx" > n< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-76" name = "__codelineno-4-76" href = "#__codelineno-4-76" > < / a > < span class = "w" > < / span > < span class = "nx" > diags2< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([]< / span > < span class = "kt" > bool< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "mi" > 2< / span > < span class = "o" > *< / span > < span class = "nx" > n< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-77" name = "__codelineno-4-77" href = "#__codelineno-4-77" > < / a > < span class = "w" > < / span > < span class = "nx" > res< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([][][]< / span > < span class = "kt" > string< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "mi" > 0< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-78" name = "__codelineno-4-78" href = "#__codelineno-4-78" > < / a > < span class = "w" > < / span > < span class = "nx" > backtrack< / span > < span class = "p" > (< / span > < span class = "mi" > 0< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "nx" > state< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "nx" > res< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "nx" > cols< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "nx" > diags1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "nx" > diags2< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-79" name = "__codelineno-4-79" href = "#__codelineno-4-79" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "nx" > res< / span >
< a id = "__codelineno-4-80" name = "__codelineno-4-80" href = "#__codelineno-4-80" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-4-32" name = "__codelineno-4-32" href = "#__codelineno-4-32" > < / a > < span class = "cm" > /* 求解 n 皇后 */< / span >
< a id = "__codelineno-4-33" name = "__codelineno-4-33" href = "#__codelineno-4-33" > < / a > < span class = "kd" > func< / span > < span class = "w" > < / span > < span class = "nx" > nQueens< / span > < span class = "p" > (< / span > < span class = "nx" > n< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > [][][]< / span > < span class = "kt" > string< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-4-34" name = "__codelineno-4-34" href = "#__codelineno-4-34" > < / a > < span class = "w" > < / span > < span class = "c1" > // 初始化 n*n 大小的棋盘,其中 ' Q' 代表皇后,' #' 代表空位< / span >
< a id = "__codelineno-4-35" name = "__codelineno-4-35" href = "#__codelineno-4-35" > < / a > < span class = "w" > < / span > < span class = "nx" > state< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([][]< / span > < span class = "kt" > string< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-36" name = "__codelineno-4-36" href = "#__codelineno-4-36" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "mi" > 0< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "p" > < < / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "o" > ++< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-4-37" name = "__codelineno-4-37" href = "#__codelineno-4-37" > < / a > < span class = "w" > < / span > < span class = "nx" > row< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([]< / span > < span class = "kt" > string< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-38" name = "__codelineno-4-38" href = "#__codelineno-4-38" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "mi" > 0< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "p" > < < / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "o" > ++< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-4-39" name = "__codelineno-4-39" href = "#__codelineno-4-39" > < / a > < span class = "w" > < / span > < span class = "nx" > row< / span > < span class = "p" > [< / span > < span class = "nx" > i< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "s" > " #" < / span >
< a id = "__codelineno-4-40" name = "__codelineno-4-40" href = "#__codelineno-4-40" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-4-41" name = "__codelineno-4-41" href = "#__codelineno-4-41" > < / a > < span class = "w" > < / span > < span class = "nx" > state< / span > < span class = "p" > [< / span > < span class = "nx" > i< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > row< / span >
< a id = "__codelineno-4-42" name = "__codelineno-4-42" href = "#__codelineno-4-42" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-4-43" name = "__codelineno-4-43" href = "#__codelineno-4-43" > < / a > < span class = "w" > < / span > < span class = "c1" > // 记录列是否有皇后< / span >
< a id = "__codelineno-4-44" name = "__codelineno-4-44" href = "#__codelineno-4-44" > < / a > < span class = "w" > < / span > < span class = "nx" > cols< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([]< / span > < span class = "kt" > bool< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-45" name = "__codelineno-4-45" href = "#__codelineno-4-45" > < / a > < span class = "w" > < / span > < span class = "nx" > diags1< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([]< / span > < span class = "kt" > bool< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "mi" > 2< / span > < span class = "o" > *< / span > < span class = "nx" > n< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-46" name = "__codelineno-4-46" href = "#__codelineno-4-46" > < / a > < span class = "w" > < / span > < span class = "nx" > diags2< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([]< / span > < span class = "kt" > bool< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "mi" > 2< / span > < span class = "o" > *< / span > < span class = "nx" > n< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-47" name = "__codelineno-4-47" href = "#__codelineno-4-47" > < / a > < span class = "w" > < / span > < span class = "nx" > res< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nb" > make< / span > < span class = "p" > ([][][]< / span > < span class = "kt" > string< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "mi" > 0< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-48" name = "__codelineno-4-48" href = "#__codelineno-4-48" > < / a > < span class = "w" > < / span > < span class = "nx" > backtrack< / span > < span class = "p" > (< / span > < span class = "mi" > 0< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "nx" > state< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "nx" > res< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "nx" > cols< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "nx" > diags1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "o" > & < / span > < span class = "nx" > diags2< / span > < span class = "p" > )< / span >
< a id = "__codelineno-4-49" name = "__codelineno-4-49" href = "#__codelineno-4-49" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "nx" > res< / span >
< a id = "__codelineno-4-50" name = "__codelineno-4-50" href = "#__codelineno-4-50" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > n_queens.swift< / span > < pre > < span > < / span > < code > < a id = "__codelineno-5-1" name = "__codelineno-5-1" href = "#__codelineno-5-1" > < / a > < span class = "cm" > /* 回溯算法:N 皇后 */< / span >
< div class = "highlight" > < span class = "filename" > n_queens.swift< / span > < pre > < span > < / span > < code > < a id = "__codelineno-5-1" name = "__codelineno-5-1" href = "#__codelineno-5-1" > < / a > < span class = "cm" > /* 回溯算法: n 皇后 */< / span >
< a id = "__codelineno-5-2" name = "__codelineno-5-2" href = "#__codelineno-5-2" > < / a > < span class = "kd" > func< / span > < span class = "nf" > backtrack< / span > < span class = "p" > (< / span > < span class = "n" > row< / span > < span class = "p" > :< / span > < span class = "nb" > Int< / span > < span class = "p" > ,< / span > < span class = "n" > n< / span > < span class = "p" > :< / span > < span class = "nb" > Int< / span > < span class = "p" > ,< / span > < span class = "n" > state< / span > < span class = "p" > :< / span > < span class = "kr" > inout< / span > < span class = "p" > [[< / span > < span class = "nb" > String< / span > < span class = "p" > ]],< / span > < span class = "n" > res< / span > < span class = "p" > :< / span > < span class = "kr" > inout< / span > < span class = "p" > [[[< / span > < span class = "nb" > String< / span > < span class = "p" > ]]],< / span > < span class = "n" > cols< / span > < span class = "p" > :< / span > < span class = "kr" > inout< / span > < span class = "p" > [< / span > < span class = "nb" > Bool< / span > < span class = "p" > ],< / span > < span class = "n" > diags1< / span > < span class = "p" > :< / span > < span class = "kr" > inout< / span > < span class = "p" > [< / span > < span class = "nb" > Bool< / span > < span class = "p" > ],< / span > < span class = "n" > diags2< / span > < span class = "p" > :< / span > < span class = "kr" > inout< / span > < span class = "p" > [< / span > < span class = "nb" > Bool< / span > < span class = "p" > ])< / span > < span class = "p" > {< / span >
< a id = "__codelineno-5-3" name = "__codelineno-5-3" href = "#__codelineno-5-3" > < / a > < span class = "c1" > // 当放置完所有行时,记录解< / span >
< a id = "__codelineno-5-4" name = "__codelineno-5-4" href = "#__codelineno-5-4" > < / a > < span class = "k" > if< / span > < span class = "n" > row< / span > < span class = "p" > ==< / span > < span class = "n" > n< / span > < span class = "p" > {< / span >
@ -3874,7 +3844,7 @@
< a id = "__codelineno-5-28" name = "__codelineno-5-28" href = "#__codelineno-5-28" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-5-29" name = "__codelineno-5-29" href = "#__codelineno-5-29" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-5-30" name = "__codelineno-5-30" href = "#__codelineno-5-30" > < / a >
< a id = "__codelineno-5-31" name = "__codelineno-5-31" href = "#__codelineno-5-31" > < / a > < span class = "cm" > /* 求解 N 皇后 */< / span >
< a id = "__codelineno-5-31" name = "__codelineno-5-31" href = "#__codelineno-5-31" > < / a > < span class = "cm" > /* 求解 n 皇后 */< / span >
< a id = "__codelineno-5-32" name = "__codelineno-5-32" href = "#__codelineno-5-32" > < / a > < span class = "kd" > func< / span > < span class = "nf" > nQueens< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "p" > :< / span > < span class = "nb" > Int< / span > < span class = "p" > )< / span > < span class = "p" > -> < / span > < span class = "p" > [[[< / span > < span class = "nb" > String< / span > < span class = "p" > ]]]< / span > < span class = "p" > {< / span >
< a id = "__codelineno-5-33" name = "__codelineno-5-33" href = "#__codelineno-5-33" > < / a > < span class = "c1" > // 初始化 n*n 大小的棋盘,其中 ' Q' 代表皇后,' #' 代表空位< / span >
< a id = "__codelineno-5-34" name = "__codelineno-5-34" href = "#__codelineno-5-34" > < / a > < span class = "kd" > var< / span > < span class = "nv" > state< / span > < span class = "p" > =< / span > < span class = "nb" > Array< / span > < span class = "p" > (< / span > < span class = "n" > repeating< / span > < span class = "p" > :< / span > < span class = "nb" > Array< / span > < span class = "p" > (< / span > < span class = "n" > repeating< / span > < span class = "p" > :< / span > < span class = "s" > " #" < / span > < span class = "p" > ,< / span > < span class = "bp" > count< / span > < span class = "p" > :< / span > < span class = "n" > n< / span > < span class = "p" > ),< / span > < span class = "bp" > count< / span > < span class = "p" > :< / span > < span class = "n" > n< / span > < span class = "p" > )< / span >
@ -3890,7 +3860,7 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > n_queens.js< / span > < pre > < span > < / span > < code > < a id = "__codelineno-6-1" name = "__codelineno-6-1" href = "#__codelineno-6-1" > < / a > < span class = "cm" > /* 回溯算法:N 皇后 */< / span >
< div class = "highlight" > < span class = "filename" > n_queens.js< / span > < pre > < span > < / span > < code > < a id = "__codelineno-6-1" name = "__codelineno-6-1" href = "#__codelineno-6-1" > < / a > < span class = "cm" > /* 回溯算法:n 皇后 */< / span >
< a id = "__codelineno-6-2" name = "__codelineno-6-2" href = "#__codelineno-6-2" > < / a > < span class = "kd" > function< / span > < span class = "w" > < / span > < span class = "nx" > backtrack< / span > < span class = "p" > (< / span > < span class = "nx" > row< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > state< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > res< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > cols< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > diags1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > diags2< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-6-3" name = "__codelineno-6-3" href = "#__codelineno-6-3" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当放置完所有行时,记录解< / span >
< a id = "__codelineno-6-4" name = "__codelineno-6-4" href = "#__codelineno-6-4" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > row< / span > < span class = "w" > < / span > < span class = "o" > ===< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
@ -3916,7 +3886,7 @@
< a id = "__codelineno-6-24" name = "__codelineno-6-24" href = "#__codelineno-6-24" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-6-25" name = "__codelineno-6-25" href = "#__codelineno-6-25" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-6-26" name = "__codelineno-6-26" href = "#__codelineno-6-26" > < / a >
< a id = "__codelineno-6-27" name = "__codelineno-6-27" href = "#__codelineno-6-27" > < / a > < span class = "cm" > /* 求解 N 皇后 */< / span >
< a id = "__codelineno-6-27" name = "__codelineno-6-27" href = "#__codelineno-6-27" > < / a > < span class = "cm" > /* 求解 n 皇后 */< / span >
< a id = "__codelineno-6-28" name = "__codelineno-6-28" href = "#__codelineno-6-28" > < / a > < span class = "kd" > function< / span > < span class = "w" > < / span > < span class = "nx" > nQueens< / span > < span class = "p" > (< / span > < span class = "nx" > n< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-6-29" name = "__codelineno-6-29" href = "#__codelineno-6-29" > < / a > < span class = "w" > < / span > < span class = "c1" > // 初始化 n*n 大小的棋盘,其中 ' Q' 代表皇后,' #' 代表空位< / span >
< a id = "__codelineno-6-30" name = "__codelineno-6-30" href = "#__codelineno-6-30" > < / a > < span class = "w" > < / span > < span class = "kd" > const< / span > < span class = "w" > < / span > < span class = "nx" > state< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nb" > Array< / span > < span class = "p" > .< / span > < span class = "kr" > from< / span > < span class = "p" > ({< / span > < span class = "w" > < / span > < span class = "nx" > length< / span > < span class = "o" > :< / span > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "w" > < / span > < span class = "p" > },< / span > < span class = "w" > < / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > => < / span > < span class = "w" > < / span > < span class = "nb" > Array< / span > < span class = "p" > (< / span > < span class = "nx" > n< / span > < span class = "p" > ).< / span > < span class = "nx" > fill< / span > < span class = "p" > (< / span > < span class = "s1" > ' #' < / span > < span class = "p" > ));< / span >
@ -3931,7 +3901,7 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > n_queens.ts< / span > < pre > < span > < / span > < code > < a id = "__codelineno-7-1" name = "__codelineno-7-1" href = "#__codelineno-7-1" > < / a > < span class = "cm" > /* 回溯算法:N 皇后 */< / span >
< div class = "highlight" > < span class = "filename" > n_queens.ts< / span > < pre > < span > < / span > < code > < a id = "__codelineno-7-1" name = "__codelineno-7-1" href = "#__codelineno-7-1" > < / a > < span class = "cm" > /* 回溯算法:n 皇后 */< / span >
< a id = "__codelineno-7-2" name = "__codelineno-7-2" href = "#__codelineno-7-2" > < / a > < span class = "kd" > function< / span > < span class = "w" > < / span > < span class = "nx" > backtrack< / span > < span class = "p" > (< / span >
< a id = "__codelineno-7-3" name = "__codelineno-7-3" href = "#__codelineno-7-3" > < / a > < span class = "w" > < / span > < span class = "nx" > row< / span > < span class = "o" > :< / span > < span class = "w" > < / span > < span class = "kt" > number< / span > < span class = "p" > ,< / span >
< a id = "__codelineno-7-4" name = "__codelineno-7-4" href = "#__codelineno-7-4" > < / a > < span class = "w" > < / span > < span class = "nx" > n< / span > < span class = "o" > :< / span > < span class = "w" > < / span > < span class = "kt" > number< / span > < span class = "p" > ,< / span >
@ -3965,7 +3935,7 @@
< a id = "__codelineno-7-32" name = "__codelineno-7-32" href = "#__codelineno-7-32" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-33" name = "__codelineno-7-33" href = "#__codelineno-7-33" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-7-34" name = "__codelineno-7-34" href = "#__codelineno-7-34" > < / a >
< a id = "__codelineno-7-35" name = "__codelineno-7-35" href = "#__codelineno-7-35" > < / a > < span class = "cm" > /* 求解 N 皇后 */< / span >
< a id = "__codelineno-7-35" name = "__codelineno-7-35" href = "#__codelineno-7-35" > < / a > < span class = "cm" > /* 求解 n 皇后 */< / span >
< a id = "__codelineno-7-36" name = "__codelineno-7-36" href = "#__codelineno-7-36" > < / a > < span class = "kd" > function< / span > < span class = "w" > < / span > < span class = "nx" > nQueens< / span > < span class = "p" > (< / span > < span class = "nx" > n< / span > < span class = "o" > :< / span > < span class = "w" > < / span > < span class = "kt" > number< / span > < span class = "p" > )< / span > < span class = "o" > :< / span > < span class = "w" > < / span > < span class = "kt" > string< / span > < span class = "p" > [][][]< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-37" name = "__codelineno-7-37" href = "#__codelineno-7-37" > < / a > < span class = "w" > < / span > < span class = "c1" > // 初始化 n*n 大小的棋盘,其中 ' Q' 代表皇后,' #' 代表空位< / span >
< a id = "__codelineno-7-38" name = "__codelineno-7-38" href = "#__codelineno-7-38" > < / a > < span class = "w" > < / span > < span class = "kd" > const< / span > < span class = "w" > < / span > < span class = "nx" > state< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nb" > Array< / span > < span class = "p" > .< / span > < span class = "kr" > from< / span > < span class = "p" > ({< / span > < span class = "w" > < / span > < span class = "nx" > length< / span > < span class = "o" > :< / span > < span class = "w" > < / span > < span class = "kt" > n< / span > < span class = "w" > < / span > < span class = "p" > },< / span > < span class = "w" > < / span > < span class = "p" > ()< / span > < span class = "w" > < / span > < span class = "p" > => < / span > < span class = "w" > < / span > < span class = "nb" > Array< / span > < span class = "p" > (< / span > < span class = "nx" > n< / span > < span class = "p" > ).< / span > < span class = "nx" > fill< / span > < span class = "p" > (< / span > < span class = "s1" > ' #' < / span > < span class = "p" > ));< / span >
@ -3980,7 +3950,7 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > n_queens.dart< / span > < pre > < span > < / span > < code > < a id = "__codelineno-8-1" name = "__codelineno-8-1" href = "#__codelineno-8-1" > < / a > < span class = "cm" > /* 回溯算法:N 皇后 */< / span >
< div class = "highlight" > < span class = "filename" > n_queens.dart< / span > < pre > < span > < / span > < code > < a id = "__codelineno-8-1" name = "__codelineno-8-1" href = "#__codelineno-8-1" > < / a > < span class = "cm" > /* 回溯算法:n 皇后 */< / span >
< a id = "__codelineno-8-2" name = "__codelineno-8-2" href = "#__codelineno-8-2" > < / a > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "n" > backtrack< / span > < span class = "p" > (< / span >
< a id = "__codelineno-8-3" name = "__codelineno-8-3" href = "#__codelineno-8-3" > < / a > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > row< / span > < span class = "p" > ,< / span >
< a id = "__codelineno-8-4" name = "__codelineno-8-4" href = "#__codelineno-8-4" > < / a > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > n< / span > < span class = "p" > ,< / span >
@ -4022,7 +3992,7 @@
< a id = "__codelineno-8-40" name = "__codelineno-8-40" href = "#__codelineno-8-40" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-8-41" name = "__codelineno-8-41" href = "#__codelineno-8-41" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-8-42" name = "__codelineno-8-42" href = "#__codelineno-8-42" > < / a >
< a id = "__codelineno-8-43" name = "__codelineno-8-43" href = "#__codelineno-8-43" > < / a > < span class = "cm" > /* 求解 N 皇后 */< / span >
< a id = "__codelineno-8-43" name = "__codelineno-8-43" href = "#__codelineno-8-43" > < / a > < span class = "cm" > /* 求解 n 皇后 */< / span >
< a id = "__codelineno-8-44" name = "__codelineno-8-44" href = "#__codelineno-8-44" > < / a > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > String< / span > < span class = "o" > > > > < / span > < span class = "w" > < / span > < span class = "n" > nQueens< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > n< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-8-45" name = "__codelineno-8-45" href = "#__codelineno-8-45" > < / a > < span class = "w" > < / span > < span class = "c1" > // 初始化 n*n 大小的棋盘,其中 ' Q' 代表皇后,' #' 代表空位< / span >
< a id = "__codelineno-8-46" name = "__codelineno-8-46" href = "#__codelineno-8-46" > < / a > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "n" > List< / span > < span class = "o" > < < / span > < span class = "kt" > String< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "n" > state< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "p" > .< / span > < span class = "n" > generate< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > index< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > => < / span > < span class = "w" > < / span > < span class = "n" > List< / span > < span class = "p" > .< / span > < span class = "n" > filled< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "s2" > " #" < / span > < span class = "p" > ));< / span >
@ -4038,7 +4008,7 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > n_queens.rs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-9-1" name = "__codelineno-9-1" href = "#__codelineno-9-1" > < / a > < span class = "cm" > /* 回溯算法:N 皇后 */< / span >
< div class = "highlight" > < span class = "filename" > n_queens.rs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-9-1" name = "__codelineno-9-1" href = "#__codelineno-9-1" > < / a > < span class = "cm" > /* 回溯算法:n 皇后 */< / span >
< a id = "__codelineno-9-2" name = "__codelineno-9-2" href = "#__codelineno-9-2" > < / a > < span class = "k" > fn< / span > < span class = "nf" > backtrack< / span > < span class = "p" > (< / span > < span class = "n" > row< / span > : < span class = "kt" > usize< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > n< / span > : < span class = "kt" > usize< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > state< / span > : < span class = "kp" > & < / span > < span class = "nc" > mut< / span > < span class = "w" > < / span > < span class = "nb" > Vec< / span > < span class = "o" > < < / span > < span class = "nb" > Vec< / span > < span class = "o" > < < / span > < span class = "nb" > String< / span > < span class = "o" > > > < / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > res< / span > : < span class = "kp" > & < / span > < span class = "nc" > mut< / span > < span class = "w" > < / span > < span class = "nb" > Vec< / span > < span class = "o" > < < / span > < span class = "nb" > Vec< / span > < span class = "o" > < < / span > < span class = "nb" > Vec< / span > < span class = "o" > < < / span > < span class = "nb" > String< / span > < span class = "o" > > > > < / span > < span class = "p" > ,< / span >
< a id = "__codelineno-9-3" name = "__codelineno-9-3" href = "#__codelineno-9-3" > < / a > < span class = "w" > < / span > < span class = "n" > cols< / span > : < span class = "kp" > & < / span > < span class = "nc" > mut< / span > < span class = "w" > < / span > < span class = "p" > [< / span > < span class = "kt" > bool< / span > < span class = "p" > ],< / span > < span class = "w" > < / span > < span class = "n" > diags1< / span > : < span class = "kp" > & < / span > < span class = "nc" > mut< / span > < span class = "w" > < / span > < span class = "p" > [< / span > < span class = "kt" > bool< / span > < span class = "p" > ],< / span > < span class = "w" > < / span > < span class = "n" > diags2< / span > : < span class = "kp" > & < / span > < span class = "nc" > mut< / span > < span class = "w" > < / span > < span class = "p" > [< / span > < span class = "kt" > bool< / span > < span class = "p" > ])< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-9-4" name = "__codelineno-9-4" href = "#__codelineno-9-4" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当放置完所有行时,记录解< / span >
@ -4069,7 +4039,7 @@
< a id = "__codelineno-9-29" name = "__codelineno-9-29" href = "#__codelineno-9-29" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-9-30" name = "__codelineno-9-30" href = "#__codelineno-9-30" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-9-31" name = "__codelineno-9-31" href = "#__codelineno-9-31" > < / a >
< a id = "__codelineno-9-32" name = "__codelineno-9-32" href = "#__codelineno-9-32" > < / a > < span class = "cm" > /* 求解 N 皇后 */< / span >
< a id = "__codelineno-9-32" name = "__codelineno-9-32" href = "#__codelineno-9-32" > < / a > < span class = "cm" > /* 求解 n 皇后 */< / span >
< a id = "__codelineno-9-33" name = "__codelineno-9-33" href = "#__codelineno-9-33" > < / a > < span class = "k" > fn< / span > < span class = "nf" > n_queens< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > : < span class = "kt" > usize< / span > < span class = "p" > )< / span > < span class = "w" > < / span > -> < span class = "nb" > Vec< / span > < span class = "o" > < < / span > < span class = "nb" > Vec< / span > < span class = "o" > < < / span > < span class = "nb" > Vec< / span > < span class = "o" > < < / span > < span class = "nb" > String< / span > < span class = "o" > > > > < / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-9-34" name = "__codelineno-9-34" href = "#__codelineno-9-34" > < / a > < span class = "w" > < / span > < span class = "c1" > // 初始化 n*n 大小的棋盘,其中 ' Q' 代表皇后,' #' 代表空位< / span >
< a id = "__codelineno-9-35" name = "__codelineno-9-35" href = "#__codelineno-9-35" > < / a > < span class = "w" > < / span > < span class = "kd" > let< / span > < span class = "w" > < / span > < span class = "k" > mut< / span > < span class = "w" > < / span > < span class = "n" > state< / span > : < span class = "nb" > Vec< / span > < span class = "o" > < < / span > < span class = "nb" > Vec< / span > < span class = "o" > < < / span > < span class = "nb" > String< / span > < span class = "o" > > > < / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nb" > Vec< / span > ::< span class = "n" > new< / span > < span class = "p" > ();< / span >
@ -4092,7 +4062,7 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > n_queens.c< / span > < pre > < span > < / span > < code > < a id = "__codelineno-10-1" name = "__codelineno-10-1" href = "#__codelineno-10-1" > < / a > < span class = "cm" > /* 回溯算法:N 皇后 */< / span >
< div class = "highlight" > < span class = "filename" > n_queens.c< / span > < pre > < span > < / span > < code > < a id = "__codelineno-10-1" name = "__codelineno-10-1" href = "#__codelineno-10-1" > < / a > < span class = "cm" > /* 回溯算法:n 皇后 */< / span >
< a id = "__codelineno-10-2" name = "__codelineno-10-2" href = "#__codelineno-10-2" > < / a > < span class = "kt" > void< / span > < span class = "w" > < / span > < span class = "nf" > backtrack< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > row< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > n< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > char< / span > < span class = "w" > < / span > < span class = "n" > state< / span > < span class = "p" > [< / span > < span class = "n" > MAX_SIZE< / span > < span class = "p" > ][< / span > < span class = "n" > MAX_SIZE< / span > < span class = "p" > ],< / span > < span class = "w" > < / span > < span class = "kt" > char< / span > < span class = "w" > < / span > < span class = "o" > ***< / span > < span class = "n" > res< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "n" > resSize< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > bool< / span > < span class = "w" > < / span > < span class = "n" > cols< / span > < span class = "p" > [< / span > < span class = "n" > MAX_SIZE< / span > < span class = "p" > ],< / span >
< a id = "__codelineno-10-3" name = "__codelineno-10-3" href = "#__codelineno-10-3" > < / a > < span class = "w" > < / span > < span class = "kt" > bool< / span > < span class = "w" > < / span > < span class = "n" > diags1< / span > < span class = "p" > [< / span > < span class = "mi" > 2< / span > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "w" > < / span > < span class = "n" > MAX_SIZE< / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span > < span class = "p" > ],< / span > < span class = "w" > < / span > < span class = "kt" > bool< / span > < span class = "w" > < / span > < span class = "n" > diags2< / span > < span class = "p" > [< / span > < span class = "mi" > 2< / span > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "w" > < / span > < span class = "n" > MAX_SIZE< / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span > < span class = "p" > ])< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-4" name = "__codelineno-10-4" href = "#__codelineno-10-4" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当放置完所有行时,记录解< / span >
@ -4124,7 +4094,7 @@
< a id = "__codelineno-10-30" name = "__codelineno-10-30" href = "#__codelineno-10-30" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-10-31" name = "__codelineno-10-31" href = "#__codelineno-10-31" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-10-32" name = "__codelineno-10-32" href = "#__codelineno-10-32" > < / a >
< a id = "__codelineno-10-33" name = "__codelineno-10-33" href = "#__codelineno-10-33" > < / a > < span class = "cm" > /* 求解 N 皇后 */< / span >
< a id = "__codelineno-10-33" name = "__codelineno-10-33" href = "#__codelineno-10-33" > < / a > < span class = "cm" > /* 求解 n 皇后 */< / span >
< a id = "__codelineno-10-34" name = "__codelineno-10-34" href = "#__codelineno-10-34" > < / a > < span class = "kt" > char< / span > < span class = "w" > < / span > < span class = "o" > ***< / span > < span class = "nf" > nQueens< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > n< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "n" > returnSize< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-10-35" name = "__codelineno-10-35" href = "#__codelineno-10-35" > < / a > < span class = "w" > < / span > < span class = "kt" > char< / span > < span class = "w" > < / span > < span class = "n" > state< / span > < span class = "p" > [< / span > < span class = "n" > MAX_SIZE< / span > < span class = "p" > ][< / span > < span class = "n" > MAX_SIZE< / span > < span class = "p" > ];< / span >
< a id = "__codelineno-10-36" name = "__codelineno-10-36" href = "#__codelineno-10-36" > < / a > < span class = "w" > < / span > < span class = "c1" > // 初始化 n*n 大小的棋盘,其中 ' Q' 代表皇后,' #' 代表空位< / span >