mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Update n_queens code.
This commit is contained in:
@ -19,8 +19,8 @@ void backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vect
|
||||
// 计算该格子对应的主对角线和副对角线
|
||||
int diag1 = row - col + n - 1;
|
||||
int diag2 = row + col;
|
||||
// 剪枝:不允许该格子所在 (列 或 主对角线 或 副对角线) 包含皇后
|
||||
if (!(cols[col] || diags1[diag1] || diags2[diag2])) {
|
||||
// 剪枝:不允许该格子所在列、主对角线、副对角线存在皇后
|
||||
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
|
||||
// 尝试:将皇后放置在该格子
|
||||
state[row][col] = "Q";
|
||||
cols[col] = diags1[diag1] = diags2[diag2] = true;
|
||||
|
||||
Reference in New Issue
Block a user