This commit is contained in:
krahets
2023-11-27 02:32:06 +08:00
parent 32d5bd97aa
commit a4a23e2488
31 changed files with 179 additions and 213 deletions

View File

@ -207,7 +207,7 @@ comments: true
bool[] cols, bool[] diags1, bool[] diags2) {
// 当放置完所有行时,记录解
if (row == n) {
List<List<string>> copyState = new();
List<List<string>> copyState = [];
foreach (List<string> sRow in state) {
copyState.Add(new List<string>(sRow));
}
@ -236,9 +236,9 @@ comments: true
/* 求解 N 皇后 */
List<List<List<string>>> NQueens(int n) {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
List<List<string>> state = new();
List<List<string>> state = [];
for (int i = 0; i < n; i++) {
List<string> row = new();
List<string> row = [];
for (int j = 0; j < n; j++) {
row.Add("#");
}
@ -247,7 +247,7 @@ comments: true
bool[] cols = new bool[n]; // 记录列是否有皇后
bool[] diags1 = new bool[2 * n - 1]; // 记录主对角线是否有皇后
bool[] diags2 = new bool[2 * n - 1]; // 记录副对角线是否有皇后
List<List<List<string>>> res = new();
List<List<List<string>>> res = [];
Backtrack(0, n, state, res, cols, diags1, diags2);